Repository navigation
perf(playback): stop at the newest segment when reading transcode progress - #2954
Conversation
…gress SegmentProgress statted every segment the playlist lists to find the produced head and its write time. FFmpeg lists up to 50,000 segments, and the throttler (every five seconds) and every missing-segment request read this, so the number of stat calls grew with playback position. FFmpeg numbers segments in order, renames each finished segment into place before rewriting the playlist to list it, and lists only its own segments, so the last listed segment still on disk is both the highest and the newest. Walk back from the tail and stop there. The playlist is still read and parsed in full. ProducedCount had no readers and needed the full walk, so it is removed.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 39 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Seeking to a missing HLS segment can now respond sooner because Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
|
I haven't added a recording for this one. The change removes stat calls from a server-side check: at 50,000 listed segments a call drops from 97.7 ms to 3.3 ms on a local SSD, and at 3,600 from 5.9 ms to 0.26 ms (see Benchmarks). A seek recording on local storage wouldn't show a difference that small, and nothing on screen changes, so the Evidence section stays "none, no user-visible change". |
Review summaryReviewed head: I found no defects, so this review has no inline findings and I pushed no commits. What I checked
Remaining for maintainers
Validated features No passed case on the v1.0 board reaches this code. #1148 C3 and #1149 C2 are not run yet. The PR body's User manual and clients The manual is unaffected: no setting, default or visible behavior changes. No API change, so neither Apple nor Android needs follow-up. Fit: 9/10 · Readiness: 9/10 — ready
Review by Claude Code (claude-opus-5-5) acting for a maintainer. |
Main's Silo-Server#3033 moves recording a planned segment request into notePlannedSegmentRequest, which its subtitle renditions also call. Keep this branch's narrower near-head case and its planned progress, manifest and audio recipe helpers, and record the request through main's helper. Main's Silo-Server#2954 drops SegmentProgress.ProducedCount, which nothing read; planned progress no longer sets it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
Related issue: N/A
Validation tasks: none passed yet. This reaches the seek paths of #1149 C2 and #1148 C3, both not run.
Every time Silo checks how far FFmpeg has got with a transcode, it reads the whole HLS playlist and stats every segment file the playlist lists. FFmpeg is started with
-hls_list_size 50000, so a long session can list tens of thousands of segments, and a copy session (or a keyframe-planned one, which restarts from segment 0) fills the playlist quickly. That check (TranscodeSession.SegmentProgress) runs:SegmentRecoveryDecision. That is every startup and every seek, on the native API, transcode nodes and jellycompat.So the number of stat calls behind a seek, and behind each throttler tick, grows with playback position. It gets worse when the transcode directory is on a network filesystem, where each stat is a round trip.
This change stops at the newest segment instead of statting all of them.
Approach
SegmentProgressneeds two things from the segment files: the highest listed segment that exists with data (ProducedHead), and when it was written (LastProducedAt). It now walks the playlist from the tail and stops at the first listed segment that exists with a size above zero.That gives the same answer as the full walk because of how FFmpeg writes the playlist. I checked this in
libavformat/hlsenc.cfrom FFmpeg 8.0 (Silo ships jellyfin-ffmpeg 8.1), both for a segment cut inhls_write_packetand for the last segment inhls_write_trailer:.tmpfile in one go and renamed into place (-hls_flags temp_file). Only then is it appended to the list and the playlist rewritten, itself through a temp file and a rename.-start_number. Withoutappend_list, a playlist lists only the segments of the process that wrote it.So within one playlist, entries go up in both number and write time, and every listed segment was complete before it was listed. The last listed segment still on disk is the highest and the newest. A pruned prefix, a gap, listed segments that are missing and zero-byte files are all skipped on the way back, as before.
ProducedHeadkeeps its floor ofStartSegmentNumber - 1, so a playlist an earlier generation left behind a later restart point still reports the floor, andLastProducedAtstill comes from the segment found.The two walks can differ in one case. An encoded restart with an unchanged recipe keeps the earlier generation's playlist on disk. That playlist could list a segment the new process has already rewritten, before the new process replaces the playlist. FFmpeg replaces it straight after renaming its first segment, in the same call, so the window is the gap between two renames. If FFmpeg died inside that window, the session would report
transcode_not_runningbefore the staleness check that readsLastProducedAt, so no decision changes. Before the new process's first segment, both walks return the old generation's head and time. When nothing listed is on disk, the walk still visits every entry, which costs the same as before.The whole playlist is still read and parsed; the stats were most of the cost (see Benchmarks). Reading only the tail, as the segment pruner does, would also cut the parse, but #2515 checks the full parsed timeline in this function, so I left that out.
ProducedCountis removed. Nothing read it outside two test assertions, and filling it would need the full walk. #1770 assigns it in its newplannedSegmentProgress, so whichever of the two merges second has to drop that assignment.Validation
go testruns oninternal/playbackforSegmentProgress, the recovery decisions and the throttler: pass. CI runs the full gate.transcode_manifest_test.gocovers a pruned prefix; a missing tail, a zero-byte tail, a gap and a newer unlisted file in one playlist; a playlist left behind a later restart point; and nothing listed on disk. It checksProducedHead,LastProducedAtandHasManifest. It also passes against the parent commit'stranscode.go, so the old and new walks agree on every row. Removing theStartSegmentNumber - 1floor makes it fail. Existing tests already cover a missing playlist and the.m4snaming.Benchmarks
Measured with
BenchmarkSegmentProgress, added in this PR. It calls onlySegmentProgress, so the same benchmark ran against the parent'stranscode.go(db95c13) and this branch's (eea621c). The branch binary was built before a code comment intranscode.gowas reworded; the compiled code is the same. Each case writes a playlist and one 1-byte file per listed segment into a temp directory, then timesSegmentProgresswith every segment present, which is the steady state during playback.-hls_list_sizecap).GOMAXPROCS=6.go test -conce per commit, then the two binaries alternated 10 times with-test.run '^$' -test.bench '^BenchmarkSegmentProgress$' -test.benchmem -test.count 1, compared withbenchstat.What is left after the change is reading and parsing the playlist. Limitations: this is one local SSD. I did not measure a network filesystem, where each stat is a round trip, a real FFmpeg run or a production server.
Evidence
Evidence: none, no user-visible change. No screen, response or setting changes; the server spends less time before answering a missing-segment request on a long session.
Risks
Low. The result matches the full walk for every playlist FFmpeg writes, apart from the two-rename window above. The function still reads the disk without holding
s.mu, as before.Checklist
AI Disclosure
ProducedCountassignment, which is noted above, and a test that kept a copy of the old walk; that test was rewritten with explicit expected values.