Repository navigation
obs-codec: AV1 keyframe walk loads each size field at a fixed offset (Fixes #1303) - #1426
Merged
Merged
Conversation
…Refs obs-rust#1303) Characterization tests (coordination.md section 4), not RED: obs-rust#1303 changes how the OBU walk is compiled, not what it returns, so these pass before and after the change. They pin the path the change touches by name: an OBU with an extension byte reads its size field from byte 2, and a walk where OBUs with and without an extension byte alternate keeps the right offsets. Checked that they bite: with the size field read from byte 1 for every OBU, both new tests fail (and av1_matches_c_oracle with them). 16/16 AV1 tests in obs-tests-codec. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ixes obs-rust#1303) The size field of an OBU starts at byte 1 or 2, after the optional extension byte. Master computes `1 + extension_flag`, and LLVM makes that a select: the size field address then depends on the first byte's value, so each OBU waits for two loads in a row. GCC branches on the flag instead (predicted), so the C walk loads the size field at once. Three changes, each measured on its own (CT104, Ryzen 9 5950X): - parse_obu_header calls obu_after(buf, 1) or obu_after(buf, 2): LLVM keeps the branch. Without it: 1.33x. - #[inline] on keyframe: it was outlined behind a call in the shim. Without it: 1.09x / 1.13x. - leb128 keeps a running shift instead of `i * 7`. No change in behaviour: 85/85 obs-tests-codec (16/16 AV1 incl. the guard tests and the parity proptests against obs-av1.c), clippy -D warnings, rustfmt. av1/keyframe Rust/C, record.py options, 5 paired passes, medians: Linux x86_64, GCC 15 16384 1.613x -> 1.053x 256 1.548x -> 1.034x Windows x86_64, MSVC 16384 1.158x -> 0.842x 256 1.072x -> 0.843x macOS arm64, clang 21 16384 1.035x -> 0.608x 256 1.019x -> 0.587x Rust after is 6.4-8.3 ns on all three; C is 7 ns (GCC), 10 ns (MSVC), 11 ns (clang). av1/extract/256 also improves (0.59x -> 0.49x on Linux), av1/metadata unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees
approved these changes
Oct 18, 2026
zackees
left a comment
Member
There was a problem hiding this comment.
Coordinator review: LGTM.
- Same behaviour:
obu_after(buf, 1|2)is the old1 + extension_flagwith the same.min(size)clamp, and the runningshift(at most 49 over 8 bytes) is the same asi * 7. - The new tests pin exactly what the split could break: a size field read from after the extension byte (0xff there would otherwise continue the leb128), and a walk that alternates OBUs with and without an extension.
- Good evidence: per-compiler disassembly explains why the ratio is a GCC-vs-LLVM codegen difference, and the doc comment asks for a
port_comparisoncheck after toolchain bumps.
This was referenced Oct 18, 2026
Merged
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 #1303 (parent #1295).
Investigation
The issue measured
av1/keyframeat 1.31-1.39x on a shared Ryzen 7 3700X. I measured again on three platforms before I changed code:keyframe/16384keyframe/256The Rust time is almost the same everywhere (10.5-11.8 ns). The C time is not: 7 ns with GCC, 10 ns with MSVC, 11 ns with clang. Thus the ratio shows mostly what the C compiler does.
Disassembly of
obs_av1_keyframe/oracle_obs_av1_keyframein theport_comparisonbench binary:leb128fully.1 + extension_flagwithbt/adc/cmovb. The size field address then depends on the value of the first byte, so each OBU waits for two loads in a row.leb128is a loop that computesi * 7for each byte.ubfx/csinc) and does not inlineparse_obu_header(outputs go through memory). Both sides pay, so the ratio on macOS is about 1.0.setne/cmovbe), but it keeps a running shift inleb128(add ecx, 7).The bench input is three OBUs (sequence header, metadata, frame), so about 20 cycles of load latency per call agree with the measured gap of about 4.5 ns.
Change
All in
rust/obs-codec/src/av1.rs. Each part measured alone on Linux:keyframe/16384keyframe/256obu_after(buf, 1)/obu_after(buf, 2)(LLVM keeps the branch)#[inline]onkeyframe(it was outlined behind a call in the shim)leb128instead ofi * 7#[inline]Two other variants that I tried and did not use:
leb128fully unrolled, as GCC does: 1.7-2.0x. LLVM then stops inliningparse_obu_header.#[inline(always)]).Robustness: please look at this
The fix works because it steers what LLVM generates. It does not change the algorithm. The main part (the branch) depends on LLVM keeping two arms that differ only in a constant. A later LLVM can merge them back into a select, and then the gain disappears without a failing test. The
#[inline(always)]onparse_obu_header/obu_afteris necessary for the same reason. The fully unrolledleb128showed how easily the inlining changes.What limits the risk:
rust-toolchain, 1.98.1). LLVM changes only with a toolchain update.parse_obu_headertells the reason and what to measure (av1/keyframe) after a toolchain update.Question: does the #1295 gate run on toolchain-update PRs? If yes, it catches a regression here. If not, I can add
av1/keyframeto a check for those PRs, if you tell me where it should go.Exactness
obs-tests-codec, with 16/16 AV1 tests. This includes the Tier 3 parity proptests againstobs-av1.c(av1_matches_c_oraclegenerates OBUs with and without an extension byte and padded size fields) andcmocka_cases_match_c_oracle.av1_matches_c_oraclealso fails).clippy -D warnings,rustfmtclean.Numbers
port_comparison,record.pyoptions (--sample-size 30 --warm-up-time 0.1 --measurement-time 0.4 --nresamples 10000), 5 paired passes, medians of the pass medians.Linux x86_64, Ryzen 9 5950X, CT,
tasksetone core, OBS stopped, two interleaved rounds (master, change, master, change):Windows 11 x86_64, same CPU in a VM, MSVC 14.44, interleaved (master, change, master, change):
macOS arm64, M1 Max, Apple clang 21:
Rust after the change: 6.4-8.3 ns on all three platforms.
av1/metadatais the same before and after.No change to rust-toolchain, target-cpu or RUSTFLAGS.
Open points (not in this PR)
av1/metadatais 1.17-1.21x on master and with this change (on the M1 it is 0.95x after obs-codec: AV1 output shims write straight into their bmalloc buffers (Fixes #1304) #1414). That is a different path, and I did not look at it.obs-c-oracledoes not build with MSVC on master:-W4 -WXstops on C4200 (thechar text[]flexible array inobs-internal.h, from b85fd69). For the Windows numbers I added/wd4200throughCFLAGS_x86_64_pc_windows_msvc. Separate issue: obs-c-oracle: MSVC build stops on C4200 (flexible array in obs-internal.h) with -W4 -WX #1427.🤖 Generated with Claude Code