Skip to content

perf(bench): build the oracle C at the shipped optimization level (#3127) - #3130

Merged
zackees merged 2 commits into
masterfrom
fix/3127-oracle-shipped-opt-level
Oct 31, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/3127-oracle-shipped-opt-level

Conversation

@zackees

@zackees zackees commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

The bench profiled the C at -O3 while users run -O2: cargo bench builds obs-c-oracle at opt-level 3, and cc took the level from the profile, whereas the shipped libobs is RelWithDebInfo (-O2 -g, /O2 on MSVC). That is a measurement bug, not a Rust regression: on Linux the audio mix/volume loops look level with C in the bench (Rust/C 0.97–1.06) but the shipped C is 3–4× slower, and the decompress conversions looked up to 7.8× when they are up to 2× against the shipped C. macOS compress cases look 1.3× slower but are 3–5× faster.

Fixes #3127

RED

ci/test_benchmark_record.py gains CFlagsTest (three cases): the oracle C is clamped to the shipped level in build.rs, the recorder names the flags it measured, and the report carries them. On master:

AttributeError: module 'record' has no attribute 'C_FLAGS'
AttributeError: module 'record' has no attribute 'report_metadata'
AssertionError: 'const SHIPPED_C_OPT_LEVEL: u32 = 2;' not found

GREEN

  • rust/obs-c-oracle/build.rs: SHIPPED_C_OPT_LEVEL = 2; when the Cargo profile is optimized, oracle.opt_level(SHIPPED_C_OPT_LEVEL). The level is also folded into the oracle input hash so an archive at another level cannot be served stale. The Rust side keeps the profile's opt-level 3, which is its shipped level (libobs/cmake/rust.cmake, profile release); a [profile.bench.package.obs-c-oracle] override is deliberately not used because it would lower the Rust the bench measures. MSVC keeps /O2. -ffp-contract=off stays: Tier 3 compares bits with the Rust, which never fuses, and a fused oracle would differ on arm64.
  • rust/tools/benchmarks/record.py: C_FLAGS records -O2 -ffp-contract=off -fno-strict-aliasing, emitted as c_flags; the report fields moved into report_metadata() so the contract is testable without running the bench.
  • docs/rust-port/release-criteria.md: the bench C is strict IEEE while the shipped macOS arm64 C fuses a * b + c, so a small shipped-C advantage there is expected, not a regression.

Evidence

Rebuilt oracle archive (target/release/build/obs-c-oracle-*/out/libobs_c_oracle.a), disassembly of the case the issue names:

$ objdump -d libobs_c_oracle.a | awk '/<oracle_obs_scene_audio_mix_add>:/{f=1} ...'
      1   addss  -0x4(%rsi),%xmm0

addss only — the shipped-C shape; the -O3 build carried addps.

soldr cargo bench -p obs-c-oracle --bench port_comparison -- --test
  ... Success
uv run ci/local_gate.py --only self-test
  == self-test: ok
uv run python -m unittest ci/test_benchmark_record.py
  Ran 11 tests ... OK
soldr cargo fmt --check -p obs-c-oracle   # clean
uv run ruff check rust/tools/benchmarks/record.py ci/test_benchmark_record.py   # pass

Tiers

  • Tier 1/3 Rust: unchanged; the optimization level does not change IEEE results for the ported code, and -ffp-contract=off is kept. Full parity run left to PR CI.
  • Tier 2: not applicable (build-script and recorder only).
  • Tier 4: not applicable.

Test cost (testing-policy §"Test time")

New tests: three cases, longest < 0.1 s, no own threads, no large allocations; they read build.rs and call record.report_metadata().

Baseline re-recorded at the shipped level (Linux)

uv run rust/tools/benchmarks/record.py --cpu 7 --repeats 5 --category audio_mix --category format --category conversions --output 3127-linux.json on dea01fd00, affinity CPU 7, AMD Ryzen 7 3700X, gcc (GCC) 15.2.0, c_flags recorded as -O2 -ffp-contract=off -fno-strict-aliasing (32 complete comparisons, 5 paired repeats). Rust/C medians, against the -O2 column of #3127's table (* = macOS row in the issue, re-run here on Linux):

Case #3127 (-O2) C ns Rust ns Rust/C now
audio_mix_scene_mix_add 0.28 444 114 0.26
audio_mix_scene_mix_with_buf 0.28 555 166 0.30
audio_mix_source_volume_block 0.27 418 327 0.78
audio_mix_mix_whole_tick 0.31 5640 1433 0.25
format_yuy2_to_uyvx 1.98 1555484 1785043 1.15
format_i420_to_uyvx 1.06 1687837 1016576 0.60
format_uyvx_to_nv12 1.35* 810190 3141503 3.88
format_uyvx_to_i444 1.27* 1612911 3364717 2.09

The mis-measurement is gone: with the oracle at the shipped level the audio mix ports read 3-4x faster than C (0.25-0.30, where -O3 reported 0.97-1.06 "level"), and yuy2_to_uyvx reads 1.15 instead of 7.77. Two differences from the issue's table are host, not code: this box is a 3700X with GCC 15.2 (the issue used a 5950X with GCC 14.2), and GCC 15's cost model vectorizes source_volume_block/stereo at -O2 too, so that case reads 0.78 rather than 0.27.

Not re-recorded: the macOS and Windows baselines (they need those machines). Those are data refreshes only; this PR's change is host-independent, and the flags are now named in every report so a future run says which C it measured.

…3127)

RED: the recorder has no c_flags field and build.rs leaves the oracle C at
the bench profile's -O3. The three new cases fail:

  AttributeError: module 'record' has no attribute 'C_FLAGS'
  AttributeError: module 'record' has no attribute 'report_metadata'
  AssertionError: 'const SHIPPED_C_OPT_LEVEL: u32 = 2;' not found

Refs #3127
)

The bench profile is opt-level 3, so cc built the oracle's C at -O3 while the
shipped libobs C is RelWithDebInfo (-O2 -g, /O2 on MSVC). GCC's -O2 cost model
refuses the runtime-overlap check in the audio mix and volume loops, so the
shipped C stays scalar where the -O3 oracle vectorized them: the bench called
the Rust 3-4x faster than C on cases that are actually at parity, and reported
up to 7.8x on conversions that are 2x against the shipped C.

build.rs now clamps the oracle's C to SHIPPED_C_OPT_LEVEL (2) whenever the
Cargo profile is optimized, and puts the level in the oracle input hash. The
Rust side keeps the profile's opt-level 3, as its shipped release static
library does; a [profile.bench.package] override would lower the Rust the
bench measures too. -ffp-contract=off stays: Tier 3 compares bits with the
Rust, which never fuses.

record.py records c_flags so a baseline names the C it measured, and
release-criteria.md says the bench C is strict IEEE while the shipped macOS
arm64 C fuses, so a small shipped-C advantage there is expected.

GREEN: objdump of the rebuilt oracle archive shows only addss (scalar) in
oracle_obs_scene_audio_mix_add, the shipped-C shape, where the -O3 build had
addps. soldr cargo bench ... -- --test: Success. uv run ci/local_gate.py
--only self-test: ok. Baselines re-recorded on Linux only if a follow-up runs
it; macOS/Windows need their own hardware.

Refs #3127
@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Handed off to the coordinator. The Linux baseline is re-recorded at the shipped level (see the table in the description), HEAD dea01fd00 has CI Gate ✅, and the PR is queued at position 1. macOS and Windows baselines still need those machines; that is data, not code.

Merged via the queue into master with commit 2692c3d Oct 31, 2026
36 checks passed
@zackees
zackees deleted the fix/3127-oracle-shipped-opt-level branch October 31, 2026 07:53
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(bench): build the port_comparison C at the shipped optimization level

1 participant