Repository navigation
perf(mp3): share Layer I/II scale info with the main-data window (#4116) - #4134
Conversation
The fixed-point decoder was using 1,480 bytes of stack in
mp3dec_decode_frame_r against a 2 KiB budget -- 94% -- and almost all of it was
one local. L12_scale_info carries mantissa plus exponent for 192 scalefactors
in the fixed build, about 1090 bytes, and it sat on the stack. At 94% the
figure moved with the compiler's inlining decisions rather than with anything
in the decoder, which is not a margin worth defending.
Moving it to the heap arena would normally cost working RAM, and there were
only 692 bytes spare against the 24 KiB budget. Overlapping it with Layer III's
maindata costs nothing instead: a frame is one layer or the other, never both,
and maindata is at least 1951 bytes, so the union sizes to maindata and
MINIMP3_SCRATCH_SIZE does not move.
metric before after
fixed decode stack 1,920 896 44% of budget, was 94%
float decode stack 2,032 1,200 59% of budget
fixed max frame 1,480 456
float max frame 1,208 824
scratch, working-ram, pipeline-peak unchanged
Two things guard the claim. A static_assert checks L12_scale_info still fits
inside maindata, so the union cannot start costing working RAM silently. That
covers the sizes; it says nothing about the half whose failure would be quiet,
which is whether the two members are really never live at once -- there the
sizes stay correct and only the audio goes wrong.
So the new test decodes Layer I and Layer III through one decoder, and so one
arena, in both orders, and requires each stream to come out bit-identical to
decoding it alone. Mutation-checked two ways. Calling L3_save_reservoir on the
Layer I/II path -- which would read maindata while it holds scale info --
segfaults. Leaving a single stale byte in the reservoir window fails exactly
one assertion in the whole codec suite, this test's, because a standalone
decode reads zero-initialised scratch there while an interleaved one reads
leftover Layer I/II data. That is the hazard the union introduces, and nothing
else in the suite sees it.
Verified: 288/288 unit, 1116 Python, conformance 69 passed / 0 failed with all
19 Layer I/II vectors at 104-122 dB, LEDGER:PASS.
Closes #4116
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ByD5ZKrnFJuchjRmPiQdd5
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughThe MP3 decoder now stores Layer I/II scale information in the existing scratch arena through a union with Layer III main data. Reservoir access was updated, stack metrics were revised, and a test verifies bit-identical decoding across shared Layer I and Layer III usage. ChangesMP3 scratch arena relocation
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: 🔵 Low · up to The change shares decoder scratch storage between Layer I/II and Layer III without increasing the arena size. Sequential decoding is protected and tested, but concurrent or reentrant use of the same scratch arena is not explicitly established; the owner should confirm that usage contract before merge. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The PR addresses issue Full details: Docstring CoverageExplanation Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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 |
Closes #4116.
The fixed-point decoder was using 1,480 bytes of stack in
mp3dec_decode_frame_ragainst a 2 KiB budget — 94% — and almost all of it was one local.L12_scale_infocarries mantissa plus exponent for 192 scalefactors in the fixed build, about 1090 bytes, and it sat on the stack. At 94% the figure moved with the compiler's inlining decisions rather than with anything in the decoder, which is not a margin worth defending.Moving it to the heap arena would normally cost working RAM, and there were only 692 bytes spare against the 24 KiB budget. Overlapping it with Layer III's
maindatacosts nothing instead: a frame is one layer or the other, never both, andmaindatais at least 1951 bytes, so the union sizes tomaindataandMINIMP3_SCRATCH_SIZEdoes not move.Guarding the claim
A
static_assertchecksL12_scale_infostill fits insidemaindata, so the union cannot start costing working RAM silently. That covers the sizes — and says nothing about the half whose failure would be quiet, which is whether the two members are really never live at once. There the sizes stay correct and only the audio goes wrong.So the new test decodes Layer I and Layer III through one decoder, and so one arena, in both orders, and requires each stream to come out bit-identical to decoding it alone. Mutation-checked two ways:
L3_save_reservoiron the Layer I/II path — which would readmaindatawhile it holds scale info — segfaults.That second one is the hazard the union introduces, and nothing else in the suite sees it.
Verification
LEDGER:PASSon the memory auditNote on the linter
cpp_lintingis currently failing onmasteritself — 11 files, none of them mp3 (asset, spi adapter, ble, flexio, teensy4 mocks, string_search_b). I confirmed the identical set on a cleanmastercheckout, so it is pre-existing and not from this branch. Flagging rather than folding an unrelated 11-file cleanup into a memory PR.🤖 Generated with Claude Code
https://claude.ai/code/session_01ByD5ZKrnFJuchjRmPiQdd5
Summary by CodeRabbit
Performance
Bug Fixes
Tests