Repository navigation
obs-codec: NAL header walk without the fold, and short scans word by word (Fixes #1301) - #1433
Merged
Merged
Conversation
…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]>
Member
|
Coordinator: Fast Gate failed only on |
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 #1301 (parent #1295).
Investigation
avc/keyframeandhevc/keyframeon master,port_comparison, 5 paired passes: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).keyframemakes 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.rsmoved the C oracle in the linked bench binary by 416 bytes, and the C time moved by 3-5 ns (oracle_obs_avc_keyframe22.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 staton the pinned bench core (x86_64, after the first change below):find_startcodekeyframeloopRust does less work but waits longer. About half of the Rust
keyframesamples are in the zero skip after each search. Its start depends on the fold infind_startcode: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: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.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.nal_headers(used only bykeyframe) uses the internal search directly. It finds00 00 01with at least one byte after it, so the unit starts 3 bytes on. That is wherenal_unitsalso 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):
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]onavc::keyframe/hevc::keyframe(no change: the call from the shim was not the cost).Exactness
obs-tests-codec, with the Tier 3 parity tests againstobs-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), andheaders_match_units(proptest:nal_headersgives the header of every unit ofnal_units, which is what change 3 relies on). The parity proptests also pass withPROPTEST_CASES=20000. The AVC/HEVC keyframe parity against the C oracle is unchanged and green.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 checksnal_headersagainstnal_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 (withevery_position_*andheaders_match_units).clippy -D warnings,rustfmtclean.test_perf_guard_lint,test_local_gateOK.Perf guards
d9f723d adds three guards to
ci/perf-guards.toml(#1430), each with a// PERF GUARD #1301comment on the function:nal_headersfind_startcode_internal(,pos + 3find_startcode(,unit_start(find_startcode_internalwhile i < NEARword_startcodeThese are not codegen shapes like #1303. They protect a shortcut whose result is the same as a simpler path: sharing the
nal_unitswalk innal_headerswould fail no test. I changed the registry header to name this kind of guard as well.perf_guard_lint: 7 guards hold. With master'snal.rs, it reports 9 problems.Numbers
record.pyoptions, 5 paired passes, medians of the pass medians.Linux x86_64, Ryzen 9 5950X, CT,
tasksetone core, OBS stopped, alignment pinned, interleaved (master, change, master, change):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):
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:
{avc,hevc}/extract/16384is 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)
port_comparisonresults by a few ns between builds. That can be important for the meta: resolve OBS Rust profiling regressions and validate recording performance #1295 gate.🤖 Generated with Claude Code