Skip to content

bug: codec: AVC and HEVC keyframe scans exceed the Rust performance bound #1301

Closed

Description

@zackees

Part of #1295.

Context and reproduction

Class: performance regression / release-gate investigation.

AVC and HEVC keyframe helpers consume nal_headers, whose common start-code finder performs block/word scans and a scalar tail. Fixtures exercise 256/16384-byte synthetic packets/prefixes. The paired results exceed 1.10 at both sizes.

Benchmark Rust ns C ns Rust/C Paired run ratio range
avc/keyframe/16384 60.921 45.725 1.332× 1.223–1.699×
avc/keyframe/256 67.491 53.719 1.256× 0.953–1.454×
hevc/keyframe/16384 77.284 53.485 1.445× 1.360–1.568×
hevc/keyframe/256 90.273 62.659 1.441× 1.330–1.796×

Location

Profiling evidence

A 5 s hevc/keyframe/16384/rust profile collected 1,045 samples, no lost samples: 73.41% self cycles in obs_codec::nal::find_startcode and 22.52% in obs_hevc_keyframe. Optimize the shared scan based on C comparison, not an unsupported claim about whole-stream encoding speed.

Proposed fix and failing-test sketch

Compare Rust and C scanner assembly and short/prefix-heavy workloads; improve shared start-code scanning while keeping first-slice early return, malformed-input bounds, zero-run/start-code semantics and existing differential fuzz coverage.

Acceptance criteria

  • Establish RED evidence before coding: reproduce the affected benchmark cases with five paired release passes, median and range; isolate a stable regression from measurement noise. Keep the baseline/command/CPU/source identity.
  • Implement only demonstrated causes, then show GREEN: affected hot-path median Rust/C ratios at or below 1.10 on the same release target, or an explicit separate acceptance of the remaining measured user impact under docs/rust-port/release-criteria.md. For unstable candidates, a reproducible negative result with evidence can close the investigation without a code change.
  • Preserve the public C ABI and allocation contracts. Run affected Rust unit/layout/differential tests and unchanged C tests against both libobs variants for any implementation change; document any inapplicable tier. Keep malformed-input/fuzz and floating-point parity coverage where relevant. Do not edit public headers or existing C tests to make a change pass.
  • Recheck related cases to show the optimization does not transfer cost elsewhere; record absolute times as well as ratios. Functional correctness alone does not close a performance issue.

Decisions

P2: shared scanner affects both codec keyframe helpers; tiny-input noise still needs confirmation. Benchmark input sizes stay together because they exercise the same implementation path. No claim is made that a synthetic ratio equals user-visible frame-time impact.

Measurement context

Measured on 2026-10-17, AMD Ryzen 7 3700X, Linux x86_64, CPU affinity 7, Rust 1.98.1, GCC 15.2.0. Five complete passes of all 70 paired Criterion comparisons; 30 samples/case/pass, 0.1 s warmup, 0.4 s measurement, 10,000 resamples. Values below are medians of the five pass medians. The ratio range contains the five paired Rust/C ratios; the reported median ratio divides the two backend medians.

CARGO_PROFILE_BENCH_DEBUG=1 RUSTFLAGS='-C force-frame-pointers=yes' \
  uv run rust/tools/benchmarks/record.py --cpu 7 --repeats 5 --output baseline.json

The optimized profiling build completed in 40.99 s. Recorded end-of-run HEAD: b56092cc66a979b2f543da20846b965c3517ac36. The checkout was shared and HEAD moved during work; the recorder's source fingerprint stayed unchanged across all five timing passes: 5eea3716b30cf03831eacbc1f06711c2c622f915ebd8945a6940e6fab9d0d422. Source links use fetched master; the fingerprint identifies the measured benchmark/implementation input set. Baseline JSON SHA-256: 9517eb96c66a484cf305a13758a856d893f4608ee454c1a6ea16f693f62c4f26.

This was a shared host. The application build ran on CPUs 0–3 during part of the CPU-7 benchmark run, and other workloads were present. Wide ranges and nanosecond-scale differences require confirmation on quiet release hardware. A descriptive threshold crossing is not proof of statistical significance. The C oracle uses Cargo's optimized C build, rather than full OBS C IPO; profiling frame pointers also affect tiny call costs. No cross-platform or whole-application speedup/slowdown is inferred from shim benchmarks. dstr/printf_control remains C in both configurations and is excluded from Rust regression decisions.

Local evidence: /home/niteris/dev/obs-rust/reports/obs-profile-20261017/ contains baseline.json (per-run medians/confidence intervals), build logs, perf data/reports, assembly, runtime logs/maps/results, and the profiling harness. Raw Criterion samples are in target/criterion, baselines ports-20261017T172745Z-1 through ports-20261017T172745Z-5. Key evidence is reproduced here so triage does not depend on local file access.

Activity

  1. zackees commented on Oct 18, 2026

    @zackees
    MemberAuthor

    Claiming. Plan: profile the start-code finder in nal.rs (block/word scan plus scalar tail) and the keyframe helpers, and match or beat C's scan, for example with a memchr-based search for the 0x00 0x00 0x01 pattern (memchr is now a direct dependency elsewhere in the workspace, #1393) and an early exit once a keyframe NAL is found if C does the same. Parity with C stays covered (keyframe results and NAL boundaries, including start codes split across the block and tail and 3- vs 4-byte start codes). Before/after numbers go in the PR.

  2. added
    claimed
    on Oct 18, 2026
  3. vpacher commented on Oct 18, 2026

    @vpacher
    Member

    @zackees, #1415 (#1302) changed the start-code scan in nal.rs that this issue is about: nal::SplitUnits now does one scan per packet. I have the codec bench driver from #1414/#1415 ready. If you have not started on this yet, I can take it after #1303, with the plan from your claim comment: a memchr-based search for the 0x00 0x00 0x01 pattern (#1393) and the early exit, if C does the same. RED first, then the before/after numbers against the 1.10 bound. If you are already working on it, ignore this.

  4. zackees commented on Oct 18, 2026

    @zackees
    MemberAuthor

    it's yours 🫡

  5. vpacher commented on Oct 18, 2026

    @vpacher
    Member

    Claiming: #1301

    Thanks. First measurements (port_comparison, 5 paired passes; on x86 with pinned code alignment, because unaligned builds moved the C oracle by 3-5 ns between builds):

    AVC keyframe HEVC keyframe
    x86_64 Linux (Ryzen 9 5950X, GCC) 1.73x 1.60-1.64x
    macOS arm64 (M1 Max, clang) 1.08x 1.34x

    First cause: find_startcode_internal runs the 64-byte block test whenever the rest of the buffer is at least 64 bytes long, also when the next start code is about 20 bytes away (parameter sets, SEI). Four word steps before the block skip save about 28 % (x86: 37 → 26 ns AVC, 45 → 34 ns HEVC; M1: 0.73x / 0.95x). That is an algorithm change, not compiler steering. On x86 it is still 1.21-1.25x, so I am looking at the rest (the zero skip in unit_start and the nal_headers walk) before I open a PR.

    🤖 Generated with Claude Code

  6. github-actions commented on Oct 18, 2026

    @github-actions

    Claim bot: @vpacher, this is already claimed by zackees. Pick something else or offer help here; once the claim goes stale, comment Taking over stale claim from @zackees.

  7. zackees commented on Oct 18, 2026

    @zackees
    MemberAuthor

    Releasing #1301 — handing it to @vpacher, who has the bench driver and the first measurements. No partial branch from me to carry over.

  8. removed
    claimed
    on Oct 18, 2026
  9. zackees commented on Oct 18, 2026

    @zackees
    MemberAuthor

    /reassign @vpacher

  10. added
    claimed
    on Oct 18, 2026
  11. vpacher commented on Oct 18, 2026

    @vpacher
    Member

    PR: #1433.

    Summary:

    • perf on x86_64: Rust ran fewer instructions per call than the C code, but at an IPC of 3.9 against 5.3. The fold of a leading zero in find_startcode is a select on a loaded byte, and the next search waited for it. Also, the 64-byte block test ran when the next start code was about 20 bytes away.
    • The fix changes the algorithm, not the codegen: short scans go word by word first, a word without a zero byte is skipped with one load, and the header walk starts each unit 3 bytes after 00 00 01, which is where the fold and the zero skip also land.
    • Result: avc/keyframe 1.73x → 0.78x on Linux (GCC), 1.47x → 0.57x on Windows (MSVC), 1.09x → 0.43x on macOS (clang); hevc/keyframe 1.62x → 0.75x, 1.37x → 0.59x, 1.35x → 0.52x. Rust is now about 16 ns (avc) and 21 ns (hevc) on all three platforms.
    • The shapes have perf guards (ci: perf guard lint keeps the #1303 code shapes (Fixes #1429) #1430).

    One finding for #1295: on x86_64, a Rust-only change moved the C oracle in the bench binary and its time by 3-5 ns. The Linux numbers in #1433 use pinned code alignment for this reason (the flags are in the PR).

    🤖 Generated with Claude Code

  12. added a commit that references this issue on Oct 18, 2026
    fdde5c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions