Repository navigation
Description
What
audio_mix/buffering_add_one_tick (the obs_audio_buffering_add() port, rust/obs-core/src/audio_mix.rs) is 1.19-1.30x the C's time on this host, above the project's 1.10x slowdown bound (#1295). Every other audio_mix bench is at or below it.
It is not a large cost in absolute terms: about 51 ns against 42 ns, once per audio tick. It matters because the bound is a rule, and because a regression hidden behind a small absolute number is what the bound exists to catch. The maintainer may decide the bound does not apply to a sub-100 ns, once-per-tick call; that decision is the point of this issue.
- Running today: Rust (
obs_audio_buffering_addinrust/obs-core/src/audio_mix.rs, FFI shimrust/obs-core/src/ffi/audio_mix.rs). - Parity: the C is scalar and deterministic, so the port is bit-exact (
rust/obs-core/tests/obs_core/audio_mix_parity.rspasses). - Expected gain: none at stake; this is closing a bound breach, not a speedup.
Measured
soldr cargo bench -p obs-c-oracle --bench port_comparison -- audio_mix, origin/master 3e17f5d0d, 2026-10-31, Ryzen 7 3700X, load average 25-38 (the box runs four worker slots). Three separate runs of the one bench, Rust and C medians:
| Run | Rust | C | Rust/C |
|---|---|---|---|
| 1 | 51.383 ns | 42.264 ns | 1.22 |
| 2 | 54.272 ns | 45.451 ns | 1.19 |
| 3 | 71.731 ns | 55.389 ns | 1.30 |
Both columns move together with the load, so the ratio is the signal. The full audio_mix sweep in the same session agrees: audio_mix/buffering_add_one_tick 51.383 ns vs 42.264 ns, while mix_whole_tick, scene_mix_*, source_volume_* and output_tick are all at or below the bound.
Lead, not a diagnosis
The bench calls the FFI shim, which takes push_front as impl FnMut; through #[no_mangle] extern "C" that becomes a trampoline that calls the C callback for every pushed window (push_ticks, rust/obs-core/src/audio_mix.rs). One window is pushed per tick, and the bench also runs rust_q.reset()/c_q.reset() inside the timed closure on both sides, so the difference is one trampoline plus the closure indirection per call. Nothing here was profiled.
Failing-test sketch
RED: a ratio check with the record.py/port_comparison shape that fails when audio_mix/buffering_add_one_tick Rust/C > 1.10 over N repeats on one named machine (or, if the maintainer decides the bound does not apply, a recorded exemption naming the absolute cost and the reason).
Rules for this work (#756)
Portable stable Rust only (no std::simd/nightly); ISA-specific code only in the crate's src/platform/ (Dylint ban_platform_cfg_outside_boundary); RED then GREEN; the parity test must stay bit-exact. If the fix is a measurement artefact (for example the bench reset belonging outside the timed closure), say so in the PR and fix the bench instead.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
zackees commentedon Oct 31, 2026
Claiming: #3138
port_comparisonrecord so a name can carry a ratio bound, and add one foraudio_mix/buffering_add_one_tick(Rust/C <= 1.10 over repeated runs); confirm it fails on current master at ~1.2x.audio_mix_parity.rsbit-exact../lint+ the Tier 1 audio tests, then one PR against master with the measured before/after ratio.zackees commentedon Oct 31, 2026
Worker tick progress (2026-10-31 ~09:45Z,
obs-studio5).Opened #3141 (draft) with the first half:
mul_div64read the quotient and the remainder throughchecked_div/checked_rem, each with its own 0 test, so LLVM could not fuse them into the single 64-bitdivthe C'snum / div+num % divcompile to — two divisions per call where the C has one, twice perbuffering_add. A single early 0 check withwrapping_div/wrapping_remrestores the fusion. The deque push chain also lacked#[inline], so where the C'sdeque_push_frontis header-inline the Rust call stayed a real call; it now folds intopush_ticks.record.py --cpu 7 --category audio_mix, five paired repeats: 47.79/39.46 ns = 1.211 on master, 45.87/40.06 ns = 1.145 on the branch (range 1.106–1.177). Still over the bound, so perf(audio_mix): fuse the mul_div64 divisions and gate the recorder's 1.10 bound (#3138) #3141 saysRefs, notFixes.record.py --max-ratio 1.10exits 1 and names the pair, so this breach cannot go quiet again while the last few percent is worked.What is left: the FFI wrapper copies
struct obs_audio_bufferingout and the state back, and keeps one call layer the C inlines (175 instructions vs 102 plus a separatepush_buffering_ticks). The next step is to restructure the shim so one call holds the deque and the buffering state, measured the same way. Parity stays bit-exact (obs_core::audio_mix_parity::buffering_matchesand 45 more audio_mix tests pass).zackees commentedon Oct 31, 2026
Worker tick progress (2026-10-31,
obs-studio4) — the residual after #3141 is not a hot spot; it needs a decision.Where it stands on master
3527bb3fb(the #3141 merge) fixed the two real inefficiencies (fusedmul_div64divisions,#[inline]on the deque chain) and moved the pair from 1.211 to ~1.145. Re-measured on this checkout today after rebasing onto that merge:3527bb3fb,record.py --cpu 13 --category audio_mix --repeats 3#[inline]onbuffering_add/push_ticks/buffering_ts_minus/frames_to_ns/ns_to_frames/ticks_to_ms, same method#[inline(always)]onbuffering_addandpush_ticksThe
#[inline]/#[inline(always)]variants are inside the paired-run noise (the C column itself moves 38.7–39.7 ns on this box under load) and the forced-inline one grew the entry by ~120 instructions, so I reverted that experiment and pushed nothing; the branchperf/3138-buffering-add-inlineis deleted. No part of that is a fix worth a PR.Why the last ~5 ns is not where you'd expect
Both sides were disassembled from the bench binary (
port_comparison, release/profile as the recorder builds it):obs_audio_buffering_add: 174 instructions, one call (push_ticks).oracle_obs_audio_buffering_add: 113 instructions, and it makes two calls —oracle_obs_audio_buffering_maxedat the top, thenpush_buffering_ticks.So it is not "an extra call layer": the Rust side makes fewer calls than the C wrapper. The differences that remain are all boundary shape, not per-tick arithmetic:
read()null-checks all four field pointers (and returns the default step) before touching state — ~10 instructions the C does not have, and not removable without weakening the soundness contract;Bufferingand written back, where the C mutatesobs_core_audiofields through the field pointers in place;ts_infopush still reachesmemcpy@pltthrough the obs-utilDequecore, while the C's header-inlinedeque_push_frontinlines the 16-byte copy intopush_buffering_ticks.Both
push_buffering_ticksandpush_ticksare out-of-line helpers on their side, so there is no single call to delete. Closing the last 0.06 needs either a 16-byte push fast path that bypasses the genericDequecore (a second copy ofdeque_push_front's inline logic living in obs-core's FFI module — a boundary smell the review would have to accept) or a decision that the bound does not apply here.Decision needed
The issue says the point is whether a 1.10x bound applies to a sub-100 ns call that runs once per audio tick. After #3141 the real inefficiencies are gone; what is left is the Rust/C boundary itself, so:
--max-ratiogate with a named exemption foraudio_mix/buffering_add_one_tickthat states the absolute cost (~45 ns vs ~39 ns, once per tick, ~0.0002% of one 10 ms tick) so the gate keeps its teeth for pairs where the bound is meaningful, orpush_ts_frontinrust/obs-core/src/ffi/audio_mix.rsimplementing the 16-bytedeque_push_frontpath directly (capacity check + two 8-byte stores,memcpyonly on wrap), measured with the same recorder.Either way the parity test stays bit-exact and
#3141's gate stays. My recommendation is (1): the bound was written for kernels, and this pair is a per-tick FFI call whose C side is a header-inline function. I will not build (2) onto the branch unless that is the call, because it duplicates deque semantics across the boundary.Claim is being held per the 7-day decision rule; nothing else is blocked on this.
zackees commentedon Oct 31, 2026
Decision needed: does the #1295 Rust/C 1.10x bound apply to
audio_mix/buffering_add_one_tick, given #3141 removed the real inefficiencies and what is left (~45 ns vs ~39 ns, once per audio tick) is the Rust/C boundary itself?Two options, both keeping
audio_mix_paritybit-exact and #3141's--max-ratiogate:push_ts_frontfast path inrust/obs-core/src/ffi/audio_mix.rsthat duplicatesdeque_push_front's inline logic to drop thememcpy@plt/genericDequecore per push.Full measurements and the disassembly comparison (Rust entry 174 instructions, C wrapper 113 with two calls) are in the comment above.
zackees commentedon Oct 31, 2026
Decided: option 1. Exempt
audio_mix/buffering_add_one_tickfrom the 1.10 ratio by name in the recorder, recording the absolute cost (~45 ns vs ~39 ns, once per audio tick) and the reason. Do not add thepush_ts_frontfast path.Reasons:
extern "C"shim measured against a header-inline C function.mul_div64divisions and the missing#[inline]on the deque chain). The remaining variants measured here sit inside paired-run noise, so there is no hot spot left to fix.deque_push_front's inline logic in the FFI layer only to win a synthetic bench. That adds a second copy to keep in parity for no full-app gain.Conditions:
--max-ratiogate keeps covering every other pair.The claimant can carry this out; RED is the recorder test showing the named exemption takes the pair out of
--max-ratiowithout dropping it from the report.github-actions commentedon Oct 31, 2026
Claim bot: @zackees, the decision is in; your claim is no longer blocked.
zackees commentedon Oct 31, 2026
Claiming: #3138 — carrying out the
Decided:above (option 1, the named exemption).PR #3153, one change, two commits:
d321093da: three recorder tests for the exemption (entry names pair,cost, machine, issue; exempt pair is not a breach; the pair stays in the
report with its ratio). Fails with
AttributeError: BOUND_EXEMPTIONS.44cdd7f94:BOUND_EXEMPTIONSinrecord.py,over_bound()skipsthe pair by name, the report labels it exempt instead of over-bound, the
gate prints it with its reason, and
docs/rust-port/release-criteria.mddocuments
--max-ratioand the list.Evidence on this host (
--cpu 7 --category audio_mix --repeats 3 --max-ratio 1.10, exit 0): the pair records 1.13x and is printed as exempt, the other 11audio_mixpairs are 0.27x-0.99x, and a non-exempt breach still exits 1../lintok; 19 recorder tests pass.zackees commentedon Oct 31, 2026
Implemented the decision (option 1) in #3152:
RATIO_EXEMPTIONSnamesaudio_mix/buffering_add_one_tickwith the absolute cost (~46 ns vs ~41 ns per call, once per audio tick) and the reason,summarizekeeps it on the row asexemption, and--max-ratio 1.10skips it while still failing every other breach.End-to-end on this host:
record.py --category audio_mix --repeats 3 --cpu 7 --max-ratio 1.10exits 0 and records the pair at 1.157 with the exemption; every otheraudio_mixpair was 0.26-1.06.zackees commentedon Oct 31, 2026
The exemption is in #3153 (merge queue, position 1,
AWAITING_CHECKS):BOUND_EXEMPTIONSnamesaudio_mix/buffering_add_one_tickwith its cost, machine and this issue;over_bound()skips it by name; the report labels it exempt instead of over-bound; the gate prints it. REDd321093da, GREEN44cdd7f94, PR CI green (run 41197171447).zackees commentedon Oct 31, 2026
Fixed by #3153 (squash
d7556f460), merged to master 2026-10-31.What landed:
BOUND_EXEMPTIONSin the format bench gate namesaudio_mix/buffering_add_one_tickwith its measured cost (~46 ns vs ~41 ns per call, once per audio tick) and the reason it is accepted;over_bound()skips it by name, the report labels the rowexemptioninstead of over-bound, and the gate still fails every other case above the 1.10 bound. REDd321093da, GREEN on the merge commit.Nothing left on this issue; the ratio itself is a recorded, argued exemption rather than a code change, so there is no follow-up fix to track here.
perf(audio_mix): fuse the mul_div64 divisions and gate the recorder's…
perf(bench): exempt audio_mix/buffering_add_one_tick from the 1.10 ra…