Repository navigation
perf(obs-codec): write the AV1 metadata OBU in place (#3175) - #3178
Merged
Merged
Conversation
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"}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3175
What
av1/metadataandav1/metadata_itu_t35were 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 staton 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, andmemcpyread 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'sbzalloc()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 ofout, which must be exactly that long (it panics on any other length). ThroughMaybeUninit, so the shim passes its freshbmallocbuffer without zeroing it (about 4 ns here) and the core keeps#![deny(unsafe_code)];metadata_obu()(theVecform) and the writer sharemetadata_prefix();metadata_obu_prefix()and its array are gone.The
metadata_obu()shim allocates once withbmallocand hands the buffer to the core. The bytes are unchanged (Tier 3 below).Commits
metadata_obu_len()andwrite_metadata_obu()astodo!()stubs and four Tier 1 tests intests/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 tometadata_obu()for three types (in a buffer pre-filled with0xee, so a skipped byte shows), and a short and a long buffer refused. 4 failed, 9 passed.av1tests 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.av1/metadataav1/metadata_itu_t35av1/metadataRust / C ns--max-ratio 1.10fails 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. WithRUSTFLAGS="-C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=5"andCFLAGS="-falign-functions=64 -falign-loops=32"on both:av1/keyframe/163841.037 on master, 1.023 here;av1/keyframe/2561.026 and 0.998;av1/metadata1.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, theav1_parityTier 3 cases against the C oracle including the size boundaries and the proptests, andav1_alloc: the shims make no Rust heap allocation)./usr/bin/time -l): Tier 1av1longest 0.02 s, peak RSS 11 MB (the 300,000-byte case included);av1_parityreduced longest 0.04 s, full cases 0.37 s, peak RSS 11 MB. No test starts its own threads.Checklist
master🤖 Generated with Claude Code