Repository navigation
Description
The margin is thin
The fixed-point decoder's deepest stack path is 1920 bytes against a 2048-byte budget — 94% of it. Float is 2032, at 99%.
mp3dec_decode_frame_r 1480 bytes (fixed) <- accumulated inlined locals
mp3d_synth_granule 440
L3_imdct36 152
everything else ~0
─────────────────────────────────
deepest path 1920 / 2048
Nothing here is a single large array. The top frame is big because -Os inlines several callees into mp3dec_decode_frame_r and their locals accumulate into one frame. That makes the figure a function of the compiler's inlining decisions, so a toolchain upgrade that inlines slightly differently can push it over a hard gate — and the gate is -Werror=frame-larger-than, so it fails the build rather than degrading gracefully.
Why this is the right shape of fix
The large blocks are already heap-allocated exactly once and reused for every frame — that was Phase 1 of FastLED/license#14:
| block | bytes | where |
|---|---|---|
decoder state (mp3dec_t) |
11,276 | heap, once |
scratch arena (mp3dec_scratch_t) |
7,936 | heap, once |
| stream buffer | 4,096 | heap, once |
| PCM output | 4,608 | heap, once |
Upstream's convenience entry point mp3dec_decode_frame() puts a 16.3 KB scratch buffer on the stack; FastLED does not expose it, and every caller uses mp3dec_decode_frame_r() with owned storage. PROVENANCE records that as a standing constraint.
So the arena already exists, is already owned by the caller, and is already accounted for in the memory ledger. Moving the largest of those inlined locals into it costs no new allocation and no new API — it just relocates bytes that are already budgeted, from the scarce resource to the plentiful one.
Acceptance criteria
- Identify which inlined callees contribute the bulk of
mp3dec_decode_frame_r's 1480/1208 bytes (the.sufiles fromci/codec_memory/audit.pygive per-function frames; the inlined contributors need attributing by hand or with-Rpass-analysis=inline). - Relocate the largest into
mp3dec_scratch_t, adjustingMINIMP3_SCRATCH_SIZEand thestatic_assertonmp3dec_scratch_internal_t. - Decoded PCM must stay bit-identical across the corpus, for fixed and float, scalar and SIMD. The gates for this already exist in
tests/fl/codec/mp3_fixed_point.hpp. - Memory ledger updated deliberately in the same PR:
scratchandworking-ramgo up by the relocated amount,stack-callgraphandstack-max-framecome down. Working RAM must stay under 24 KiB — there is currently 692 bytes of headroom against that budget (23,308 used), which bounds how much can move. - Target a stack figure with real headroom rather than one that merely passes today.
Note on the budget interaction
This trades stack for working RAM, and both are gated. Fixed-point currently sits at 23,308 of 24,576 bytes working RAM, so at most ~692 bytes can move before the other budget binds. If more than that needs to relocate, one of the two budgets has to be re-argued rather than quietly raised.
Found while investigating #4106, where the per-function stack breakdown made the 94% figure visible.
Refs #4106, FastLED/license#14
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Sep 1, 2026
Investigated: the 1480 bytes are almost entirely one struct, and it can cost zero RAM to move
Per-function stack frames, fixed-point scalar at
-Os:mp3dec_decode_frame_rdeclares only two locals of consequence, and one of them is nearly all of it:L12_scale_infois:int32_t scf_mant[3*64]int8_t scf_exp[3*64]total_bands,stereo_bands,bitalloc[64],scfcod[64]That also explains the fixed-vs-float gap exactly: float carries
float scf[3*64](768) where fixed carries mantissa plus exponent (960), and 1480 - 1208 = 272 is that difference plus alignment. The struct's own comment already says it is "the difference between meeting the 2 KiB decode-stack budget and missing it" -- so this was known, just not acted on.The union makes it free
I flagged when filing this that working RAM has only 692 bytes of headroom, so moving ~1090 bytes into the arena would breach the other budget. That is avoidable:
L12_scale_infois only used for Layer I and Layer II, and the Layer III scratch is only used for Layer III. A frame is one or the other, never both.So it does not need new arena space; it needs to share existing space:
bsandgrbufstay outside the union -- Layer I/II usesgrbuftoo, viaL12_dequantize_granule, so they are not exclusive. Everything else is. The Layer III arm is already far larger than 1090 bytes, so the union adds nothing andMINIMP3_SCRATCH_SIZEis unchanged.Expected outcome: decode stack 1920 -> roughly 830 bytes, against a 2048 budget, with working RAM unchanged at 23,308. That turns a 94%-of-budget figure into about 40%, and removes the compiler-inlining sensitivity that made it fragile.
Revised acceptance criteria
L12_scale_infomoves into a union arm ofmp3dec_scratch_internal_talongside the Layer III members;bsandgrbufstay shared.MINIMP3_SCRATCH_SIZEand the ledger'sscratchandworking-ramrows are unchanged -- if they move, the union is wrong.stack-callgraphandstack-max-framedrop, updated deliberately in the ledger.Blocked only on #4128 landing, since both touch
minimp3.h.test(mp3): make the hardware pass report what it actually decoded
perf(mp3): share Layer I/II scale info with the main-data window (#4116…
feat(mp3): verify the fixed-point decoder on hardware via AutoResearch
test(mp3): make the hardware pass report what it actually decoded
feat(mp3): verify the fixed-point decoder on hardware via AutoResearch
test(mp3): make the hardware pass report what it actually decoded
chore(mp3): record the speed-for-text-and-stack trade in the ledger
chore(mp3): record the speed-for-text-and-stack trade in the ledger
feat(mp3): verify the fixed-point decoder on hardware via AutoResearch (