Skip to content

perf(mp3): share Layer I/II scale info with the main-data window (#4116) - #4134

Merged
zackees merged 1 commit into
masterfrom
feat/4116-l12-union
Sep 1, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/4116-l12-union

Conversation

@zackees

@zackees zackees commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Closes #4116.

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 the saving is free

Guarding 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 — 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:

  • 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. A standalone decode reads zero-initialised scratch there; an interleaved one reads leftover Layer I/II data.

That second one is the hazard the union introduces, and nothing else in the suite sees it.

Verification

  • 288/288 unit tests, 1116 Python tests
  • Conformance 69 passed / 0 failed, with all 19 Layer I/II vectors at 104–122 dB
  • LEDGER:PASS on the memory audit

Note on the linter

cpp_linting is currently failing on master itself — 11 files, none of them mp3 (asset, spi adapter, ble, flexio, teensy4 mocks, string_search_b). I confirmed the identical set on a clean master checkout, 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

    • Reduced MP3 decoder stack usage, lowering memory pressure for both fixed-point and floating-point builds.
    • Moved temporary decoding data into shared scratch storage while preserving decoding behavior.
  • Bug Fixes

    • Prevented Layer I and Layer III decoding data from interfering when streams are decoded sequentially using the same decoder.
  • Tests

    • Added coverage confirming identical PCM output across decoding orders and isolated decoding scenarios.

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
@coderabbitai

coderabbitai Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 245819c1-2ac1-45c1-9f76-8b38782d94ad

📥 Commits

Reviewing files that changed from the base of the PR and between 0a25ad5 and 9382352.

📒 Files selected for processing (3)
  • codec_memory_ledger.md
  • src/third_party/minimp3/minimp3.h
  • tests/fl/codec/mp3.hpp

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The 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.

Changes

MP3 scratch arena relocation

Layer / File(s) Summary
Shared scratch storage and stack metrics
src/third_party/minimp3/minimp3.h, codec_memory_ledger.md
The scratch structure overlaps L12_scale_info with Layer III main data, validates the union size, updates reservoir access, and aliases Layer I/II scale information from scratch storage. The memory ledger records reduced stack metrics.
Shared scratch decoding validation
tests/fl/codec/mp3.hpp
The test decodes Layer I and Layer III streams independently and in both orders through one decoder, then compares frame layers and PCM samples.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: 🔵 Low · up to 93823

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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning 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… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the MP3 performance change: sharing Layer I/II scale information with the Layer III main-data window.
Linked Issues check ✅ Passed The PR addresses issue #4116 by moving L12_scale_info off the decode stack, reusing the existing scratch arena through a union, adding a size assertion, updating memory metrics, and testing Layer I/La…
Out of Scope Changes check ✅ Passed All changes are directly related to issue #4116. The decoder update, memory-ledger update, and collision regression test support the stated stack-usage optimization.
Full details: Linked Issues check

Explanation

The PR addresses issue #4116 by moving L12_scale_info off the decode stack, reusing the existing scratch arena through a union, adding a size assertion, updating memory metrics, and testing Layer I/Layer III reuse for bit-identical output. The reported stack reductions provide meaningful headroom while avoiding new working-memory allocation.

Full details: Docstring Coverage

Explanation

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.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/4116-l12-union

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 7c43414 into master Sep 1, 2026
35 of 37 checks passed
@zackees
zackees deleted the feat/4116-l12-union branch September 1, 2026 21:19
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

perf(mp3): move mp3dec_decode_frame_r's inlined locals into the heap scratch arena

1 participant