Skip to content

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

Closed

Description

@zackees

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 .su files from ci/codec_memory/audit.py give per-function frames; the inlined contributors need attributing by hand or with -Rpass-analysis=inline).
  • Relocate the largest into mp3dec_scratch_t, adjusting MINIMP3_SCRATCH_SIZE and the static_assert on mp3dec_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: scratch and working-ram go up by the relocated amount, stack-callgraph and stack-max-frame come 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

Activity

  1. zackees commented on Sep 1, 2026

    @zackees
    MemberAuthor

    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:

      1480  mp3dec_decode_frame_r
       440  mp3d_synth_granule
       168  L3_imdct36
        48  L3_imdct12
        48  L3_dct3_9
        24  hdr_compare
    

    mp3dec_decode_frame_r declares only two locals of consequence, and one of them is nearly all of it:

    bs_t bs_frame[1];
    ...
    L12_scale_info sci[1];        // ~1090 bytes in the fixed-point build

    L12_scale_info is:

    member bytes
    int32_t scf_mant[3*64] 768
    int8_t scf_exp[3*64] 192
    total_bands, stereo_bands, bitalloc[64], scfcod[64] 130
    total ~1090

    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_info is 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:

    typedef struct {
        bs_t bs;
        mp3d_dsp_t grbuf[2][576];      /* both paths use these */
        union {
            struct {                    /* Layer III only */
                uint8_t maindata[...];
                L3_gr_info_t gr_info[4];
                int32_t scf_mant[40];
                int16_t scf_exp[40];
                uint8_t ist_pos[2][39];
            } l3;
            L12_scale_info l12;         /* Layer I/II only */
        } layer;
    } mp3dec_scratch_internal_t;

    bs and grbuf stay outside the union -- Layer I/II uses grbuf too, via L12_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 and MINIMP3_SCRATCH_SIZE is 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_info moves into a union arm of mp3dec_scratch_internal_t alongside the Layer III members; bs and grbuf stay shared.
    • MINIMP3_SCRATCH_SIZE and the ledger's scratch and working-ram rows are unchanged -- if they move, the union is wrong.
    • stack-callgraph and stack-max-frame drop, updated deliberately in the ledger.
    • Decoded PCM byte-identical across the conformance suite, fixed and float, scalar and SIMD. Layer I and Layer II vectors matter most here, since they are the ones that exercise the moved struct -- and the suite has 8 Layer I and 11 Layer II vectors with references, which the vendored corpus did not.

    Blocked only on #4128 landing, since both touch minimp3.h.

  2. moved this from Triage to Done in FastLED Trackeron Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions