Skip to content

obs-codec: NAL header walk without the fold, and short scans word by word (Fixes #1301) - #1433

Merged
zackees merged 4 commits into
obs-rust:masterfrom
vpacher:perf/1301-nal-header-walk
Oct 18, 2026
Merged

zackees merged 4 commits into
obs-rust:masterfrom
vpacher:perf/1301-nal-header-walk

Conversation

@vpacher

@vpacher vpacher commented Oct 18, 2026

Copy link
Copy Markdown
Member

Fixes #1301 (parent #1295).

Investigation

avc/keyframe and hevc/keyframe on master, port_comparison, 5 paired passes:

Platform C compiler avc hevc
Linux x86_64, Ryzen 9 5950X GCC 15 1.729x 1.618x
Windows 11 x86_64, same CPU (VM) MSVC 14.44 1.471x 1.374x
macOS arm64, M1 Max Apple clang 21 1.090x 1.346x

The bench input is 00 00 00 01 + SPS, PPS, SEI (16 bytes each), then the IDR slice (HEVC: VPS, SPS, PPS, SEI, then IDR). keyframe makes 3 or 4 short scans of about 20 bytes and returns at the slice, so the payload size does not matter.

Layout note. On x86_64, a change in nal.rs moved the C oracle in the linked bench binary by 416 bytes, and the C time moved by 3-5 ns (oracle_obs_avc_keyframe 22.1 → 25.7 ns, with no change to C code). All Linux numbers in this PR therefore use pinned alignment for both languages: RUSTFLAGS="-C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=5", CFLAGS="-falign-functions=64 -falign-loops=32". With it, the C time stays at 21.4-21.5 ns (avc) and 27.8-27.9 ns (hevc) in every build.

perf record / perf stat on the pinned bench core (x86_64, after the first change below):

Rust C
instructions per call ~451 ~498
IPC 3.94 5.27
branch misses ~0 ~0
find_startcode 11.4 ns 15.2 ns
keyframe loop 12.7 ns 5.5 ns

Rust does less work but waits longer. About half of the Rust keyframe samples are in the zero skip after each search. Its start depends on the fold in find_startcode:

lea    rax,[rcx-0x1]
cmp    BYTE PTR [rdi+rcx*1-0x1],0x0
cmovne rax,rcx

The result is a select on a loaded byte, so the zero skip and the next search wait for that load. GCC branches there (if (... && !out[-1]) out--;), and the branch is predicted.

Change

All in rust/obs-codec/src/nal.rs. What the functions return does not change:

  1. NEAR: the first 32 positions are tested a word at a time before the 64-byte block skip. Before, the block test ran whenever the rest of the buffer was at least 64 bytes, also when the next start code was 20 bytes away.
  2. Prefilter in word_startcode: a start code begins with a zero byte. If the first word has none, no start code begins in it: one load instead of three.
  3. nal_headers (used only by keyframe) uses the internal search directly. It finds 00 00 01 with at least one byte after it, so the unit starts 3 bytes on. That is where nal_units also lands after the fold and the zero skip, so no loaded byte decides where the next search starts. nal_units (the extract path, which needs exact unit ends) is unchanged.

All three are algorithm changes, not changes that steer the compiler (compare #1426). Each measured alone (x86_64, alignment pinned, interleaved twice):

Variant avc/keyframe/256 hevc/keyframe/256
master 1.718x / 1.722x 1.603x / 1.612x
1 only 1.217x / 1.214x 1.143x / 1.141x
3 only 1.429x / 1.428x 1.415x / 1.410x
1 + 3 0.802x / 0.802x 0.783x / 0.783x
1 + 2 + 3 0.777x / 0.779x 0.748x / 0.749x

1 and 3 depend on each other: while the block test takes most of the time, the load chain hardly shows, and the reverse. Also tried and not used: #[inline] on avc::keyframe / hevc::keyframe (no change: the call from the shim was not the cost).

Exactness

  • No change in behaviour. 86/86 obs-tests-codec, with the Tier 3 parity tests against obs-nal.c: find_startcode_matches_c_* (proptests), every_position_matches_c, every_position_in_long_buffers_matches_c (a start code at every position around the word and block steps), and headers_match_units (proptest: nal_headers gives the header of every unit of nal_units, which is what change 3 relies on). The parity proptests also pass with PROPTEST_CASES=20000. The AVC/HEVC keyframe parity against the C oracle is unchanged and green.
  • Characterization, dd0f35e: zeros_without_a_code_at_every_position_match_c. Zeros that start no code (00 02, 00 00 02, 00 00 00 02) at every position, in short buffers and around the first 64-position step, with and without a real code after them. It checks against C and checks nal_headers against nal_units. It passes before and after the change. I checked that it bites: a prefilter that ignores the eighth byte, and a header walk one byte off, make it fail (with every_position_* and headers_match_units).
  • RED: none, the behaviour does not change (as in obs-codec: AV1 keyframe walk loads each size field at a fixed offset (Fixes #1303) #1426). The evidence is the perf data and the bench numbers.
  • Tier 2: not applicable (no C test or header change). Tier 4: not applicable.
  • clippy -D warnings, rustfmt clean. test_perf_guard_lint, test_local_gate OK.

Perf guards

d9f723d adds three guards to ci/perf-guards.toml (#1430), each with a // PERF GUARD #1301 comment on the function:

fn keeps must not get back without it (x86_64)
nal_headers find_startcode_internal(, pos + 3 find_startcode(, unit_start( 1.21x / 1.14x
find_startcode_internal while i < NEAR 1.43x / 1.41x
word_startcode the zero-byte prefilter about +0.5 ns

These are not codegen shapes like #1303. They protect a shortcut whose result is the same as a simpler path: sharing the nal_units walk in nal_headers would fail no test. I changed the registry header to name this kind of guard as well. perf_guard_lint: 7 guards hold. With master's nal.rs, it reports 9 problems.

Numbers

record.py options, 5 paired passes, medians of the pass medians.

Linux x86_64, Ryzen 9 5950X, CT, taskset one core, OBS stopped, alignment pinned, interleaved (master, change, master, change):

bench master change
avc/keyframe/16384 1.714x / 1.695x 0.776x / 0.773x
avc/keyframe/256 1.729x / 1.723x 0.778x / 0.777x
hevc/keyframe/16384 1.609x / 1.608x 0.751x / 0.750x
hevc/keyframe/256 1.618x / 1.603x 0.749x / 0.752x
avc/extract/256 1.031x / 1.034x 0.919x / 0.920x
hevc/extract/256 1.026x / 1.024x 0.909x / 0.904x

Rust: avc 37.0 → 16.7 ns, hevc 44.8 → 20.9 ns. C: 21.5 / 27.9 ns.

Windows 11 x86_64, same CPU in a VM, MSVC 14.44, interleaved (alignment not pinned):

bench master change
avc/keyframe/256 1.467x / 1.471x 0.561x / 0.567x
hevc/keyframe/256 1.360x / 1.374x 0.587x / 0.589x
hevc/extract/256 0.812x / 0.807x 0.717x / 0.710x

Here the C avc time moved with the layout (27.3 ns in the master builds, 29.6-29.9 ns in the change builds). Against the master C time, avc is 0.61x.

macOS arm64, M1 Max, Apple clang 21:

bench master change
avc/keyframe/256 1.090x 0.426x
hevc/keyframe/256 1.346x 0.524x
avc/extract/256 0.840x 0.766x
hevc/extract/256 0.780x 0.698x

{avc,hevc}/extract/16384 is the same before and after on all three platforms. Rust after the change: 15.2-16.8 ns (avc) and 20.0-21.3 ns (hevc) on all three. C: 21.5 / 27.9 ns (GCC), 27-30 / 36 ns (MSVC), 35.8 / 38.1 ns (clang).

No change to rust-toolchain, target-cpu or RUSTFLAGS (the alignment flags were for the measurement only).

Open points (not in this PR)

🤖 Generated with Claude Code

vpacher and others added 3 commits October 18, 2026 21:57
…s-rust#1301)

Characterization test (coordination.md section 4), not RED: obs-rust#1301 makes
the start-code search and the header walk faster without changing what
they return, so this passes before and after. It covers what the change
adds: a word that holds a zero but no code (`00 02`, `00 00 02`,
`00 00 00 02`) must not stop the search, in short buffers and around the
first 64-position step, with and without a real code after it. Each case
runs the search against the C oracle and checks that the header walk
finds the header of every unit.

Checked that it bites: a prefilter that ignores the eighth byte of a
word, and a header walk one byte off, both make it fail (with
every_position_* and headers_match_units).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…word (Fixes obs-rust#1301)

perf on x86_64 (Ryzen 9 5950X) showed two causes. Rust executed fewer
instructions per call than the C code (about 451 against 498) but at an
IPC of 3.9 against 5.3:
- find_startcode ran the 64-byte block test whenever the rest of the
  buffer was at least 64 bytes, also when the next start code was about
  20 bytes away (parameter sets, SEI).
- The fold of a leading zero is a select on a loaded byte (`cmov`), and
  the zero skip and the next search wait for it. GCC branches there.

Three changes in nal.rs, all without a change in what is returned:
- NEAR: the first 32 positions are tested a word at a time before the
  block skip.
- word_startcode returns early when its first word holds no zero byte (a
  start code begins with one): one load instead of three.
- nal_headers (keyframe only) uses the internal search directly: the
  unit starts 3 bytes after `00 00 01`, which is where the fold and the
  zero skip of nal_units also land. nal_units is unchanged.
Each measured alone on x86_64 (keyframe/256, alignment pinned): NEAR
1.22x, header walk 1.43x, both 0.80x, all three 0.78x (master 1.72x).

86/86 obs-tests-codec (parity proptests against obs-nal.c, also with
PROPTEST_CASES=20000), clippy -D warnings, rustfmt.

{avc,hevc}/keyframe Rust/C, record.py options, 5 paired passes, medians:
  Linux x86_64, GCC 15   avc 1.729x -> 0.778x   hevc 1.618x -> 0.749x
  Windows x86_64, MSVC   avc 1.471x -> 0.567x   hevc 1.374x -> 0.589x
  macOS arm64, clang 21  avc 1.090x -> 0.426x   hevc 1.346x -> 0.524x
Rust after: 15.2-16.8 ns (avc) and 20.0-21.3 ns (hevc) on all three.
{avc,hevc}/extract/256 improve too (Linux 1.03x -> 0.92x, 1.02x ->
0.90x); extract/16384 is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Three guards in ci/perf-guards.toml, with `// PERF GUARD obs-rust#1301` comments
on the functions:
- nal_headers keeps find_startcode_internal and `pos + 3`, and does not
  get find_startcode( or unit_start( back. Its result is the same as the
  nal_units walk, so sharing that walk fails no test: 1.21x / 1.14x
  instead of 0.78x / 0.75x on x86_64.
- find_startcode_internal keeps `while i < NEAR` (with the header walk
  alone: 1.43x / 1.41x).
- word_startcode keeps the zero-byte prefilter.

The registry header now also names this kind of guard: a shortcut that
gives the same result as a simpler path, next to the compiler shapes of
obs-rust#1303.

`perf_guard_lint: 7 guards hold`. With master's nal.rs it reports 9
problems. test_perf_guard_lint, test_local_gate, 86/86 obs-tests-codec,
clippy -D warnings, rustfmt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Fast Gate's `cargo fmt --all --check` failed on one long line in
nal_parity.rs. The codec tests compile through the obs-nvenc test
executable, so the `cargo fmt -p obs-codec --check` I ran did not see
the file. Formatting only.

Checked locally: cargo fmt --all --check, clippy --workspace
--all-targets --locked -D warnings, cargo dylint --all, the ifdef
ratchet and the perf guard lint (7 guards hold).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zackees

zackees commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Coordinator: Fast Gate failed only on cargo fmt --check (tests/codec/nal_parity.rs:193). You already pushed the fix (9e77522), so I dropped mine. Auto-merge is on.

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: AVC and HEVC keyframe scans exceed the Rust performance bound

2 participants