Skip to content

bug: codec: AV1 header and metadata output allocation regresses against C #1304

Closed

Description

@zackees

Part of #1295.

Context and reproduction

Class: performance regression / release-gate investigation.

extract_headers builds packet/header Vecs and metadata_obu builds a Vec; ABI wrappers then allocate C-owned buffers and copy with bmalloc_concat. These intermediate allocations/copies are reached on every matching header/metadata request.

Benchmark Rust ns C ns Rust/C Paired run ratio range
av1/extract/16384 741.258 588.611 1.259× 1.205–1.423×
av1/extract/256 235.976 159.787 1.477× 1.160–3.133×
av1/metadata 57.698 38.303 1.506× 1.425–1.506×
av1/metadata_itu_t35 55.405 40.521 1.367× 1.163–1.647×

Location

Profiling evidence

A 5 s av1/extract/256/rust profile collected 1,080 samples, no lost samples: _int_malloc 14.78%, memmove 11.76%, extract_headers 11.58%, plus free/realloc paths. This supports investigating allocation/copy work; it does not isolate the exact cost of each allocation.

Proposed fix and failing-test sketch

Measure allocation counts and pre-size or directly fill final C-owned output through a safe-core seam. Preserve AV1 header duplication, metadata type/leb128/trailing bits, NULL/empty outputs and bmalloc/bfree ownership.

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: four related output-producing comparisons cross the median bound. 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: #1304 (AV1 header and metadata output allocation)

    Cause, from the code: av1::extract_headers grows two Vecs without a capacity (repeated reallocation), and the shim then copies each one into a new bmalloc buffer. av1::metadata_obu builds an exact-size Vec and the shim copies it into bmalloc. C allocates only the final outputs.

    Plan (one PR):

    1. RED, measured: the four cases (av1/extract/256, av1/extract/16384, av1/metadata, av1/metadata_itu_t35) with record.py, five paired passes, median and range, on a quiet host.
    2. RED, deterministic: a test that counts allocations around the shims (counting global allocator in the test binary plus bnum_allocs). Expected after the fix: only the C-owned outputs are allocated (extract: at most 2, metadata: 1), the same as C.
    3. GREEN: a safe-core output seam. The core gives the output length and fills a caller-provided &mut [u8]; the shim allocates the bmalloc buffer once and passes it in. The packet output is a contiguous part of the input, so it is one copy. The bytes stay the same: header duplication, metadata type, leb128 size, trailing bits, NULL with size 0 for empty outputs, bmalloc/bfree ownership. The existing parity and fuzz tests must stay green.
    4. Report the four medians and ranges again, against the 1.10 bound.

    Decisions: no change to the C ABI or to any output byte. I do not touch nal.rs here; #1302 has the same pattern for AVC/HEVC, and I can use the same seam there in a separate PR after this one.

    I expect to open the PR within the next days.

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