You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf: obs-audio-controls: SIMD volume meter peak and true peak #3005
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).
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.
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):
The generated assembly of sample_peak and true_peak on x86_64 and aarch64, posted here.
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.
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/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.
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):
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.
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):
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.
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.
Activity
zackees commentedon Oct 30, 2026
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.vpacher commentedon Oct 30, 2026
Claiming: #3005, the sample-peak part first (7.2x slower than the C on x86 in #3029;
true_peakis already equal or faster).Plan (one PR, RED then GREEN):
sample_peakandtrue_peakon x86_64 and aarch64, posted here.oracle_get_sample_peak(in the oracle since perf(bench): Record the SIMD candidates and repair the benchmark recorder (#3003) #3029)._mm_max_psreturns its second operand when either is NaN;f32::maxreturns 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.maxwith_mm_max_pssemantics inpeak.rs(bit-exact with the C, no intrinsics, nosrc/platform/code), and thevolmeternumbers before and after fromrecord.py --category volmeteron the machines of perf(bench): Record the SIMD candidates and repair the benchmark recorder (#3003) #3029.Draft PR within 48 h.
zackees commentedon Oct 30, 2026
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_psatlibobs/obs-audio-controls.c.h:349-354. Leaving the active claim with @vpacher per collision policy.vpacher commentedon Oct 30, 2026
Assembly check (acceptance criterion 1), release build of
obs-audio-controls,-C codegen-units=1.Before (master 700e7a6), x86_64:
sample_peakreadsamples[i + lane]againstframes, so every lane kept its own bounds check (4cmp/jaetopanic_bounds_checkper group), lanes 0 and 1 weremaxss, lanes 2 and 3 onemaxpson amovsdhalf. Not vectorized as a group.Two causes, two changes (PR to follow; thanks for the reproduction above, it matches ours):
f32::max, which returns the other operand when one is NaN;_mm_max_ps(a, b)returnsb. With a NaN in a lane's last group the C drops that lane's peak (sample_peak: rust 0.5 c 0for[0.5, 0, 0, 0, NaN, 0, 0, 0]; fortrue_peakthe C can return NaN where the Rust returned a number). The lanes now usemax_ps(a, b) = if a > b { a } else { b }, one lane of_mm_max_ps.hmaxstays onf32::max, as the C'sfmaxf.samples[..frames / 4 * 4].as_chunks::<4>(), the same whole groups as the C'si + 3 < nr_samples.After, x86_64 (
sample_peakinner loop, unrolled twice by LLVM, no bounds check):true_peak: 5maxpsper group, no bounds check. The remainingmaxssin both are the finalhmax, scalar in the C too.After, aarch64 (
sample_peakinner loop, no bounds check):NEON's
fmax/fmaxnmhave 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):--cpu 27)sample_peak/1024true_peak/1024sample_peak/1024true_peak/1024sample_peak/1024true_peak/1024On aarch64,
true_peakloses the lead it had: it came fromf32::maxbeing onefmaxnm, with the NaN rule that differed from the C. With the C's rule it does the same work as the C.No
std::archcode was needed: the scalar Rust now compiles to the C's instructions on x86, so there is nosrc/platform/path and no runtime detection.obs-audio-controls: Take the peak as _mm_max_ps does and read whole g…
obs-audio-controls: AVX2-dispatch the sample_peak loop