Skip to content

obs-codec: AV1 keyframe walk loads each size field at a fixed offset (Fixes #1303) - #1426

Merged
zackees merged 2 commits into
obs-rust:masterfrom
vpacher:perf/1303-av1-keyframe-walk
Oct 18, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
vpacher:perf/1303-av1-keyframe-walk

Conversation

@vpacher

@vpacher vpacher commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #1303 (parent #1295).

Investigation

The issue measured av1/keyframe at 1.31-1.39x on a shared Ryzen 7 3700X. I measured again on three platforms before I changed code:

Platform C compiler keyframe/16384 keyframe/256
Linux x86_64, Ryzen 9 5950X GCC 15 1.613x 1.548x
Windows 11 x86_64, same CPU (VM) MSVC 14.44 1.158x 1.072x
macOS arm64, M1 Max Apple clang 21 1.035x 1.019x

The 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_keyframe in the port_comparison bench binary:

  • GCC branches on the extension flag and makes two copies of the parse, with header length 1 and 2. The branch is predicted, so the size field load uses a constant offset from the OBU start. It also unrolls leb128 fully.
  • Rust (LLVM) computes 1 + extension_flag with bt/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. leb128 is a loop that computes i * 7 for each byte.
  • clang makes the same select as rustc (ubfx/csinc) and does not inline parse_obu_header (outputs go through memory). Both sides pay, so the ratio on macOS is about 1.0.
  • MSVC also makes a select (setne/cmovbe), but it keeps a running shift in leb128 (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:

Variant keyframe/16384 keyframe/256
master 1.623x 1.547x
+ obu_after(buf, 1) / obu_after(buf, 2) (LLVM keeps the branch) 1.275x 1.264x
+ #[inline] on keyframe (it was outlined behind a call in the shim) 1.223x 1.183x
+ running shift in leb128 instead of i * 7 1.073x 1.039x
all, without the branch 1.326x 1.326x
all, without #[inline] 1.093x 1.126x

Two other variants that I tried and did not use:

  • leb128 fully unrolled, as GCC does: 1.7-2.0x. LLVM then stops inlining parse_obu_header.
  • Decode the size field at offset 1 and at offset 2, then select. The loads then do not depend on the first byte, and the disassembly shows that. But the extra work cancels the gain: 1.626x / 1.460x (2.08x without #[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)] on parse_obu_header / obu_after is necessary for the same reason. The fully unrolled leb128 showed how easily the inlining changes.

What limits the risk:

  • The toolchain is pinned (rust-toolchain, 1.98.1). LLVM changes only with a toolchain update.
  • The doc comment on parse_obu_header tells 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/keyframe to a check for those PRs, if you tell me where it should go.

Exactness

  • No change in behaviour. 85/85 obs-tests-codec, with 16/16 AV1 tests. This includes the Tier 3 parity proptests against obs-av1.c (av1_matches_c_oracle generates OBUs with and without an extension byte and padded size fields) and cmocka_cases_match_c_oracle.
  • Characterization, 1acc731: two Tier 1 tests for the path that the change makes into its own branch: the size field after the extension byte, and a walk where OBUs with and without an extension byte alternate. They pass before and after the change. I checked that they bite: if the size field is read from byte 1 for every OBU, both fail (and av1_matches_c_oracle also fails).
  • RED: none. The change keeps the behaviour the same, so a behaviour test cannot fail before it. The evidence is the bench numbers and the disassembly above. If you want a different RED for codegen-only issues (for example a bench threshold in a test), I will add it.
  • Tier 2: not applicable (no C test or header change). Tier 4: not applicable.
  • clippy -D warnings, rustfmt clean.

Numbers

port_comparison, record.py options (--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, taskset one core, OBS stopped, two interleaved rounds (master, change, master, change):

bench master change
av1/keyframe/16384 1.613x / 1.612x 1.061x / 1.053x
av1/keyframe/256 1.544x / 1.548x 1.046x / 1.034x
av1/extract/256 0.587x / 0.588x 0.482x / 0.493x
av1/extract/16384 0.612x / 0.607x 0.585x / 0.582x

Windows 11 x86_64, same CPU in a VM, MSVC 14.44, interleaved (master, change, master, change):

bench master change
av1/keyframe/16384 1.155x / 1.158x 0.834x / 0.842x
av1/keyframe/256 1.063x / 1.072x 0.834x / 0.843x
av1/extract/256 0.517x / 0.518x 0.446x / 0.440x

macOS arm64, M1 Max, Apple clang 21:

bench master change
av1/keyframe/16384 1.035x 0.608x
av1/keyframe/256 1.019x 0.587x
av1/extract/256 0.460x 0.440x

Rust after the change: 6.4-8.3 ns on all three platforms. av1/metadata is the same before and after.

No change to rust-toolchain, target-cpu or RUSTFLAGS.

Open points (not in this PR)

🤖 Generated with Claude Code

vpacher and others added 2 commits October 18, 2026 20:47
…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 zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: LGTM.

  • Same behaviour: obu_after(buf, 1|2) is the old 1 + extension_flag with the same .min(size) clamp, and the running shift (at most 49 over 8 bytes) is the same as i * 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_comparison check after toolchain bumps.

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.

bug: codec: AV1 keyframe OBU walking exceeds the Rust performance bound

2 participants