Skip to content

Comments

refactor: optimize trip filtering and fix service date calculation#398

Merged
aaronbrethorst merged 1 commit intoOneBusAway:mainfrom
3rabiii:refactor/optimize-arrivals-logic
Feb 18, 2026
Merged

refactor: optimize trip filtering and fix service date calculation#398
aaronbrethorst merged 1 commit intoOneBusAway:mainfrom
3rabiii:refactor/optimize-arrivals-logic

Conversation

@3rabiii
Copy link
Contributor

@3rabiii 3rabiii commented Feb 12, 2026

Context

This PR addressing suggestions from the previous review regarding arrivalsAndDeparturesForStopHandler.

Changes

  • Fix serviceDate: Updated serviceDateMillis to use the calculated serviceMidnight instead of the raw request timestamp. This ensures the date correctly represents midnight of the service day.
  • Optimize Filtering: Eliminated the expensive GetTripsByServiceID database query which loaded all trips into memory. Instead, stopTimes are now filtered directly using ServiceID (via an O(1) lookup set from activeServiceIDs).

Impact

  • Corrects the semantic meaning of serviceDate in the response.
  • Significantly reduces database load and memory usage by avoiding fetching all trips for active services.
Screenshot From 2026-02-12 04-59-34

@aaronbrethorst
fixes : #397

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.

Elegant simplification, Adel. Replacing a full database round-trip with an in-memory set lookup is exactly the right move here, and the serviceDate fix catches a subtle semantic bug.

What Changed

1. Eliminate GetTripsByServiceID query — Instead of fetching all trips for active service IDs from the database (potentially thousands of rows) just to build a trip ID filter set, the code now builds an activeServiceIDSet directly from the already-available activeServiceIDs slice and filters allStopTimes by st.ServiceID. This is semantically equivalent — a stop time is active iff its service is active — and avoids a costly DB round-trip plus the memory for the full trip result set.

2. Fix serviceDateMillis — Previously computed from params.Time.UnixMilli() (the raw request timestamp, e.g., 14:30:00). Now computed from serviceMidnight.UnixMilli(), which correctly represents midnight of the service day. This matches the semantic meaning of serviceDate in the OBA API response.

Review

No issues found. Both changes are correct, well-motivated, and result in a net reduction of 6 lines. The ServiceID field on GetStopTimesForStopInWindowRow (confirmed in query.sql.go:2643) makes the in-memory filtering straightforward. Existing tests all pass.

@3rabiii
Copy link
Contributor Author

3rabiii commented Feb 18, 2026

Hi Aaron, since this is approved, could we get it merged? Thanks!
@aaronbrethorst

@aaronbrethorst aaronbrethorst merged commit ce61dca into OneBusAway:main Feb 18, 2026
4 checks passed
@aaronbrethorst
Copy link
Member

@3rabiii my bad, sorry!

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.

refactor: Optimize arrivals filtering and fix serviceDate midnight

2 participants