Optimize CELT decode hot paths#122
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #122 +/- ##
==========================================
+ Coverage 82.98% 83.28% +0.29%
==========================================
Files 26 27 +1
Lines 5825 5969 +144
==========================================
+ Hits 4834 4971 +137
- Misses 765 766 +1
- Partials 226 232 +6
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:
|
RFC 6716 / 8251 conformationStatus: pass The action extracts the RFC 6716 reference implementation, applies the RFC 8251 decoder update patch, and then builds the patched reference tools. Legend: numeric cells are Inputs use the shared RFC 6716 / RFC 8251 bitstream corpus; accepted references follow RFC 8251 Section 11.
Run output |
JoTurk
approved these changes
Jun 8, 2026
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.
Summary
Optimize CELT decode hot paths after the iterative FFT rewrite by:
1..8bit range-coder chunks directlyThis remains a pure-Go implementation. It does not change the public API or Opus bitstream behavior.
Algorithm
CELT PVQ shape decoding maps a range-coded codeword index into a signed pulse vector:
CWRS decoding
The generic CWRS decoder walks a recurrence row while recovering one signed pulse-vector coordinate at a time. That row depends only on the vector dimension and pulse count, so this change caches immutable rows across frames.
For dimensions 2, 3, and 4, the recurrence has closed-form counts:
The generic decoder now handles the leading coordinates and delegates the last four dimensions to the specialized path. Exhaustive tests compare the specialized decoder and the four-dimensional tail against the generic algorithm across small codebooks.
Scratch reuse
CELT band decoding previously allocated normalized bands, low-band folding storage, pulse vectors, temporary float storage, and collapse masks repeatedly. These buffers now live in decoder scratch storage and are reused across frames.
The new
internal/slicetoolspackage centralizes the existing scratch-slice pattern:Resizereuses capacity for buffers that callers fully overwriteResizeZeroreuses capacity and clears buffers that must start zeroedSILK and CELT both use the shared helpers.
Range-coder bits
getBitspreviously calledgetBitonce per bit. The common1..8bit case now loads at most two adjacent bytes, shifts and masks the requested field, and advancesbitsReadonce. The original bit-by-bit loop remains as the fallback.Tests cover a byte-boundary crossing and exhaustively compare the fast path against repeated
getBitcalls for all valid offsets and widths from 1 through 8.Remaining loop cleanup
The smaller hot-loop changes:
math.Pow(2, x)withmath.Exp2(x)Performance
Measured before the final shared-helper cleanup using the production-shaped 48 kHz stereo RFC conformance corpus and
DecodeToInt16, with alternating full-corpus runs:mainafter iterative FFT PR26,170 packets/s1.03M allocs/op32,856 packets/s8,101 allocs/ophraban/opus.v258,327 packets/s0 allocs/opThe stack adds approximately
25.5%throughput over the iterative-FFT baseline and removes most per-frame allocation pressure.The final shared-helper extraction was also checked with
go test -gcflags=-m=1: the compiler inlinesslicetools.Resizeandslicetools.ResizeZeroat the CELT and SILK call sites.Validation
The complete RFC conformance matrix passes.