Skip to content

perf(bench): build the port_comparison C at the shipped optimization level #3127

Closed

Description

@vpacher

What

The port_comparison bench compares each Rust port with the C oracle (rust/obs-c-oracle). The oracle's C is not built like shipped libobs:

Shipped libobs (RelWithDebInfo, build-project.yaml) Oracle under cargo bench
GCC / Clang -O2 -g -O3 (cc takes the level from the bench profile, opt-level 3)
MSVC /O2 /Ob1 /O2
Contraction compiler default (Apple clang on arm64 fuses a * b + c into fmla) -ffp-contract=off (build.rs:439)

The difference in the optimization level changes the results. We ran the whole port_comparison bench on master 88d49a9 twice per machine: as it is (-O3), and with CFLAGS=-O2 (cc puts CFLAGS after its own -O3; the machine code confirms it, see below). The Rust is the same in both runs and is the control for noise.

Machine Cases Rust noise (median, p90) C at -O2 ≥ 2× slower 1.1–2× slower ≥ 10 % faster
Ryzen 9 5950X, Linux, GCC 14.2 (pinned to one core) 310 0.4 %, 4.9 % 17 14 11
Apple M1 Max, macOS, Apple clang (not pinned) 310 0.9 %, 5.3 % 6 15 3

The large changes, mean time per iteration:

Case Machine C -O3 C -O2 Rust Rust/C -O3 Rust/C -O2
audio_mix_scene_mix_add_whole_tick Linux 64.4 ns 236.2 ns 65.7 ns 1.02 0.28
audio_mix_scene_mix_with_buf_whole_tick Linux 93.6 ns 350.6 ns 98.8 ns 1.06 0.28
audio_mix_source_volume_block_stereo Linux 254.2 ns 935.2 ns 249.9 ns 0.98 0.27
audio_mix_mix_whole_tick (audio output tick) Linux 1.06 µs 3.28 µs 1.03 µs 0.97 0.31
format_yuy2_to_uyvx_1080p Linux 150.0 µs 588.6 µs 1165.1 µs 7.77 1.98
format_i420_to_uyvx_1080p Linux 285.9 µs 612.0 µs 648.7 µs 2.27 1.06
format_uyvx_to_nv12_1080p macOS 901.5 µs 5288.7 µs 1215.4 µs 1.35 0.22
format_uyvx_to_i444_1080p macOS 1203.2 µs 5049.4 µs 1530.8 µs 1.27 0.30
  • Linux: the 17 cases at ≥ 2× are the audio mix and volume loops (obs-scene: Port the scene audio mix kernels (#3079) #3092, obs-source: Port the source volume multiply kernels (#3080) #3093 and the output tick mix) and the decompress conversions to UYVX. GCC vectorizes these loops at -O3. At -O2, GCC 12 and later vectorize only with the "very cheap" cost model, which rejects a loop that needs a run-time check that two pointers do not overlap, so these loops stay scalar. In oracle_obs_scene_audio_mix_add, the -O3 build has addps; the -O2 build has only addss.
  • macOS: clang vectorizes the audio loops at -O2 too (oracle_obs_scene_audio_mix_add has the same instructions in both builds), so they do not change. The six cases at ≥ 2× are the compress conversions from UYVX, through SIMDe's SSE on arm64.
  • So for these cases the bench reports the wrong conclusion: on Linux the audio ports look level with C but are 3–4× faster than the shipped C, and the decompress gap looks like up to 7.8× but is up to 2× against the shipped C. On macOS the compress ports look 1.3× slower but are 3–5× faster.
  • The small changes (below 2×) are mostly cases of a few nanoseconds. The cases where the C is faster at -O2 are in that group too.

To get the shipped numbers today, a PR author must build a second C by hand (as in #3092 and #3093). The 2026-10-30 baselines in docs/rust-port/benchmarks/ have the same bias, and rust/tools/benchmarks/record.py records only profile: "bench (release, opt-level 3)", not the C flags.

Proposed

  1. In rust/obs-c-oracle/build.rs, build the oracle C at the shipped level when the profile is optimized: -O2 for GCC/Clang (cc opt_level(2) when OPT_LEVEL is 2 or more). MSVC stays at /O2. The Rust side stays at opt-level 3, as the shipped staticlib (libobs/cmake/rust.cmake, profile release).
    • Do not use a Cargo override such as [profile.bench.package.obs-c-oracle] opt-level = 2. The bench target and its Rust wrappers are in obs-c-oracle, so the override would also lower the Rust that the bench measures where LLVM inlines it into the bench crate.
  2. Keep -ffp-contract=off. Tier 3 compares bits with the Rust, which never fuses; a fused oracle would make parity on arm64 impossible. Write in docs/rust-port/release-criteria.md ("Record C/Rust benchmark evidence") that the bench C is strict IEEE, and that on macOS arm64 the shipped C fuses and can be a little faster.
  3. Add the C compiler flags to the record.py output (for example "c_flags": "-O2 -ffp-contract=off -fno-strict-aliasing"), and record the 2026-10-30 Linux, macOS and Windows baselines again.
  4. Optional, separate PR: a second C build "as shipped" (compiler default contraction) for the bench only, to measure the fused arm64 case.

Tests

  • RED: a check (ci/test_benchmark_record.py or a build.rs unit test) that the oracle's C flags in an optimized profile contain -O2 and not -O3, and that the record has the c_flags field.
  • Tier 3: no change in results expected. The optimization level does not change IEEE results for the ported code, except NaN payloads where two NaNs meet, which the parity tests already accept (obs-source: Port the source volume multiply kernels (#3080) #3093). Run the full parity suite in the release profile to confirm.
  • Bench: the cases in the table above give the -O2 numbers without a second hand build.

Notes

Activity

  1. zackees commented on Oct 31, 2026

    @zackees
    Member

    Claiming: investigation (worker tick, obs-studio4).

    Plan, one branch fix/3127-oracle-shipped-opt-level:

    1. RED: extend ci/test_benchmark_record.py with the oracle-flags contract — in an optimized profile the obs-c-oracle C compile carries -O2 (GCC/Clang) and not -O3, -ffp-contract=off stays, and record.py emits a c_flags field. It fails on current master (build.rs takes cc's default from the bench profile = -O3; no c_flags in the record).
    2. GREEN: rust/obs-c-oracle/build.rs clamps an optimized profile's C opt level to 2 via cc opt_level(2) when OPT_LEVEL >= 2; MSVC stays /O2. record.py records the flags actually used.
    3. docs/rust-port/release-criteria.md: state that the bench C is strict IEEE (-ffp-contract=off) while the shipped C fuses on macOS arm64, so a small shipped-C advantage there is expected.
    4. Re-record the Linux baseline on this host; macOS/Windows baselines stay as a follow-up note in the PR (no hardware here) — or I will say so explicitly if the run is not practical in one tick.

    First PR within this tick.

  2. zackees commented on Oct 31, 2026

    @zackees
    Member

    /ready

  3. added
    needs-investigation
    on Oct 31, 2026
  4. added
    ready
    and removed
    needs-investigation
    on Oct 31, 2026
  5. zackees commented on Oct 31, 2026

    @zackees
    Member

    Draft PR #3130 (worker tick, obs-studio4), fix/3127-oracle-shipped-opt-level.

    • RED: ci/test_benchmark_record.py gains the flags contract and fails on master (no C_FLAGS/report_metadata, no clamp in build.rs).
    • GREEN: build.rs clamps the oracle's C to SHIPPED_C_OPT_LEVEL = 2 when the profile is optimized and folds the level into the oracle input hash; record.py emits c_flags; release-criteria.md documents strict-IEEE bench C vs the fusing shipped macOS arm64 C.
    • Evidence: rebuilt oracle archive disassembly has only addss in oracle_obs_scene_audio_mix_add (shipped-C shape; the -O3 build had addps); cargo bench ... -- --test Success; ci/local_gate.py --only self-test ok; 11/11 recorder tests pass.
    • Still Refs: re-recording the 2026-10-30 baselines at the shipped level needs the three machines; Linux can be done on a quiet host, macOS/Windows cannot from here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ready

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions