Skip to content

perf: audio_mix/buffering_add_one_tick is 1.19-1.30x the C, above the 1.10x bound #3138

Closed

Description

@zackees

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_add in rust/obs-core/src/audio_mix.rs, FFI shim rust/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.rs passes).
  • 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.

Activity

  1. added
    enhancement
    P3
    size:S
    on Oct 31, 2026
  2. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Claiming: #3138

    1. RED: tighten the existing port_comparison record so a name can carry a ratio bound, and add one for audio_mix/buffering_add_one_tick (Rust/C <= 1.10 over repeated runs); confirm it fails on current master at ~1.2x.
    2. GREEN: remove the per-call FFI trampoline/closure indirection where the bench actually pays it (or, if the cost is the bench reset inside the timed closure, fix the bench and say so in the PR), keeping audio_mix_parity.rs bit-exact.
    3. Local ./lint + the Tier 1 audio tests, then one PR against master with the measured before/after ratio.
  3. added
    ready
    on Oct 31, 2026
  4. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Worker tick progress (2026-10-31 ~09:45Z, obs-studio5).

    Opened #3141 (draft) with the first half:

    • Cause found, and it was not the closure. mul_div64 read the quotient and the remainder through checked_div/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 — two divisions per call where the C has one, twice per buffering_add. A single early 0 check with wrapping_div/wrapping_rem restores the fusion. The deque push chain also lacked #[inline], so where the C's deque_push_front is header-inline the Rust call stayed a real call; it now folds into push_ticks.
    • Measured with 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 says Refs, not Fixes.
    • The bound is now a check: record.py --max-ratio 1.10 exits 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_buffering out and the state back, and keeps one call layer the C inlines (175 instructions vs 102 plus a separate push_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_matches and 45 more audio_mix tests pass).

  5. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    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 (fused mul_div64 divisions, #[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:

    Run Rust C Rust/C
    master 3527bb3fb, record.py --cpu 13 --category audio_mix --repeats 3 44.93 ns 38.71 ns 1.161
    + #[inline] on buffering_add/push_ticks/buffering_ts_minus/frames_to_ns/ns_to_frames/ticks_to_ms, same method 44.20 ns 39.14 ns 1.129
    + #[inline(always)] on buffering_add and push_ticks 46.05 ns 38.83 ns 1.186

    The #[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 branch perf/3138-buffering-add-inline is 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):

    • Rust entry obs_audio_buffering_add: 174 instructions, one call (push_ticks).
    • C entry oracle_obs_audio_buffering_add: 113 instructions, and it makes two calls — oracle_obs_audio_buffering_maxed at the top, then push_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:

    • the shim's 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;
    • state is copied into a Buffering and written back, where the C mutates obs_core_audio fields through the field pointers in place;
    • the 16-byte ts_info push still reaches memcpy@plt through the obs-util Deque core, while the C's header-inline deque_push_front inlines the 16-byte copy into push_buffering_ticks.

    Both push_buffering_ticks and push_ticks are 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 generic Deque core (a second copy of deque_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:

    1. Exempt this pair, with the reason recorded. Extend the recorder's --max-ratio gate with a named exemption for audio_mix/buffering_add_one_tick that 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, or
    2. Keep the bound and accept the boundary duplication described above: a push_ts_front in rust/obs-core/src/ffi/audio_mix.rs implementing the 16-byte deque_push_front path directly (capacity check + two 8-byte stores, memcpy only 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.

  6. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    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_parity bit-exact and #3141's --max-ratio gate:

    1. Exempt this one pair in the recorder by name, recording the absolute cost and the reason (my recommendation — the bound was written for kernels; this is a per-tick FFI call whose C side is a header-inline function).
    2. Keep the bound and accept a 16-byte push_ts_front fast path in rust/obs-core/src/ffi/audio_mix.rs that duplicates deque_push_front's inline logic to drop the memcpy@plt/generic Deque core per push.

    Full measurements and the disassembly comparison (Rust entry 174 instructions, C wrapper 113 with two calls) are in the comment above.

  7. added
    decision-needed
    claim-blocked
    on Oct 31, 2026
  8. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Decided: option 1. Exempt audio_mix/buffering_add_one_tick from 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 the push_ts_front fast path.

    Reasons:

    Conditions:

    1. The exemption entry names the pair, the measured absolute cost, the machine, and this issue.
    2. The release gate that does decide full-app performance, the 5% recording workload bound (quality: Benchmark Rust ports against C on hot paths #142), still applies, so this pair stays in scope there.
    3. Parity stays bit-exact, and perf(audio_mix): fuse the mul_div64 divisions and gate the recorder's 1.10 bound (#3138) #3141's --max-ratio gate 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-ratio without dropping it from the report.

  9. removed
    decision-needed
    claim-blocked
    on Oct 31, 2026
  10. github-actions commented on Oct 31, 2026

    @github-actions

    Claim bot: @zackees, the decision is in; your claim is no longer blocked.

    Decided: option 1. Exempt audio_mix/buffering_add_one_tick from 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 the push_ts_front fast path.

    Reasons:

    Conditions:

    1. The exemption entry names the pair, the measured absolute cost, the machine, and this issue.
    2. The release gate that does decide full-app performance, the 5% recording workload bound (quality: Benchmark Rust ports against C on hot paths #142), still applies, so this pair stays in scope there.
    3. Parity stays bit-exact, and perf(audio_mix): fuse the mul_div64 divisions and gate the recorder's 1.10 bound (#3138) #3141's --max-ratio gate 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-ratio without dropping it from the report.

  11. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Claiming: #3138 — carrying out the Decided: above (option 1, the named exemption).

    PR #3153, one change, two commits:

    • RED 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.
    • GREEN 44cdd7f94: BOUND_EXEMPTIONS in record.py, over_bound() skips
      the 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.md
      documents --max-ratio and 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 11
    audio_mix pairs are 0.27x-0.99x, and a non-exempt breach still exits 1.
    ./lint ok; 19 recorder tests pass.

  12. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Implemented the decision (option 1) in #3152: RATIO_EXEMPTIONS names audio_mix/buffering_add_one_tick with the absolute cost (~46 ns vs ~41 ns per call, once per audio tick) and the reason, summarize keeps it on the row as exemption, and --max-ratio 1.10 skips 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.10 exits 0 and records the pair at 1.157 with the exemption; every other audio_mix pair was 0.26-1.06.

  13. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    The exemption is in #3153 (merge queue, position 1, AWAITING_CHECKS): BOUND_EXEMPTIONS names audio_mix/buffering_add_one_tick with 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. RED d321093da, GREEN 44cdd7f94, PR CI green (run 41197171447).

  14. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Fixed by #3153 (squash d7556f460), merged to master 2026-10-31.

    What landed: BOUND_EXEMPTIONS in the format bench gate names audio_mix/buffering_add_one_tick with 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 row exemption instead of over-bound, and the gate still fails every other case above the 1.10 bound. RED d321093da, 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.

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

Metadata

Metadata

Assignees

Labels

P3enhancementreadysize:S

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions