Skip to content

obs-audio-controls: Take the peak as _mm_max_ps does and read whole groups (#3005) - #3055

Merged
zackees merged 3 commits into
masterfrom
perf/3005-sample-peak
Oct 30, 2026
Merged

zackees merged 3 commits into
masterfrom
perf/3005-sample-peak

Conversation

@vpacher

@vpacher vpacher commented Oct 30, 2026

Copy link
Copy Markdown
Member

Fixes #3005

What and why

The volume meter's peak kernels (rust/obs-audio-controls/src/peak.rs) were 7.2 times slower than the SSE C for sample_peak on x86 (#3029; reproduced independently in #3005 (comment) at 7.39x), and they did not match the C when a sample is NaN.

  1. Parity (bug): the lanes took their maximum with 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 (the final fmaxf then skips the NaN); the Rust kept it. 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. Speed: the loops read samples[i + lane] against frames, so each read kept its own bounds check and the lanes were loaded one by one. They now walk samples[..frames / 4 * 4].as_chunks::<4>(): the same whole groups as the C's i + 3 < nr_samples, as [f32; 4] arrays, and a panic in exactly the cases the old indexing panicked.

No std::arch code: the scalar Rust now compiles to the C's instructions on x86 (movups, andps, maxps per group, no bounds check) and to a compare-and-select (fcmgt.4s + bif.16b) on aarch64, where NEON's own fmax/fmaxnm have other NaN rules. So there is no src/platform/ path and no runtime detection. The assembly is on #3005 (acceptance criterion 1).

Numbers

record.py --category volmeter --repeats 3, before = master 700e7a6, after = this branch:

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

x86, by step: the parity change alone took sample_peak to 236 ns (3.65) and true_peak to 886 ns (0.52); the slice change took sample_peak the rest of the way.

On aarch64 true_peak loses the lead it had (0.61 → 0.99): it came from f32::max being one fmaxnm, with the NaN rule that differed from the C. With the C's rule the Rust does the same work as the C (which runs _mm_max_ps through simde there).

Tests

The oracle exports get_sample_peak()/get_true_peak() since #3029, so the kernels get Tier 3 tests of their own in rust/obs-audio-controls/tests/audio_controls/parity.rs, compared bit for bit: a hand case (nan_in_the_last_group_matches_c) and a property test (random_peaks_match_c, NaN, infinities and -0.0, parity::cases(2000)). The test header no longer says the peak path is cmocka-only. proptest-regressions/parity.txt keeps the case proptest found on RED (previous = [0, 0, 0, 0], samples = [NaN, 0, 0, 0]: for true_peak the C returns NaN).

RED → GREEN

  • RED: 917c9f6. Fails against the simde oracle (macOS) and the native SSE oracle (x86_64 Linux): sample_peak: rust 0.5 c 0 for [0, 0, 0, 0] [0.5, 0, 0, 0, NaN, 0, 0, 0]; on x86 the property test also finds true_peak: rust inf c NaN.
  • GREEN: 454ef45 (max_ps), then 02d0987 (one slice, as_chunks). audio_controls 24/24 on macOS, on x86_64 Linux and against the MSVC oracle on Windows.
  • Local lanes on the Linux gate host, final head: rust-test passed (5250 + 857 tests), cross passed (Windows and macOS clippy and Dylint), tier2-linux passed (629/629 against the C reference and the hybrid libobs, which carries peak.rs). lint passes except ci/test_analyze_project.py, which fails there on master since ci: port the macOS analysis steps to ci/analyze_project.py, clearing the analyze-project GEN-005 block (#3040) #3043 (jq 1.7, ci: test_analyze_project.py's reference jq program fails with jq 1.7 (Debian 13, macOS) #3051); so the head is not stamped and PR CI runs the full suite.
  • Real app: N/A in this PR. The volume meter runs per metered source per tick; the gain shows on our production machine at the next rebuild A/B.
  • Test cost: two new Tier 3 cases, under 0.1 s together; no own threads.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: not applicable (same meter values, except the NaN cases, which now match the C)
  • (Optional) Local gate passed and the pushed head is stamped: not stamped (ci: test_analyze_project.py's reference jq program fails with jq 1.7 (Debian 13, macOS) #3051)
  • Warning or flag changes: not applicable
  • Intentional differences from C: none; this removes one

🤖 Generated with Claude Code

vpacher and others added 3 commits October 30, 2026 12:07
…samples (#3005)

The oracle exports get_sample_peak() and get_true_peak() since #3003, so
the peak kernels get their own Tier 3 tests, compared bit for bit:
a hand case and a property test with NaN, infinities and -0.0.

_mm_max_ps returns its second operand when either is NaN; the Rust lanes
use f32::max, which returns the other one. With a NaN in a lane's last
group the C drops that lane's earlier peak (the final fmaxf skips the
NaN), the Rust keeps it:

  sample_peak: rust 0.5 c 0 for [0, 0, 0, 0] [0.5, 0, 0, 0, NaN, 0, 0, 0]

Both new tests fail on the current peak.rs (macOS, simde oracle).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#3005)

The three lane maxima of sample_peak() and true_peak() use max_ps(a, b),
`a > b ? a : b`, which is one lane of _mm_max_ps: b when either is NaN.
f32::max returned the other operand and so kept a peak the C drops. The
final hmax stays on f32::max, as the C's fmaxf.

The comparison form is also what compilers turn into maxps; the numbers
are in the PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sample_peak() and true_peak() indexed samples[i + lane] against
`frames`, so every read kept its own bounds check and the compiler
loaded the four lanes one by one. They now walk
samples[..frames / 4 * 4].as_chunks::<4>(): the same whole groups the
C's `i + 3 < nr_samples` reads, as [f32; 4] arrays, and a panic in
exactly the cases the old indexing panicked. The numbers are in the PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher marked this pull request as ready for review October 30, 2026 11:17
@vpacher

vpacher commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

Ready for review. CI is green (CI Gate ✅). The head is not stamped (the local lint lane fails on master since #3043, #3051), so PR CI ran the full Linux suite: Fast Gate and every Tier 2 shard passed. Locally on the final head: rust-test, cross and tier2-linux (629/629 both modes) passed; the MSVC and macOS numbers are in the description.

Auto-merge is off on purpose: please comment before it merges. The assembly is on #3005 (#3005 (comment)).

@vpacher
vpacher requested a review from zackees as a code owner October 30, 2026 11:17

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: LGTM.

  • NaN parity: max_ps(a, b) = a > b ? a : b is one lane of _mm_max_ps(a, b) (it returns b on NaN). Every call keeps the C's argument order (peak first, the new value second, as obs-audio-controls.c.h:321-354 does), and hmax stays on f32::max, matching fmaxf. The proptest regression (samples = [NaN, 0, 0, 0], where the C's true_peak returns NaN) is kept.
  • Panics unchanged: the old loop panicked exactly when a whole group reached past samples.len(), that is when frames / 4 * 4 > len. samples[..frames / 4 * 4] panics in exactly that case. as_chunks is stable on the pinned toolchain.
  • Tiers: new bit-exact Tier 3 cases against the oracle (a hand case plus random_peaks_match_c with NaN, infinities and -0.0) on SSE, simde and MSVC; Tier 2 629/629 against the hybrid libobs. No intrinsics, so no src/platform/ path is needed.
  • Numbers: x86 sample_peak goes 7.15 → 0.93 (GCC) and 3.73 → 0.46 (MSVC). true_peak on aarch64 goes 0.61 → 0.99, but the old lead came from the wrong NaN rule, so parity wins. That is the right trade under testing-policy.md:296.

Queuing. Afterwards #3005 can close and #3009 can mark it done.

@zackees
zackees added this pull request to the merge queue Oct 30, 2026
Merged via the queue into master with commit 0f0dbe9 Oct 30, 2026
41 checks passed
@zackees
zackees deleted the perf/3005-sample-peak branch October 30, 2026 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

2 participants