Skip to content

Optimize memory allocation in stops-for-route polyline generation#299

Merged
aaronbrethorst merged 1 commit intoOneBusAway:mainfrom
ARCoder181105:perf/optimize-stops-for-route-polyline-allocs
Feb 6, 2026
Merged

Optimize memory allocation in stops-for-route polyline generation#299
aaronbrethorst merged 1 commit intoOneBusAway:mainfrom
ARCoder181105:perf/optimize-stops-for-route-polyline-allocs

Conversation

@ARCoder181105
Copy link
Contributor

Description:
This PR optimizes the generatePolylines function in internal/restapi/stops_for_route_handler.go by pre-allocating the coordinate slice.

Changes:

  • Pre-allocated the coords slice to len(shapes) to prevent repeated memory re-allocations during iteration.
  • Reduces garbage collection pressure for routes with significant shape data.

Note: This applies the same optimization pattern recently added to the shapes handler.

Closes #298

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.
Copy link
Member

@aaronbrethorst aaronbrethorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Aditya, thanks for this allocation optimization to generatePolylines. The change is spot-on — since every element in shapes produces exactly one coordinate pair, pre-allocating with len(shapes) is the ideal capacity and completely eliminates intermediate slice growth.

@aaronbrethorst aaronbrethorst merged commit 0be1862 into OneBusAway:main Feb 6, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize slice allocation in stops-for-route polyline generation

2 participants