perf: build the full-playlist cache from YouTube's own batches, not a re-download - #681
Merged
Conversation
… re-download The cache worked on-device, but the wait before it did was almost entirely self-inflicted: 19:50:42.395 .. 19:50:43.128 YouTube delivers every batch 0.73s 19:50:45.006 .. 19:50:52.963 our collector re-fetches them 8.0s 19:50:52.966 full_cache.stored -> reloading 19:50:53.135 full_cache.injected YouTube had the entire playlist in hand at 43.128. The collector then spent eight seconds re-downloading exactly that, paced at 2.5s per request, and only then could the reload happen — so the helper tiles sat on screen for about ten seconds instead of one. adblock.js already intercepts every continuation response before it filters them, so the raw items can be taken straight from there. noteContinuationBatch accumulates them, and when a response arrives carrying no continuation token the playlist is complete: cache it and reload immediately, with no extra network work at all. The hook is placed BEFORE filterContinuationItems deliberately — after it, an all-watched batch is reduced to the single kept helper. Also cancels the background collector once the native path has completed, since it would otherwise keep issuing requests for data already held. The flag is cleared on navigation and at the start of each collection, so a finished playlist cannot block collection on the next one. The collector is still worth keeping for playlists that are NOT fully watched: there YouTube only fetches as the user scrolls, so the accumulator would fill slowly and the prefetch still does useful work.
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.
The cache works — but almost the entire wait before it was self-inflicted:
YouTube had the whole playlist at 43.128. The collector then spent 8s re-downloading exactly that at 2.5s per request before the reload could happen — so the helpers sat on screen ~10s instead of ~1s.
adblock.js already intercepts every continuation response before filtering, so the raw items are taken straight from there. When a response arrives with no continuation token the playlist is complete: cache and reload immediately, no extra network work.
The hook sits before
filterContinuationItemsdeliberately — after it, an all-watched batch is reduced to the single kept helper.Also cancels the background collector once the native path completes (flag cleared on navigation and at each collection start, so a finished playlist can't block the next one). The collector is still kept for playlists that aren't fully watched, where YouTube only fetches on scroll and the prefetch still does real work.
Expected:
full_cache.from_native→stored→reloading→injectedabout a second after opening, withbatch_collect.cancelledinstead of eight seconds ofbatch_fetched.