perf: start the load at song:loading, before the highway is drawn (fixes the 698ms video freeze) - #40
Conversation
Building the graph hands every stem's decoded PCM to the audio worklet — which
means copying the WHOLE SONG. For a 4-minute 6-stem pack that is over half a
GIGABYTE of memcpy, in one frame, on the main thread.
It ran on the highway's WS `ready`, i.e. with the player already on screen, and
it froze the picture. Measured on a real load: a 698 ms frame, right as the
song-credits card appeared, with the venue video visibly stopping ("it's the
video that pauses/drops frames the moment the author appears").
The reason it could not start sooner is that the stem list only ever arrived with
that WS `ready`. Core now publishes the same list at GET /api/song/{f}?stems=1
(feedBack#…), so the whole load — fetch, decode, graph build — starts on
`song:loading` instead. Nothing about the work changes; only WHEN.
Verified on the real build. The ~700 ms stall moved to BEFORE the highway is up,
behind the loading overlay where a stalled frame costs nothing:
before: the 698 ms frame lands AT the credits, venue on screen
after: 709 ms at frame #98; song:loaded (highway up) is frame #102
worst frame after the highway appears: 698 ms -> 169 ms
Safe by construction:
- the highway's list still wins the moment it arrives; the preload is only a
stand-in before it. Core builds both from one shared helper and pins them as
byte-identical, so the signature matches and the ready path skips a second,
identical build rather than rebuilding.
- generation-counted: an in-flight preload for a previous song cannot overtake
a newer one, or it would build the wrong song's graph over the current one.
- entirely best-effort: an offline core, a non-200, an older core that does not
know ?stems=1, or a stem-less pack all fall through to the old ready-driven
path.
Tests: 6 new (all failing against the pre-fix source), suite 53/53.
📝 WalkthroughWalkthroughPlayback loading now preloads stem metadata from the REST API, stores it in shared state with generation protection, and allows initialization to use matching preload data until live highway metadata becomes available. ChangesStem metadata preload
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Playback
participant Loading
participant Preload
participant REST
participant Init
participant Highway
Playback->>Loading: emit song loading
Loading->>Preload: preload current filename
Preload->>REST: request stems metadata
REST-->>Preload: return stem metadata
Preload-->>Init: provide matching preload info
Init->>Highway: check live song info
Highway-->>Init: return live metadata when ready
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main.js (1)
972-972: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valuePreload fetch has no abort signal.
Unlike the other fetches in this file (
probe,loadStems,loadFullMix), this fetch isn't tied to anAbortController. A superseded preload keeps downloading to completion even though its result will be discarded by the gen/filename check — harmless for correctness, just a wasted request on rapid song switches.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/main.js` at line 972, Update the preload fetch in the surrounding preload flow to accept and pass the same AbortController signal used by probe, loadStems, and loadFullMix. Ensure superseded preloads are aborted while preserving the existing gen/filename result validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/main.js`:
- Line 972: Update the preload fetch in the surrounding preload flow to accept
and pass the same AbortController signal used by probe, loadStems, and
loadFullMix. Ensure superseded preloads are aborted while preserving the
existing gen/filename result validation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: e996b648-b42f-4030-91ca-f1afb6e547ad
📒 Files selected for processing (3)
src/main.jssrc/state.jstests/preload.test.mjs
* fix: recover player when initial stems stream pump fails setupStreaming() reports success once the graph/worklet are built, but runPump() keeps prefilling asynchronously afterward. If that prefill rejected (a stem reader dying, a dropped fetch), the failure was only logged: core <audio> stayed paused (silenced for the takeover) with nothing resuming it, so the play request stayed pending and the player appeared to be loading/playing with no audible output. Route a genuine pump failure (not a superseded seek, not an intentional stop) through the same recovery already used for a synchronous setupStreaming()/buildGraphFromBuffers() failure: tear the takeover down, clear the reroute guard, and resume core <audio> if playback was requested. Fixes got-feedBack#40 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * refactor: consolidate stems takeover-failure recovery into one helper Codacy review on PR #6 flagged that the recovery sequence (teardown + clear reroute guard + resume core <audio>) was duplicated across four failure sites in onSongReady/onStreamFailure, three of them also calling hideOverlay() redundantly since teardown() already hides it. Extract _revertToCoreAudio(shouldResume) (teardown + reroute-guard clear + conditional resume) and _resumeCoreAudio() (resume only, for the one site where buildGraphFromBuffers already tore down internally) and use them at all four sites. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Building the graph hands every stem's decoded PCM to the audio worklet — which means copying the whole song. For a 4-minute 6-stem pack that is over half a gigabyte of memcpy, in one frame, on the main thread.
It ran on the highway's WS
ready— with the player already on screen — and it froze the picture. Measured on a real load: a 698 ms frame, right as the song-credits card appeared, with the venue video visibly stopping. That is the "it's the video that pauses/drops frames the moment the author appears" report.The reason it could not start sooner: the stem list only ever arrived with that WS
ready. Core now publishes the same list atGET /api/song/{f}?stems=1(got-feedBack/feedBack#972), so the whole load — fetch, decode, graph build — starts onsong:loadinginstead. Nothing about the work changes; only when.Verified on the real build
The ~700 ms stall moved to before the highway is up, behind the loading overlay where a stalled frame costs nothing:
Safe by construction
?stems=1, or a stem-less pack all fall through to the old ready-driven path.Tests: 6 new (all failing against the pre-fix source), suite 53/53.
Summary by CodeRabbit
Performance Improvements
Reliability
Tests