Skip to content

Seeking performance: Avoid full decode of every visited frame when seeking - #943

Open
enzo1982 wants to merge 1 commit into
xiph:masterfrom
enzo1982:seek-optimization
Open

enzo1982 wants to merge 1 commit into
xiph:masterfrom
enzo1982:seek-optimization

Conversation

@enzo1982

Copy link
Copy Markdown
Contributor

This improves seeking performance by reading only frame headers while looking for the frame containing the target sample.

I had the idea for this a few years back. We are using FLAC in our DAW products at Fender and are performing lots of seeks when users move around in projects with a large number of FLAC files. I wrote a prototype back then, but was still fighting false sync issues, so this never got adapted and the unfinished patch laid around for a few years.

I decided to revisit this now and try to finish it with AI assistance, so this was authored with help from Claude Code and the Opus 5.5 model. I was able to fix the remaining issues this way and implement a comprehensive test that doubled as a benchmark during development.

The final version of this patch reduces the number of false syncs by also looking at the frame header immediately following the current frame and only accepting the current frame if the next header agrees. There still is a very small chance for false syncs, which can cause the header-only seek operation to fail. The algorithm then falls back to full-decode seeking to ensure that any seek operation behaves the same way as without the optimization.

To quickly skip to the next possible sync point, the patch also introduces a new FLAC__bitreader_skip_until_byte_ff function and corresponding test.

The added test/benchmark shows a 5-15x improved seeking performance, depending on file contents and format.

This improves seeking performance by reading only frame
headers while looking for the frame containing the target
sample.

This header-only seeking can fail (e.g. because of a false
sync), so the algorithm falls back to full-decode seeking
in that case.

Also adds tests for a newly introduced bitreader function
and the seeking algorithm itself. The seeking test was used
to benchmark the improvement and showed a 5-15x speedup
depending on file contents and format.

Authored with assistance from Claude Code Opus 5.5.
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.

1 participant