From 3af227c3000eb29126ff8e683e290b732813e779 Mon Sep 17 00:00:00 2001 From: Aditya Rana Date: Tue, 3 Feb 2026 00:16:03 +0530 Subject: [PATCH] perf: optimize memory allocation in stops-for-route polyline generation Pre-allocate the coords slice in generatePolylines to the known capacity of shapes. This eliminates repeated memory resizing and copying during iteration, reducing garbage collection pressure for routes with many shape points. --- internal/restapi/stops_for_route_handler.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/restapi/stops_for_route_handler.go b/internal/restapi/stops_for_route_handler.go index 5581a1db..eb3148ca 100644 --- a/internal/restapi/stops_for_route_handler.go +++ b/internal/restapi/stops_for_route_handler.go @@ -388,7 +388,8 @@ func processTripGroups( func generatePolylines(shapes []gtfsdb.GetShapesGroupedByTripHeadSignRow) []models.Polyline { var polylines []models.Polyline - var coords [][]float64 + // This prevents repeated memory re-allocation during the loop. + coords := make([][]float64, 0, len(shapes)) for _, shape := range shapes { coords = append(coords, []float64{shape.Lat, shape.Lon}) }