perf: stream GTFS into typed arrays and grid the walking transfers #2

Merged
madeddie merged 2 commits from perf-typed-feeds into main 2026-11-07 11:18:10 -05:00
Owner

Groundwork for adding bus feeds, with no behaviour change.

Today the loader builds a JS object per csv row and per stop time, and a JS array per connection; walking transfers compare every pair of stations (O(n^2)). That is already the slowest part of startup with subway + Metro-North, and it does not survive buses: MTA's five borough feeds plus MTA Bus Company and Bee-Line add roughly 17k stops and 7.6M stop times, versus ~0.6k and ~0.5M today.

Changes:

  • stream csv files row by row instead of reading them into arrays of row objects
  • hold stop times in flat typed arrays, indexed per trip (Trip.stopStart / stopCount, read via Gtfs.tripStops)
  • size the per-date connection set first, then fill it and sort an index permutation, instead of allocating an array per connection
  • bucket stations into a grid when building walking transfers, so each station is only compared with neighbouring cells
  • keep footpaths in compressed-row typed arrays; the scan reads them by index

Verification

A fixed set of four queries (Overlook Terrace -> Bronx Zoo, Grand Central -> Coney Island, Scarsdale -> Union Square, Astoria -> JFK, transit-only so results are deterministic) returns byte-identical itineraries before and after - same legs, stations, times, fares and transfers.

Startup on the current two feeds:

before after
load 51.1s 31.4s
heap after load 275MB 37MB

Next PR adds the bus feeds themselves (bus mode, fares with free subway<->bus transfers, Bee-Line from the 511NY-hosted feed - the Westchester-hosted copy is stale).

Groundwork for adding bus feeds, with no behaviour change. Today the loader builds a JS object per csv row and per stop time, and a JS array per connection; walking transfers compare every pair of stations (O(n^2)). That is already the slowest part of startup with subway + Metro-North, and it does not survive buses: MTA's five borough feeds plus MTA Bus Company and Bee-Line add roughly 17k stops and 7.6M stop times, versus ~0.6k and ~0.5M today. Changes: - stream csv files row by row instead of reading them into arrays of row objects - hold stop times in flat typed arrays, indexed per trip (`Trip.stopStart` / `stopCount`, read via `Gtfs.tripStops`) - size the per-date connection set first, then fill it and sort an index permutation, instead of allocating an array per connection - bucket stations into a grid when building walking transfers, so each station is only compared with neighbouring cells - keep footpaths in compressed-row typed arrays; the scan reads them by index ## Verification A fixed set of four queries (Overlook Terrace -> Bronx Zoo, Grand Central -> Coney Island, Scarsdale -> Union Square, Astoria -> JFK, transit-only so results are deterministic) returns **byte-identical itineraries** before and after - same legs, stations, times, fares and transfers. Startup on the current two feeds: | | before | after | |---|---|---| | load | 51.1s | 31.4s | | heap after load | 275MB | 37MB | Next PR adds the bus feeds themselves (`bus` mode, fares with free subway<->bus transfers, Bee-Line from the 511NY-hosted feed - the Westchester-hosted copy is stale).
Loading used to build a JS object per csv row and per stop time, then a
JS array per connection; walking transfers compared every pair of
stations. That is already the slowest part of startup with subway +
Metro-North, and does not survive adding bus feeds (~17k stops and
~7.6M stop times).

- stream csv files instead of reading them into row objects
- hold stop times in flat typed arrays indexed per trip
- size the per-date connection set first, then fill and sort an index
  permutation, instead of building an array per connection
- bucket stations into a grid for walking transfers (was O(stations^2))
- store footpaths in compressed-row typed arrays

No behaviour change: the planner returns identical itineraries for a
fixed set of queries. Load 51s -> 31s and 275MB -> 37MB heap on the
current two feeds.
Sorting an index permutation and copying into a fresh set of columns
doubles the peak memory of a service date, which is ~170MB once bus
feeds are loaded. Apply the permutation in place instead (cycle
follow, one byte of scratch per connection). Itineraries unchanged.
madeddie deleted branch perf-typed-feeds 2026-11-07 11:18:11 -05:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
madeddie/ptt!2
No description provided.