Skip to content

bug: codec: small AVC and HEVC header extraction regress against C #1302

Closed

Description

@zackees

Part of #1295.

Context and reproduction

Class: performance regression / release-gate investigation.

Header extraction classifies NAL units into SplitHeaders Vec buffers, then the ABI adapter copies the resulting buffers into C-allocator-owned outputs via bmalloc_concat. Current 256-byte cases cross 1.10; larger tracked extraction cases do not cross it in this report.

Benchmark Rust ns C ns Rust/C Paired run ratio range
avc/extract/256 301.855 240.697 1.254× 0.338–1.636×
hevc/extract/256 399.055 294.933 1.353× 0.985–2.892×

Location

Profiling evidence

Temporary Vec construction and the later C-owned copy are visible in the code. Their share of extraction cost has not been isolated from scanner cost; this is a hypothesis to validate with allocation counts and a focused extraction profile.

Proposed fix and failing-test sketch

Confirm stable regression, measure allocation/copy counts, and reduce intermediate allocation or scanning overhead through a safe core output seam. Preserve packet/header/SEI split bytes, allocation ownership, NULL/empty outputs and C parity.

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 investigation: 256-byte median regressions; paired ranges cross or approach parity. 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. vpacher commented on Oct 18, 2026

    @vpacher
    Member

    Claiming: #1302 (small AVC and HEVC header extraction)

    Cause, from the code: nal::split_headers grows three Vecs (packet, header, SEI) unit by unit, and packet::write_split then copies each one into a new bmalloc buffer. This is the same pattern as #1304 (PR #1414).

    Plan (one PR, stacked on #1414 because it uses its bmalloc_concat_iter):

    1. RED, measured: avc/extract/256, hevc/extract/256 (and the larger extract cases as a check) with the record.py options, five paired passes, median and range. The issue's ranges are wide (0.338-1.636x), so this step shows first whether the regression is stable.
    2. RED, deterministic: count the Rust heap allocations around obs_extract_avc_headers and obs_extract_hevc_headers, as in obs-codec: AV1 output shims write straight into their bmalloc buffers (Fixes #1304) #1414. Expected after the fix: none for a normal packet; only the three C-owned outputs are allocated.
    3. GREEN: one start-code scan. The unit ranges go into a fixed-size stack array (a normal packet has a few NAL units), with a heap fallback only for packets with very many units. Then each output gets one bmalloc and one copy per unit. A second scan only to get the lengths would double the scan cost, so I do not use one.
    4. Bytes stay the same: unit order, start codes, the split into packet/header/SEI, NULL with size 0 for empty outputs, bmalloc/bfree ownership. The parity proptests against the C oracle must stay green.

    Decisions: if step 1 shows no stable regression at 256 bytes, I still report the numbers here and keep the change only if the allocation test and the benchmarks show a gain.

    I expect to open the PR today.

  2. added
    claimed
    on Oct 18, 2026
  3. added a commit that references this issue on Oct 18, 2026
    b5fc888
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