Reuse the encoder analysis scratch buffers - #211
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #211 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 58 58
Lines 10476 10481 +5
=======================================
+ Hits 9760 9765 +5
Misses 505 505
Partials 211 211
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
FrantaBOT
approved these changes
Aug 13, 2026
thomas-vilte
force-pushed
the
perf/encoder-allocs
branch
from
August 13, 2026 21:03
689206a to
904f981
Compare
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.
Description
EncodeFramewas allocating 15 buffers and 11 KB per frame. At 50 frames per second, that's approximately 550 KB/s of garbage per stream, and the encoder's hot path had zero allocations before I touched it: the pitch search and tf_analysis ports broke it.Profiling the stereo frame, the allocations came from:
bandEncodeState.floatScratchceltLPCpitchSearchtfAnalysisremoveDoublingThese are all bounded buffers that can be resized once. This PR adds
encoderScratch—the same pattern as thedecoderScratchalready present insynthesis.go—and gives the band state its own persistent scratch instead of starting it null at the beginning of each frame.The sizes are outside the limits of the mode, not the run: the worst case of
pitchSearchis the window decimated by four, that oftfAnalysisis the widest band at the longest frame, and that ofremoveDoublingis half the maximum period of the comb filter.Measurement
The frame time doesn't change appreciably (320 µs stereo before and after on this machine); what changes is the pressure on the GC.
Nothing else changes
The output is byte by byte identical to that of main: I compared the decoded PCM of 60 seconds of music at 96 kb/s CBR and 24 kb/s VBR, and the files are the same. Reusing memory is precisely the type of change where an uncleaned buffer silently alters the result, so the check is the key:
celtLPCand the correlation accumulator ofpitchSearchare explicitly cleaned because their code assumes a zero starting state; the rest is written entirely before being read.Reference issue
Part of #34.