Skip to content

ci: perf guard lint keeps the #1303 code shapes (Fixes #1429) - #1430

Merged
zackees merged 2 commits into
obs-rust:masterfrom
vpacher:ci/1429-perf-guard-lint
Oct 18, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
vpacher:ci/1429-perf-guard-lint

Conversation

@vpacher

@vpacher vpacher commented Oct 18, 2026

Copy link
Copy Markdown
Member

Fixes #1429 (follow-up to #1303 / #1426).

Why

#1426 is fast because of how LLVM translates one shape of code: two calls with a constant start keep a branch in the AV1 OBU walk. The "simpler" 1 + extension_flag brings back a select and 1.6x the C code on x86_64 Linux, and no test fails, because the behaviour is the same. On #1303 you asked for hardening through comments and custom linting, as a deterrent for future LLMs. This is that lint.

What

  • ci/perf-guards.toml: the guards. Each one names a file and a Rust function, attributes (must be right above the fn), require (must be in the body), forbid (must not be in the body), the reason with the measured numbers, and the bench to run.
  • ci/perf_guard_lint.py: checks the guards. It runs in the lint lane (ci/local_gate.py, next to platform_ifdef_ratchet). Stdlib only, Python 3.11+ (tomllib).
  • rust/obs-codec/src/av1.rs: a // PERF GUARD #1303 (ci/perf-guards.toml) comment on each guarded function, so the warning is in the code before the lint fires. The lint also fails if the comment goes.

Rules: a required shape in a comment does not count, whitespace does not matter (rustfmt), and a guarded function that is gone fails. To change guarded code: measure, and change the guard in the same PR with the numbers.

First guards, all from #1303:

fn keeps must not get back
parse_obu_header obu_after(buf, 1), obu_after(buf, 2), #[inline(always)] 1 + usize::from(
obu_after #[inline(always)]
keyframe #[inline]
leb128 shift += 7 i * 7

What it prints

With av1.rs put back to its shape before #1426, the lint reports 11 problems. The reason and the bench come once per guard:

rust/obs-codec/src/av1.rs: fn parse_obu_header lost #[inline(always)]
rust/obs-codec/src/av1.rs: fn parse_obu_header no longer has `obu_after(buf, 1)`
rust/obs-codec/src/av1.rs: fn parse_obu_header no longer has `obu_after(buf, 2)`
rust/obs-codec/src/av1.rs: fn parse_obu_header has `1 + usize::from(` again
rust/obs-codec/src/av1.rs: fn parse_obu_header lost its `// PERF GUARD #1303` comment
  perf guard #1303: two calls with a constant start make LLVM keep a branch; a computed start (1 + extension_flag) becomes a select, and the size field load waits for the first byte: 1.6x the C code on x86_64 Linux
  measure first: soldr cargo bench -p obs-c-oracle --bench port_comparison -- av1/keyframe (x86_64 Linux, against GCC)
  then change the guard in ci/perf-guards.toml together with the code (#1303)
...
perf_guard_lint: 11 problem(s), see ci/perf-guards.toml

Design choice

Python next to the ifdef ratchet, not a Dylint. The checks are textual ("this call stays", "this attribute stays"), and a Dylint needs rustc_private and a pinned nightly for this. If you prefer a Dylint, I can port it.

Tests

  • RED a0ce67e: ci/test_perf_guard_lint.py against a stub that finds nothing: 10 failures, 1 error of 15. The 4 that pass check that a guarded shape passes, which the stub does trivially.
  • GREEN 0ce9788: 15/15. uv run ci/perf_guard_lint.py on master: 4 guards hold.
  • test_local_gate, test_ci_tests_stdlib_only OK. 85/85 obs-tests-codec, clippy -D warnings, rustfmt (the av1.rs change is comments only).

🤖 Generated with Claude Code

vpacher and others added 2 commits October 18, 2026 21:33
…(Refs obs-rust#1429)

RED: ci/test_perf_guard_lint.py against a stub that finds nothing.
11 of 15 fail (10 failures, 1 error); the 4 that pass check that a
guarded shape passes, which the stub does trivially.

The lint reads ci/perf-guards.toml. Each guard names a file, a Rust
function, the attributes and code shapes that must stay, shapes that
must not come back, the reason and the bench to run. The function must
also carry a `PERF GUARD #<issue>` comment, so a reader (or an LLM) sees
the warning before the lint fires. Comments do not satisfy a required
shape, whitespace does not matter, and a guarded function that is gone
fails.

  uv run python -m unittest discover -s ci -p test_perf_guard_lint.py

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…st#1429)

GREEN: 15/15 ci/test_perf_guard_lint.py, and the repo's guards hold
(`perf_guard_lint: 4 guards hold`). Runs in the lint lane next to
ci/platform_ifdef_ratchet.py.

ci/perf-guards.toml has four guards for the obs-rust#1303 AV1 keyframe walk:
parse_obu_header keeps its two constant-start calls and
#[inline(always)] and does not get `1 + usize::from(` back; obu_after
keeps #[inline(always)]; keyframe keeps #[inline]; leb128 keeps
`shift += 7` and does not get `i * 7` back. Each reason states what
was measured. The guarded functions in av1.rs carry a
`// PERF GUARD obs-rust#1303 (ci/perf-guards.toml)` comment.

With av1.rs put back to its shape before obs-rust#1303, the lint reports 11
problems, for example:

  rust/obs-codec/src/av1.rs: fn parse_obu_header has `1 + usize::from(` again
    perf guard obs-rust#1303: two calls with a constant start make LLVM keep a
    branch; a computed start (1 + extension_flag) becomes a select, ...
    measure first: soldr cargo bench -p obs-c-oracle --bench
    port_comparison -- av1/keyframe (x86_64 Linux, against GCC)
    then change the guard in ci/perf-guards.toml together with the code

Also: 85/85 obs-tests-codec, clippy -D warnings, rustfmt,
test_local_gate and test_ci_tests_stdlib_only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner October 18, 2026 19:34

@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. This is the hardening asked for on #1303.

  • Each guard names its issue, the reason with measured numbers, and the bench to re-run, so a failing lint tells the next person (or LLM) what to measure, not just what not to touch.
  • Comments are stripped and whitespace ignored before matching, so rustfmt and doc edits don't trip it; a guard whose function is gone fails, so it can't silently rot.
  • Wired into the lint lane next to platform_ifdef_ratchet, with its own tests in ci/test_perf_guard_lint.py.

@zackees
zackees enabled auto-merge October 18, 2026 19:42
@zackees
zackees added this pull request to the merge queue Oct 18, 2026
Merged via the queue into obs-rust:master with commit 56e93c9 Oct 18, 2026
23 checks passed
@vpacher
vpacher deleted the ci/1429-perf-guard-lint branch October 18, 2026 20:11
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.

ci: perf guard lint for code shapes that measured fixes depend on (follow-up to #1303)

2 participants