Skip to content

perf(obs-codec): write the AV1 metadata OBU in place (#3175) - #3178

Merged
vpacher merged 2 commits into
masterfrom
perf/3175-av1-metadata-prefix
Oct 31, 2026
Merged

vpacher merged 2 commits into
masterfrom
perf/3175-av1-metadata-prefix

Conversation

@vpacher

@vpacher vpacher commented Oct 31, 2026

Copy link
Copy Markdown
Member

Fixes #3175

What

av1/metadata and av1/metadata_itu_t35 were 1.12–1.21x the C (#3175). The cause was not more work: per call, the Rust shim ran fewer instructions than the C (about 289 against 324, perf stat on the pinned bench process) but at 1.91 instructions per cycle against 2.57. metadata_obu_prefix() built the header byte, the leb128 size field and the type with byte stores into a 12-byte stack array, returned it by value, and memcpy read it back with wider loads: a store-to-load forwarding stall. Ruled out first: code layout (with the same function and loop alignment for both languages it stayed at 1.217) and the C's bzalloc() zeroing (zeroing the Rust buffer too made it 1.32x).

The safe core now writes the OBU straight into the caller's buffer:

  • metadata_obu_len(payload_len): the OBU's length, from the size field's bit count;
  • write_metadata_obu(payload, metadata_type, out: &mut [MaybeUninit<u8>]): writes every byte of out, which must be exactly that long (it panics on any other length). Through MaybeUninit, so the shim passes its fresh bmalloc buffer without zeroing it (about 4 ns here) and the core keeps #![deny(unsafe_code)];
  • metadata_obu() (the Vec form) and the writer share metadata_prefix(); metadata_obu_prefix() and its array are gone.

The metadata_obu() shim allocates once with bmalloc and hands the buffer to the core. The bytes are unchanged (Tier 3 below).

Commits

  1. RED 1f3a9da: metadata_obu_len() and write_metadata_obu() as todo!() stubs and four Tier 1 tests in tests/codec/av1.rs: the length at the leb128 boundaries (1, 2 and 3 size bytes, payloads 0 to 300,000 bytes), the written OBU equal to metadata_obu() for three types (in a buffer pre-filled with 0xee, so a skipped byte shows), and a short and a long buffer refused. 4 failed, 9 passed.
  2. GREEN 3502882: the writer, the length, the shared prefix and the shim. 20 of 20 av1 tests pass.

Numbers

rust/tools/benchmarks/record.py --cpu 31 --repeats 5 --category av1 --max-ratio 1.10, Ryzen 9 5950X (Linux x86_64), no profiling flags, the oracle C at the shipped level. Medians of the 5 pass medians; paired run ratios in brackets.

Case master 9ba24b9 this PR (2 runs)
av1/metadata 1.201 (1.179–1.209) 1.005 (0.983–1.012), 0.983 (0.976–1.003)
av1/metadata_itu_t35 1.194 (1.160–1.198) 0.986 (0.955–0.990), 0.957 (0.955–0.988)
av1/metadata Rust / C ns 34.68 / 28.89 28.97 / 28.83, 28.90 / 29.41

--max-ratio 1.10 fails on master and passes on this PR.

Without alignment flags, av1/keyframe/* also moved in these runs (0.90 → 1.08), though this PR does not touch that code. With RUSTFLAGS="-C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=5" and CFLAGS="-falign-functions=64 -falign-loops=32" on both: av1/keyframe/16384 1.037 on master, 1.023 here; av1/keyframe/256 1.026 and 0.998; av1/metadata 1.193 and 0.982. So the keyframe move is code placement, and the metadata gain holds with the layout held fixed.

Tests

  • soldr cargo test -p obs-nvenc --test obs-tests-codec -- av1: 20/20 (Tier 1, the av1_parity Tier 3 cases against the C oracle including the size boundaries and the proptests, and av1_alloc: the shims make no Rust heap allocation).
  • Local gate, stamped head 3502882: lint, rust-test, cross and tier2-linux passed (4 min 48 s).
  • Test cost (macOS, debug, 25 runs, /usr/bin/time -l): Tier 1 av1 longest 0.02 s, peak RSS 11 MB (the 300,000-byte case included); av1_parity reduced longest 0.04 s, full cases 0.37 s, peak RSS 11 MB. No test starts its own threads.
  • Not run on Windows or macOS separately: no platform code; the cross lane of the gate covers Windows and macOS clippy.

Checklist

  • Base is master
  • I claimed the issue
  • RED, then GREEN commits
  • User-visible change: not applicable (no behaviour change)
  • (Optional) Local gate passed and the pushed head is stamped
  • Warning or flag changes: not applicable
  • Intentional differences from C: none

🤖 Generated with Claude Code

vpacher and others added 2 commits October 31, 2026 18:04
av1/metadata and av1/metadata_itu_t35 are 1.12-1.21x the C (#3175).
The shim runs fewer instructions per call than the C (about 289 against
324, perf stat on the pinned bench) but at 1.91 instructions per cycle
against 2.57: metadata_obu_prefix() builds the header, leb128 size and
type with byte stores into a 12-byte stack array, returns it by value,
and memcpy reads it back with wider loads. Writing the prefix straight
into the output buffer (a scratch build) gives 1.023 and 1.010.

The safe core gets the seam for that: metadata_obu_len() and
write_metadata_obu(), which writes the OBU into a caller's buffer of
exactly that length and panics on any other length. Both are todo!()
stubs here. Four Tier 1 tests in tests/codec/av1.rs: the length over
payload sizes at the leb128 boundaries (1, 2 and 3 size bytes, up to a
300,000-byte payload), the written OBU equal to metadata_obu() for
three metadata types, and a short and a long buffer refused.

soldr cargo test -p obs-nvenc --test obs-tests-codec -- av1::
4 failed (the stubs), 9 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
write_metadata_obu() writes the header byte, the leb128 size field,
metadata_type, the payload and the trailing bits straight into the
caller's buffer, through MaybeUninit, so the shim passes its fresh
bmalloc buffer without zeroing it (zeroing costs about 4 ns here) and
the core stays safe. metadata_obu_len() gives that buffer's length from
the size field's bit count. metadata_obu() (the Vec form) and the
writer share metadata_prefix(); metadata_obu_prefix() and its 12-byte
array are gone.

The metadata_obu() shim allocates once with bmalloc and hands the
buffer to the core: no prefix array, no copy of it.

soldr cargo test -p obs-nvenc --test obs-tests-codec -- av1: 20 passed
(Tier 1, the av1_parity Tier 3 cases against the C oracle, and the
av1_alloc check that the shims make no Rust heap allocation).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=e7161da358f9ee6b984f6199784dc60cadb72c4c secs=287 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793467013,"gate":"rust/all/fmt","host":"linux-x86_64","key":"528cd201d8572739944fa3d0bae379f8bee7d8dc6abeab645f8dde185fa64fd1","lane":"lint","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":73,"stamp":"b174a2cfc763e4544927f1749ac9a8b1","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"528cd201d8572739944fa3d0bae379f8bee7d8dc6abeab645f8dde185fa64fd1","lane":"lint","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":73,"stamp":"4b77f351096289f8732075967e664afa","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"0c362611a39442ebaf668af0ed1d1ee013c2b8aed5fe51629b2b962f591316a4","lane":"rust-test","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":164,"stamp":"331ee6899cab4c9bc36a77c6dd369749","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"1961d9269cb465b21769c044bd87c5a383ba205e16cdef4fd5db67bfbf54725c","lane":"cross","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":49,"stamp":"83b0c5093e9ed165f9c469d1b4cd795a","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"1961d9269cb465b21769c044bd87c5a383ba205e16cdef4fd5db67bfbf54725c","lane":"cross","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":49,"stamp":"d1ab995b993d54cc3861bdfd61ab0de2","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"1961d9269cb465b21769c044bd87c5a383ba205e16cdef4fd5db67bfbf54725c","lane":"cross","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":49,"stamp":"637a06f43ec809cf6ffe5e299ac7e3c0","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"1961d9269cb465b21769c044bd87c5a383ba205e16cdef4fd5db67bfbf54725c","lane":"cross","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":49,"stamp":"5abb21f998f7e911a8651bf8c12385b8","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
Ci-Attestation: {"at":1793467013,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4e4c891fd98fd42c3b3506e9a01d9921c74da452d72155ff3338cbb655c27635","lane":"tier2-linux","parents":["1f3a9da26eb461ffb3e7b747316344951d695fcb"],"secs":248,"stamp":"192155dd30f01973dbcbf186d398474b","tree":"e7161da358f9ee6b984f6199784dc60cadb72c4c","v":1,"via":"run"}
@vpacher
vpacher requested a review from zackees as a code owner October 31, 2026 17:21
@vpacher
vpacher enabled auto-merge October 31, 2026 17:22
@vpacher
vpacher added this pull request to the merge queue Oct 31, 2026
Merged via the queue into master with commit 0d4da10 Oct 31, 2026
36 checks passed
@vpacher
vpacher deleted the perf/3175-av1-metadata-prefix branch October 31, 2026 17:34
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: codec: av1/metadata and av1/metadata_itu_t35 are 1.12–1.21x the C, above the 1.10x bound

1 participant