Perf/vehicles for agency route index#763
Open
Adityatorgal17 wants to merge 2 commits intoOneBusAway:mainfrom
Open
Perf/vehicles for agency route index#763Adityatorgal17 wants to merge 2 commits intoOneBusAway:mainfrom
Adityatorgal17 wants to merge 2 commits intoOneBusAway:mainfrom
Conversation
Member
|
Please fix the merge conflicts, then this will be ready to go! |
aaronbrethorst
approved these changes
Mar 25, 2026
Member
aaronbrethorst
left a comment
There was a problem hiding this comment.
Hey Aditya, this is a clean performance win — replacing the O(all vehicles) scan with O(agency routes) prebuilt index lookups is the right pattern, and it follows the same design used for realTimeTripLookup and realTimeVehicleLookupByTrip. The test coverage is thorough.
Critical Issues (0 found)
(none)
Important Issues (0 found)
(none)
Suggestions (1 found)
1. PR description mentions a vehicles_for_agency_handler_test.go endpoint regression test that wasn't included
The description says "adds an endpoint regression test proving vehicles on two different routes of the same agency both appear in /vehicles-for-agency/{id}", but no changes were made to that file. The manager-level tests cover the core logic well, so this isn't blocking — but the endpoint-level test would be a nice addition in a follow-up.
Strengths
- The index build in
rebuildMergedRealtimeLockedis the correct place — it follows the existing pattern for all other realtime lookup maps - Route ID dedup in
VehiclesForAgencyIDprevents duplicate vehicles whenroutesByAgencyIDhas duplicates - Test coverage is excellent: route-bucket union, dedup, filtering invalid vehicles, stale-index clearing on rebuild, and no-match empty result
- Benchmarks are included for both rebuild and lookup paths
- Mock helpers (
MockAddVehicle,MockAddVehicleWithOptions,MockResetRealTimeData) are all updated to keepvehiclesByRoutein sync - The bonus
MockClearServiceIDsCachefix in the arrivals test addresses a pre-existing flaky test
Verification
make lint: 0 issuesmake test: All packages pass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the scan-based
VehiclesForAgencyID()implementation with a prebuilt realtimevehiclesByRouteindex, removing the need to iterate over all realtime vehicles for each/vehicles-for-agency/{id}request.Closes #762
Changes
internal/gtfs/gtfs_manager.govehiclesByRoute map[string][]gtfs.VehicletoManagerVehiclesForAgencyID()to union prebuilt route buckets instead of scanningrealTimeVehiclesnilinternal/gtfs/realtime.govehiclesByRouteinsiderebuildMergedRealtimeLocked()Trip != niland non-emptyTrip.ID.RouteID, matching the old filtering behaviorinternal/gtfs/gtfs_manager_mock.govehiclesByRoutein syncMockResetRealTimeData()internal/gtfs/gtfs_manager_test.gointernal/restapi/vehicles_for_agency_handler_test.go/vehicles-for-agency/{id}internal/gtfs/realtime_benchmark_test.govehiclesByRouteVehiclesForAgencyID()Why
routesByAgencyIDis already available from static GTFS data, so pairing it with a realtimeroute -> vehiclesindex is a natural fit. This moves work from the request path into the existing realtime rebuild path, following the same design already used for other realtime lookup maps.Validation Passed
make test