Skip to content

perf(audio_mix): fuse the mul_div64 divisions and gate the recorder's 1.10 bound (#3138) - #3141

Merged
zackees merged 2 commits into
masterfrom
perf/3138-buffering-add-ratio
Oct 31, 2026
Merged

zackees merged 2 commits into
masterfrom
perf/3138-buffering-add-ratio

Conversation

@zackees

@zackees zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member

audio_mix/buffering_add_one_tick (the obs_audio_buffering_add() port) recorded 1.19–1.30x the C on this host, above the 1.10 bound. This PR removes two causes and makes the bound a check instead of a report row.

RED

test(bench): RED: require an over-bound gate in the recorder (#3138) — ci/test_benchmark_record.py::BoundGateTest asks for record.over_bound. It fails on that commit with AttributeError: module 'record' has no attribute 'over_bound'.

The perf half's RED is empirical, not a unit test (the box is shared and a Criterion run does not belong in PR CI): record.py --category audio_mix on master recorded the pair over the bound, three repeats apart, as the issue does.

GREEN

  1. One division instead of two, twice. mul_div64 took the quotient and the remainder through checked_div and checked_rem, each carrying its own 0 test, so LLVM could not fuse them into the single 64-bit div that the C's num / div + num % div compile to. An early 0 check plus wrapping_div/wrapping_rem restores the fusion (and keeps the documented 0-divisor result). ticks_to_ms gets the same shape.
  2. The deque push inlines. deque_push_front, with_deque and Deque::push_front/push_front_src were cross-crate calls, while the C's deque_push_front is header-inline. #[inline] on that chain folds the push into push_ticks (confirmed in the disassembly: no call to deque_push_front left in push_ticks).
  3. The recorder gates the bound. record.py --max-ratio R prints every pair above R and exits 1; dstr/printf_control is never a breach.

Measured

uv run rust/tools/benchmarks/record.py --cpu 7 --category audio_mix --max-ratio 1.10 --output …, five paired repeats, medians:

Rust C Rust/C paired run range
master 3e17f5d0d 47.79 ns 39.46 ns 1.211 1.166–1.214
this branch 45.87 ns 40.06 ns 1.145 1.106–1.177

The rest of the audio_mix category stays inside the bound (0.26–0.98), so this change does not trade one pair for another. The reports were produced by the recorder on the shared box under load average 10–35; the ratio is the signal, the absolute numbers are not.

Still above the bound. The residual is the FFI wrapper: obs_audio_buffering_add copies struct obs_audio_buffering (5 pointers) out and the state back, and keeps a call layer the C inlines (175 instructions against 102 plus a separate push_buffering_ticks). Closing the last ~0.04 needs the shim restructured to hold the deque and state for one call — a separate, riskier change. --max-ratio now fails the recording until then, so the breach cannot go quiet again.

Tiers

  • Tier 1: soldr cargo test -p obs-data --test obs-tests-core --release audio_mix — 46 passed, including obs_core::audio_mix_parity::buffering_matches (bit-exact vs the C).
  • Tier 3: covered by the same parity module plus the paired bench, which compares fixture output bit-for-bit before timing.
  • Tier 2/4: not applicable — no C test, header, protocol or user-visible behaviour changes.
  • ./lint: ok (146.6 s).

Test cost: three Python cases in ci/test_benchmark_record.py; the whole file runs in 0.4 s, single-threaded, no allocation or proptest.

Refs #3138 (does not close it: the pair is still over the bound).

`audio_mix/buffering_add_one_tick` records 1.19-1.30x the C on this host,
above the 1.10 bound, and the recorder reports it but exits 0, so nothing
turns the breach into a check. Add the three cases the gate must satisfy:
a pair over the bound is reported, one within the bound passes, and the
`dstr/printf_control` pair is never a breach.

Fails on this commit: `record.over_bound` does not exist.
…sh (#3138)

`audio_mix/buffering_add_one_tick` recorded 1.19-1.30x the C on this host,
above the 1.10 bound. Two things paid for it:

- `mul_div64` took the quotient and the remainder through `checked_div` and
  `checked_rem`, each with its own 0 test, so LLVM could not fuse them into
  the single 64-bit `div` the C's `num / div` + `num % div` compile to. One
  early 0 check plus `wrapping_div`/`wrapping_rem` restores the fusion;
  `ticks_to_ms` gets the same shape (its 0 rate case is the check).
- `deque_push_front` and the `Deque::push_front` chain were cross-crate calls
  the compiler could not inline, while the C's `deque_push_front` is header
  inline. `#[inline]` on the chain lets the push fold into `push_ticks`.

Measured with `uv run rust/tools/benchmarks/record.py --cpu 7 --category
audio_mix` on this host (Ryzen 7 3700X, shared box):

| | Rust | C | ratio |
|---|---:|---:|---:|
| before (master) | 47.79 ns | 39.46 ns | 1.211 |
| after | 45.87 ns | 40.06 ns | 1.145 |

Still above the bound: the remaining gap is the FFI wrapper's struct copies
and one extra call layer; the recorder now flags it (`--max-ratio`).

Refs #3138
@zackees
zackees marked this pull request as ready for review October 31, 2026 09:40
@zackees
zackees added this pull request to the merge queue Oct 31, 2026
Merged via the queue into master with commit 3527bb3 Oct 31, 2026
41 checks passed
@zackees
zackees deleted the perf/3138-buffering-add-ratio branch October 31, 2026 09:49
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 31, 2026
…tio (obs-rust#3138) (obs-rust#3153)

* test(bench): RED: exempt audio_mix/buffering_add_one_tick by name (obs-rust#3138)

obs-rust#3138 decided option 1: the pair is one extern "C" shim on a
once-per-audio-tick call at ~46 ns against the C's ~39 ns, with no hot spot
left after obs-rust#3141, so the Rust/C ratio bound does not apply to it (the 5%
full-app bound of obs-rust#142 still does).

The three cases the exemption must satisfy: the entry names the pair, its
measured cost, the machine and the issue; an exempt pair is not a breach;
and the pair stays in the report with its ratio.

Fails on this commit: record.BOUND_EXEMPTIONS does not exist and summarize
still marks the pair over the bound.

* perf(bench): exempt audio_mix/buffering_add_one_tick from the 1.10 ratio (obs-rust#3138)

obs-rust#3138 decided option 1: name the pair in record.py's BOUND_EXEMPTIONS
with its measured absolute cost, the machine and the issue, instead of
adding a push_ts_front fast path to the FFI layer.

- over_bound() skips an exempt pair by name, so --max-ratio no longer fails
  on it; every other pair keeps the gate.
- summarize() keeps the row and marks it exempt rather than over the bound,
  so the report still carries its ratio and absolute numbers.
- The report records the exemption list, and the gate prints each exempt
  pair above the bound with its reason.
- docs/rust-port/release-criteria.md documents --max-ratio and the list.

Tests: 19 in ci/test_benchmark_record.py pass (three RED cases plus the
gate print/exit paths).
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.

1 participant