Skip to content

perf: obs-audio-controls: SIMD volume meter peak and true peak #3005

Closed

Description

@zackees

What

rust/obs-audio-controls/src/peak.rs:16 (sample_peak) and :28 (true_peak) emulate the C's SSE lanes with plain [f32; 4] loops. The C, libobs/obs-audio-controls.c.h:303,349, uses SSE.

  • Runs: for every source with a volume meter, every audio tick.
  • Running today: the Rust, when the Rust ports are on.
  • Expected gain: medium. LLVM may already emit maxps for the max loop. The true-peak FIR shuffles are the likely win.
  • Parity: the C is SIMD, so a ULP tolerance is allowed. Keep the C's lane order to stay bit-exact if possible.

Acceptance criteria

  • First, check the generated assembly for both functions and post it on this issue. Hand-write std::arch (SSE/NEON) code only where the compiler didn't vectorize.
  • The volmeter parity tests pass on each path.
  • The volmeter benchmark (sub-issue 1) shows a gain.

Failing-test sketch

RED: a benchmark-bound or assembly check (for example, assert true_peak is ≤ C time on volmeter/true_peak/1024) that fails on the scalar code.

Rules for this work (#756): portable stable Rust only (no std::simd/nightly), a scalar fallback with runtime CPU detection, ISA-specific code only in the crate's src/platform/ (Dylint ban_platform_cfg_outside_boundary). Tier 3 parity is bit-exact unless the C reference itself uses SIMD (docs/rust-port/testing-policy.md:296). RED then GREEN. Every kernel needs a benchmark in rust/tools/benchmarks/record.py first (sub-issue 1 of the meta issue).

Activity

  1. added
    enhancement
    P2
    size:M
    on Oct 30, 2026
  2. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Triage: well defined (acceptance criteria and a failing-test sketch are in the body), but not Ready yet. It depends on #3003 (benchmark baseline in record.py), which is open. It becomes Ready once #3003 merges, and the baseline numbers decide whether this one is still worth doing.

  3. added
    P1
    and removed
    P2
    on Oct 30, 2026
  4. vpacher commented on Oct 30, 2026

    @vpacher
    Member

    Claiming: #3005, the sample-peak part first (7.2x slower than the C on x86 in #3029; true_peak is already equal or faster).

    Plan (one PR, RED then GREEN):

    1. The generated assembly of sample_peak and true_peak on x86_64 and aarch64, posted here.
    2. RED: a parity test with NaN samples against oracle_get_sample_peak (in the oracle since perf(bench): Record the SIMD candidates and repair the benchmark recorder (#3003) #3029). _mm_max_ps returns its second operand when either is NaN; f32::max returns the other one, so the two differ when a lane's last group holds a NaN. If they do not differ, the RED is the benchmark bound of the issue's sketch instead.
    3. GREEN: a scalar max with _mm_max_ps semantics in peak.rs (bit-exact with the C, no intrinsics, no src/platform/ code), and the volmeter numbers before and after from record.py --category volmeter on the machines of perf(bench): Record the SIMD candidates and repair the benchmark recorder (#3003) #3029.

    Draft PR within 48 h.

  5. added
    ready
    on Oct 30, 2026
  6. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Reproduced on current master descendant ed844f6 (Rust/benchmark source unchanged from #3029): uv run rust/tools/benchmarks/record.py --category volmeter --repeats 3 --output /tmp/obs3005-volmeter-benchmark/current.json.

    • volmeter/sample_peak/1024: Rust 667.73 ns, C 90.36 ns, Rust/C 7.39x (paired runs 6.64x–7.42x): regression reproduces.
    • volmeter/true_peak/1024: Rust 1841.90 ns, C 1936.25 ns, Rust/C 0.95x: remains within the 10% bound.

    The scalar lane loop is at rust/obs-audio-controls/src/peak.rs:18-27; C uses _mm_max_ps at libobs/obs-audio-controls.c.h:349-354. Leaving the active claim with @vpacher per collision policy.

  7. vpacher commented on Oct 30, 2026

    @vpacher
    Member

    Assembly check (acceptance criterion 1), release build of obs-audio-controls, -C codegen-units=1.

    Before (master 700e7a6), x86_64: sample_peak read samples[i + lane] against frames, so every lane kept its own bounds check (4 cmp/jae to panic_bounds_check per group), lanes 0 and 1 were maxss, lanes 2 and 3 one maxps on a movsd half. Not vectorized as a group.

    Two causes, two changes (PR to follow; thanks for the reproduction above, it matches ours):

    1. Parity: the lanes used f32::max, which returns the other operand when one is NaN; _mm_max_ps(a, b) returns b. With a NaN in a lane's last group the C drops that lane's peak (sample_peak: rust 0.5 c 0 for [0.5, 0, 0, 0, NaN, 0, 0, 0]; for true_peak the C can return NaN where the Rust returned a number). The lanes now use max_ps(a, b) = if a > b { a } else { b }, one lane of _mm_max_ps. hmax stays on f32::max, as the C's fmaxf.
    2. Bounds checks: the loops walk samples[..frames / 4 * 4].as_chunks::<4>(), the same whole groups as the C's i + 3 < nr_samples.

    After, x86_64 (sample_peak inner loop, unrolled twice by LLVM, no bounds check):

    movups  (%rsi,%rax,4), %xmm2
    movups  16(%rsi,%rax,4), %xmm3
    andps   %xmm1, %xmm2
    maxps   %xmm2, %xmm0
    andps   %xmm1, %xmm3
    maxps   %xmm3, %xmm0
    addq    $8, %rax
    cmpq    %rax, %rcx
    jne     .LBB29_6

    true_peak: 5 maxps per group, no bounds check. The remaining maxss in both are the final hmax, scalar in the C too.

    After, aarch64 (sample_peak inner loop, no bounds check):

    ldr      q1, [x1], #16
    fabs.4s  v1, v1
    fcmgt.4s v2, v0, v1
    bif.16b  v0, v1, v2
    subs     x8, x8, #16
    b.ne     LBB29_2

    NEON's fmax/fmaxnm have other NaN rules than _mm_max_ps, so the compare and select (fcmgt + bif) is the exact form; the C oracle runs the same through simde.

    record.py --category volmeter --repeats 3 (before = master 700e7a6):

    Machine, compiler of the C Kernel Rust before Rust after C Rust/C before → after
    Ryzen 9 5950X, Linux, GCC (--cpu 27) sample_peak/1024 463 ns 60 ns 65 ns 7.15 → 0.93
    true_peak/1024 1665 ns 859 ns 1727 ns 0.97 → 0.50
    Ryzen 9 5950X, Windows VM, MSVC 14.51 sample_peak/1024 481 ns 60 ns 130 ns 3.73 → 0.46
    true_peak/1024 1689 ns 852 ns 2031 ns 0.83 → 0.42
    Apple M1 Max, macOS, Apple clang sample_peak/1024 329 ns 249 ns 245 ns 1.33 → 1.01
    true_peak/1024 979 ns 1610 ns 1619 ns 0.61 → 0.99

    On aarch64, true_peak loses the lead it had: it came from f32::max being one fmaxnm, with the NaN rule that differed from the C. With the C's rule it does the same work as the C.

    No std::arch code was needed: the scalar Rust now compiles to the C's instructions on x86, so there is no src/platform/ path and no runtime detection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1enhancementreadysize:M

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions