perf(search): parallelize SONG_AS_TRACK validate loop#535
Merged
Conversation
`search_song_as_track` awaited `validate_track_on_release` per Discogs candidate serially. With ~7 candidates per lookup at ~1.2s each that's an 8-10s serial chain on `/api/v1/lookup`'s p95/p99 hot path — the dominant contributor to the early-May regression. Mirror the shape `search_compilations_for_track` already uses (extract per-release work into a helper, gather concurrently, walk results in input-index order to preserve relevance ranking during dedup). An orchestrator-local `Semaphore(5)` caps per-request fan-out at the same size as the global Discogs semaphore in `discogs/service.py:373`; the global rate limiter still sits underneath, so cross-request load is unaffected. Bench (5 candidates x 200ms validate): serial 1.007s -> parallel 0.20s (5x). Same inputs in -> same `matched_items` / `matched_via_by_id` out; order and hint accumulation pinned by four new tests in `test_orchestrator_helpers.py::TestSearchSongAsTrack`.
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.
Closes #534.
Summary
search_song_as_trackawaiteddiscogs_service.validate_track_on_releaseonce per Discogs candidate serially — with ~7 candidates per lookup at ~1.2s each (worse when the PG cache write path is degraded, Sentry LIBRARY-METADATA-LOOKUP-9), an 8-10s serial chain on/api/v1/lookup's hot path.search_compilations_for_trackpattern: extract per-release library-match + validate into_validate_one(release), fan out withasyncio.gather, walk results in input-index order for the dedup pass so relevance ranking andmatched_via_by_idhint order are preserved.Semaphore(5)caps per-request fan-out at the same size as the global Discogs semaphore (discogs/service.py:373); the global rate limiter still sits underneath.Test plan
[A, C]even when B finishes firstmatched_via_by_id[id]is[X_hint, Y_hint]TestSearchSongAsTracktests still passruff check ./ruff format --check ./mypy . --ignore-missing-importsall clean