perf: cache parsed feeds, and speed up the csv and connection paths #5

Merged
madeddie merged 1 commit from perf-startup-scan into main 2026-11-07 15:05:39 -05:00
Owner

Follow-up to #3. With the bus feeds loaded, startup was ~132s and the first query of a service date another ~10s - a container that looks hung.

Changes

  • stop_times.txt is read as byte ranges, not strings: fields are located by offset, times and sequences parsed straight from the bytes, and the repeated trip_id / stop_id lookups memoised. 8.86M rows, so a string per field dominated startup. Parse: ~125s -> ~80s.
  • Binary cache of the parsed feeds (data/feeds.cache): a json header for the small string-heavy tables, raw typed-array bytes for the rest, keyed by the size and mtime of every feed file. The image build writes it (PTT_BUILD_CACHE=1 node dist-server/index.js), the container reads it. Stale, missing or unreadable cache falls back to parsing and rewrites it, so nothing depends on the file being there.
  • Counting sort for connections: each service date's connections are ordered by two stable counting sorts on departure and arrival seconds instead of a comparator sort over ~2.2M entries.
  • Today's connections are built at startup, so the first real request doesn't pay for them.

Results (all four feeds, 16,911 stations / 293,660 trips)

before after
startup 132s 3.7s (cold parse 85s, then cached)
heap after load 248MB 83MB
Overlook Terrace -> Bronx Zoo 11.7s 4.8s
Scarsdale -> Union Square 10.9s 4.6s
Astoria -> JFK 8.3s 3.6s
Getty Square -> Woodlawn 1.3s 1.4s

Against the running server (warm, today's timetable prepared at boot) Overlook Terrace -> Bronx Zoo answers in 2.2s.

The benchmark cases return identical itineraries to before this branch.

Still open

Query time is now scan cost, not setup: a few seconds for long trips because the scan sweeps every connection in the day. Narrowing it to a corridor around the origin and destination is the next step, and belongs in its own PR.

Follow-up to #3. With the bus feeds loaded, startup was ~132s and the first query of a service date another ~10s - a container that looks hung. ## Changes - **stop_times.txt is read as byte ranges**, not strings: fields are located by offset, times and sequences parsed straight from the bytes, and the repeated `trip_id` / `stop_id` lookups memoised. 8.86M rows, so a string per field dominated startup. Parse: ~125s -> ~80s. - **Binary cache of the parsed feeds** (`data/feeds.cache`): a json header for the small string-heavy tables, raw typed-array bytes for the rest, keyed by the size and mtime of every feed file. The image build writes it (`PTT_BUILD_CACHE=1 node dist-server/index.js`), the container reads it. Stale, missing or unreadable cache falls back to parsing and rewrites it, so nothing depends on the file being there. - **Counting sort for connections**: each service date's connections are ordered by two stable counting sorts on departure and arrival seconds instead of a comparator sort over ~2.2M entries. - **Today's connections are built at startup**, so the first real request doesn't pay for them. ## Results (all four feeds, 16,911 stations / 293,660 trips) | | before | after | |---|---|---| | startup | 132s | **3.7s** (cold parse 85s, then cached) | | heap after load | 248MB | 83MB | | Overlook Terrace -> Bronx Zoo | 11.7s | 4.8s | | Scarsdale -> Union Square | 10.9s | 4.6s | | Astoria -> JFK | 8.3s | 3.6s | | Getty Square -> Woodlawn | 1.3s | 1.4s | Against the running server (warm, today's timetable prepared at boot) Overlook Terrace -> Bronx Zoo answers in **2.2s**. The benchmark cases return **identical itineraries** to before this branch. ## Still open Query time is now scan cost, not setup: a few seconds for long trips because the scan sweeps every connection in the day. Narrowing it to a corridor around the origin and destination is the next step, and belongs in its own PR.
Starting the server with the bus feeds loaded took ~132s, and the first
query of a service date another ~10s, which reads as a hung container.

- read stop_times.txt as byte ranges instead of building a string per
  field, and memoise the repeated trip_id / stop_id lookups: the parse
  drops from ~125s to ~80s
- write the parsed feeds to a binary cache (json header for the small
  string tables, raw typed-array bytes for the rest) keyed by the size
  and mtime of every feed file; the image build produces it, so the
  container loads in ~3.7s and falls back to parsing if it is missing
  or stale
- sort each service date's connections with two stable counting sorts
  rather than a comparator sort over millions of entries
- build today's connections at startup, so the first request does not
  pay for them

Itineraries are unchanged: the benchmark cases return results identical
to before this branch.
madeddie deleted branch perf-startup-scan 2026-11-07 15:05:39 -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!5
No description provided.