Skip to content

perf(mp3): add a fast fixed-point mode at Helix parity, keeping today's decoder as HQ #4139

Closed as not planned

Description

@zackees

Follow-on from #4138, which measured that minimp3-fixed — the build that ships on every embedded target — is ~1.59x slower than the Helix decoder it replaced, while being 20.4 dB more accurate than it.

We are paying for quality nobody asked for, on the parts least able to afford it.

Proposal: two modes

mode target accuracy target speed who
HQ (today's decoder, unchanged default where affordable) 123.24 dB 1.59x Helix hosts, ESP32-S3/P4, anything with SIMD or headroom
Fast (new) ≥102.87 dB (Helix parity) ≈1.0x Helix ESP32-C6/C3/H2 and other non-SIMD 32-bit parts

The accuracy budget is the whole point of the split:

123.24 dB   minimp3-fixed today (HQ)
102.87 dB   Helix — the parity bar
 60.00 dB   ISO limited-accuracy floor

There is 20.4 dB of headroom between what we ship and what Helix managed, and 63 dB above the ISO floor. Fast mode spends part of the first number and must not touch the second.

Why this matters on the C6

bash autoresearch esp32c6 --mp3 on an ESP32-C6 at 160 MHz:

decode 155810 us for 235101 us of audio -> 1.51x real time

One stereo 44.1 kHz stream consumes two thirds of a core. At Helix parity that becomes roughly 2.4x, which is the difference between "MP3 plus a demanding LED driver" working and not.

Known-dead path: do not re-run this

A naive 16-bit MAC has already been measured (#4108) and it fails outright:

variant PSNR
32-bit MACs (ships today) 123.24 dB
16x16 MACs simulated 44.37 dB
Helix 102.87 dB
ISO floor 60 dB

Keeping only the top 16 bits of the wide operand in mp3d_mulshift (DCT-32) and mp3d_synth_pair (polyphase) costs ~79 dB — 15 dB below the ISO floor, not merely below Helix. The coefficients there are already 16/17-bit (29, 213, 459, 2037, 5153, 6574, 37489, 75038, 64019), so the Q26 samples are the wide side and truncating them is what destroys it.

So a blanket fp<8,8> / Q8.8 across the polyphase is not viable. Anything in that direction needs to be measured against l3-hecommon.bit before it is built on.

Directions that look better

1. Copy what Helix actually does. Helix reaches 102.87 dB using MULSHIFT32 — keep the high 32 bits of a 32x32 product — alongside MADD64 where it needs the width. minimp3-fixed instead accumulates in int64 throughout and saturates via mp3d_sat64. Trading full 64-bit accumulation for a high-half multiply in the polyphase is the single most direct route to Helix parity, and Helix is the existence proof that it lands above 100 dB.

2. Spend the headroom selectively, not uniformly. The stage timings say the cost is not evenly spread. Per-frame host medians: synthesis 20,598 ns, huffman 7,373 ns (float) vs 12,134 ns (fixed), imdct 3,440 ns. Synthesis dominates and is the right place to trade precision; huffman is integer work in both builds and its fixed-build regression is unexplained (#4138) — that is a bug to find, not precision to spend.

3. Reduced intermediate width, not reduced operand width. 24-bit or Q1.30 intermediates in the polyphase keep the wide operand intact while shrinking the accumulator, which is a different trade from the one already measured to fail.

4. fl::fixed_point templating. If the DSP is parameterised on a fixed-point type, HQ and Fast become instantiations rather than a fork, and the conformance suite can run both. This was floated earlier in the MP3 work and is the cleanest structure if the arithmetic change is otherwise viable.

Acceptance

  • Fast mode scores ≥102.87 dB on l3-hecommon.bit (Helix parity) and clears the 60 dB ISO floor on all 83 conformance vectors, fixed and float
  • Fast mode is ≤1.1x Helix in host cycles on the ci/codec_cpu/driver.cpp corpus
  • HQ mode is bit-identical to today's decoder — no accidental regression of the default
  • On-device: bash autoresearch esp32c6 --mp3 reports ≥2.0x real time in Fast mode, still bit-exact against a host-generated fixture for that mode
  • Both modes carry host baselines in codec_cpu_trend.json (today minimp3-fixed has none — see perf(mp3): fixed-point minimp3 is ~1.6x slower than the Helix it replaced, and nothing gates it #4138)

Notes

  • Mode selection should follow the existing pattern: decided in minimp3.h, not by a per-TU build flag. MINIMP3_SCRATCH_SIZE depends on the DSP choice, and a flag passed to one translation unit and forgotten in another fails as a malloc assertion far from MP3.
  • Measure on a 32-bit target, not just the host. fix(mp3): signed overflow (UB) in fixed-point mp3d_synth_pair on malformed input #4133 cost +78% instructions on armv7m/riscv32/i386 and exactly 0% on x86-64 — host ratios can be blind to the thing being optimised.
  • Anything general here should go upstream to lieff/minimp3; the fixed-point port is a FastLED fork.

Background, method and recovered Helix baselines: agents/docs/mp3-decoder-performance.md and #4138.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByD5ZKrnFJuchjRmPiQdd5

Activity

  1. zackees commented on Sep 1, 2026

    @zackees
    MemberAuthor

    The gap is ~18%, not ~59% — acceptance criteria need revising

    See #4138's correction. The measurement this issue was written against was taken on a machine under load average 12. Clean re-measurement, three consecutive trials with pinned affinity and 30-run medians:

    backend vs Helix (audit flags)
    minimp3-float 0.38x
    minimp3-fixed 1.18x
    helix 1.00x

    Production flags land somewhere around 1.2–1.45x and are still too noisy on this host to pin down.

    What this changes

    The premise stands — the shipping fixed-point build is slower than the decoder it replaced, and it does carry 20.4 dB of accuracy nobody asked for. A Fast mode is still worth having.

    But the size of the prize is much smaller than stated. Closing ~18% is a different engineering proposition from closing ~59%, and it changes the cost/benefit of every option listed:

    • Trading precision for speed looks much less attractive now. Giving up meaningful accuracy to recover 18% is a poor trade when we hold 63 dB of margin above the ISO floor but only 18% of runtime is at stake.
    • The huffman anomaly is now the most interesting lead by a wide margin — 12,134 ns/frame fixed against 7,373 float, for integer work that is identical in both builds. That is potentially most of the remaining gap and it is a bug, not a precision trade. Fixing it costs no accuracy at all.
    • Anything requiring a second DSP path (HQ/Fast split, fl::fixed_point templating) should probably wait until the huffman regression is understood, in case it closes the gap on its own.

    Revised acceptance

    Superseding the criteria in the body:

    • Understand and fix the huffman regression first; re-measure before committing to a Fast mode at all
    • If a Fast mode is still needed: ≤1.05x Helix in host cycles, and ≥102.87 dB on l3-hecommon.bit
    • HQ mode bit-identical to today's decoder
    • Both modes carry host baselines in codec_cpu_trend.json
    • Re-measure on a genuinely idle machine before accepting any result — this issue exists partly because that was not done

    The known-dead 16-bit path (44.37 dB, 15 dB below the ISO floor) is unaffected by the correction and still should not be re-attempted.

  2. zackees commented on Sep 1, 2026

    @zackees
    MemberAuthor

    Callgrind says the gap is one line of C

    Switched from cycles to Callgrind instruction counts, on the reasoning that this issue has now had its premise corrected twice off noisy cycle data. Instruction counts are deterministic: the Helix figure measured today lands 0.18% from the baseline recorded a year ago on different hardware.

    Scalar (-DMINIMP3_NO_SIMD), -O2 with inlining, 892 frames:

    build instructions vs Helix
    minimp3-float 202,832,660 0.91x
    minimp3-fixed 353,592,466 1.59x
    helix 222,939,814 1.00x

    Correcting the correction

    My earlier "~18%" was measured at -O1 -fno-inline. That is a real number for a configuration nobody ships. The same comparison:

    config metric fixed vs Helix
    -O1 -fno-inline cycles 1.18x
    -O1 -fno-inline instructions 1.42x
    -O2 inlined instructions 1.59x

    Helix gains more from inlining than minimp3 does (2.07x vs 1.86x reduction), so the gap widens with optimisation. The audit's -fno-inline flags understate it. Quote the -O2 scalar figure.

    So the original 1.59x was right by accident — from a loaded machine with SIMD on — and my correction to 1.18x was right about the method but measured the wrong build.

    Where it is

    minimp3-fixed helix
    mp3d_synth_granule 201,839,372 (57.1%) PolyphaseStereo (asm+hpp) 66,139,380 (29.7%)
    ...one line inside it 86,547,296 (24.5%) ClipToShort + clip helper ~26M
    mp3dec_decode_frame_r 57,394,378 (16.2%) IMDCT 30,288,816 (13.6%)
    L3_imdct36 43,809,800 (12.4%) FDCT32 26,169,120 (11.7%)

    Total excess is 130.7M. Synthesis alone differs by 135.7M — synthesis is the gap; everything else roughly cancels.

    And inside synthesis:

    if (value > MP3D_SAT_MAX) return MP3D_SAT_MAX;   // 86,547,296 instructions -- 24.5% of the decode

    mp3d_sat64 saturates every intermediate in the polyphase. Helix does not: it truncates via MULSHIFT32 in the inner loop and clips once, at output, in ClipToShort. That is a design difference, not a micro-optimisation, and it is roughly two thirds of the excess.

    What this changes for this issue

    The HQ/Fast split may not be needed. This is not a precision-vs-speed trade — it is a redundant clamp on every intermediate. If synthesis can accumulate without per-step saturation and clip once at output, the gap closes at no accuracy cost at all, and both the 123.24 dB and the speed are kept.

    Revised first task:

    • Establish whether the polyphase accumulator can drop per-intermediate saturation. The accumulator is already int64 and the coefficients are 16/17-bit, so the true sum may not need clamping until it narrows to output — the same argument fix(mp3): signed overflow (UB) in fixed-point mp3d_synth_pair on malformed input #4133 used for widening rather than saturating there.
    • If so, clip once at output and re-measure. Target is the 130.7M excess, of which ~86.5M is this line.
    • Only if that fails, revisit a Fast mode.

    Do not confuse this with #4127

    #4127 fixed the clamp's bounds — it was pinning to ±1.0 instead of the int32 range — and was worth 81 dB. The cost here is evaluating the clamp per intermediate at all. Any change must keep #4127's bounds and the 83-vector ISO suite green; that suite exists precisely to catch a regression there.

    Method and full attribution: agents/docs/mp3-decoder-performance.md.

  3. zackees commented on Sep 1, 2026

    @zackees
    MemberAuthor

    Clarification on the ratio direction. The tables above report backend / helix in instructions executed, so higher is worse. 1.59x means minimp3-fixed does 59% more work than Helix — it is the slower one. Written as a bare multiplier that reads naturally as "1.59x faster", which is the opposite of what it says.

    helix           222,939,814   <- fewest instructions = fastest
    minimp3-fixed   353,592,466   <- 130,652,652 MORE  -> 59% slower
    minimp3-float   202,832,660   <-  20,107,154 fewer ->  9% faster
    

    The audit now prints the direction with the number (sense=slower:pct=59) so this cannot be misread from CI output.

  4. zackees commented on Sep 2, 2026

    @zackees
    MemberAuthor

    On-device: the gap is 4x, not 1.59x

    Measured on an ESP32-C6 at 160 MHz, both decoders compiled into one firmware and timed in the same run with no reflash between them:

    Layer III only -- minimp3 142647 us, helix 35742 us
      ->  minimp3-fixed is 3.99x helix (299% slower)  [both decoded 8 frames]
    
    host x86-64 ESP32-C6 (riscv32)
    minimp3-fixed vs Helix 1.59x 3.99x

    The host understated it by two and a half times. Nothing in the x86 profile hinted at 4x, which is the whole argument for having put Helix back in the tree.

    Likely cause is what #4133 already demonstrated: minimp3-fixed's arithmetic is dense with int64 — mp3d_sat64, the polyphase accumulators, mp3d_mulshift — and an int64 operation is a register pair plus a carry chain on a 32-bit core while being free on x86-64. Helix was written for 32-bit MCUs and uses MULSHIFT32 (32x32 keeping the high half) throughout.

    Two wrong answers preceded this one, both from timing something without checking it did the work. The first run said 4.32x — it was timing minimp3 over 11 frames against Helix over 8, because Helix does not decode Layer I. The comparison is now Layer III only on both sides, and the runner suppresses the ratio outright if frame and sample counts differ. Residual bias is quantified: minimp3's leg computes the fixture checksum and Helix's does not, ~0.3%, biasing against minimp3.

    The target, restated

    Saturation analysis from the previous comment still holds and is now much more valuable:

    • mp3d_sat64 is 24.5% of the decode on x86-64 and 32.4% of mp3d_DCT_II statically on riscv32
    • across the 83 ISO vectors it clamps 4,609 times out of 154,115,265 calls
    • every one of those comes from a single malformed vector, l3-nonstandard-big-iscf
    • on real music: 82,537,927 calls, zero clamps

    So on conformant input, saturating and not saturating produce identical output. The trade is malformed-input robustness against a third of the DCT-32 — not accuracy against speed.

    Revised acceptance

    Superseding both earlier sets:

    • Measure on the C6, not the host. Host ratios were wrong by 2.5x here. bash autoresearch esp32c6 --mp3 reports the on-device ratio directly now.
    • Target ≤1.5x Helix on device as a first milestone (from 3.99x), ≤1.1x as the goal
    • Keep 123.24 dB on l3-hecommon.bit — the 20.4 dB advantage over Helix is the reason to prefer minimp3 and must not be spent
    • All 83 ISO vectors stay above the 60 dB floor, fixed and float
    • l3-nonstandard-big-iscf stays bounded — no wrap to a sign-flipped full-scale sample
    • fix(mp3): fixed-point dequant clamps samples to 1.0, losing 81 dB on l3-si_huff #4127's clamp bounds unchanged

    Order of work

    1. Bound the DCT's input at dequantisation rather than clamping every intermediate. The failing vector is "big intensity-stereo scalefactors" — the cause is upstream of the butterflies. If the input is bounded, the network cannot overflow and needs no internal clamp.
    2. Failing that, clamp once per butterfly stage rather than per operation. Growth is at most 2x per stage.
    3. Attack int64 use on 32-bit paths generally — mp3d_mulshift and the polyphase accumulators are the other half of the arithmetic.
    4. Not a branchless 32-bit clamp: already tried and reverted at +24% text for ~3% time.
    5. Not a 16-bit MAC: measured at 44.37 dB, 15 dB below the ISO floor (perf(mp3): ESP32-S3 PIE kernels for the fixed-point polyphase and DCT-32 #4108).

    An HQ/Fast split now looks unnecessary. If the clamp is redundant on conformant input, removing it costs no accuracy at all, and there is no trade to expose as a mode.

  5. zackees commented on Sep 5, 2026

    @zackees
    MemberAuthor

    Closing as superseded. The comment trail on this issue retired its own premise: the fixed-vs-Helix gap turned out to be per-intermediate saturation cost, not a precision trade, so there is nothing to expose as a Fast mode. The work then landed without a mode split.

    Against the final revised acceptance list:

    The only unmet item is the 1.1x stretch goal. That is ordinary optimisation work in minimp3_synth_fixed.h, not a feature request, and does not need this issue open. Leftover housekeeping from this thread and #4138 is collected in #4155.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01XQyTuAVs4tK9pDcxAZVqVe

  6. moved this from Triage to Done in FastLED Trackeron Sep 5, 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