Skip to content

obs-codec: AV1 output shims write straight into their bmalloc buffers (Fixes #1304) - #1414

Merged
zackees merged 2 commits into
obs-rust:masterfrom
vpacher:perf/1304-av1-output-alloc
Oct 18, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
vpacher:perf/1304-av1-output-alloc

Conversation

@vpacher

@vpacher vpacher commented Oct 18, 2026

Copy link
Copy Markdown
Member

Fixes #1304

What and why

The AV1 C ABI shims made Rust heap allocations before they copied into bmalloc. obs_extract_av1_headers grew two Vecs (7 allocations for a 256-byte keyframe packet) and then copied each one. metadata_obu and metadata_obu_itu_t35 built an exact-size Vec and copied it. obs-av1.c allocates only the outputs. This made av1/extract and av1/metadata 1.26x to 1.51x slower than C in #1295.

The safe core now gives slices, and the shims copy them straight into one bmalloc buffer per output:

  • av1::header_obus(data) yields the sequence header and metadata OBUs as slices of data. The packet output is all of data: the OBU walk starts at 0, every OBU is at least one byte and the last one is cut to the end of the buffer, so the OBUs cover every byte.
  • av1::metadata_obu_prefix returns the header byte, the leb128 size and the type in a 12-byte stack buffer. The shim copies prefix, payload and the trailing bits into one buffer.
  • packet::bmalloc_concat_iter fills one bmalloc buffer from an iterator of slices. It panics instead of writing past the buffer when the parts do not add up to the length.

extract_headers and metadata_obu keep their Vec API for the tests. No change to the C ABI or to any output byte: header duplication, metadata type, leb128 size, trailing bits, NULL with size 0 for empty outputs and bmalloc/bfree ownership stay the same.

RED → GREEN

  • RED commit: ad887b5. rust/obs-codec/tests/codec/av1_alloc.rs counts Rust heap allocations on the calling thread (counting global allocator, as in dstr_alloc.rs for bug: dstr: safe formatting is 6.6–58.2× slower than C #1296); bmalloc uses the C allocator, so the outputs themselves are not counted. 0/3 pass: extract 7 allocations (256-byte packet) and 2 (no header OBUs), metadata 1 and 1.
  • GREEN: 53b964c. 3/3; all 14 AV1 tests including the parity proptests against the obs-av1.c oracle; 78/78 obs-tests-codec; clippy -D warnings; cargo bench ... -- --test fixture checks. Tier 2 (C tests against libobs-with-rust) runs in PR CI.
  • Benchmarks: record.py options (--sample-size 30 --warm-up-time 0.1 --measurement-time 0.4 --nresamples 10000), only the av1/ cases, 5 paired passes before and after, same host (Apple M1 Max) and load. Median Rust/C and paired range:
Benchmark before after
av1/extract/16384 1.195x (1.182-1.227) 0.662x (0.647-0.662)
av1/extract/256 1.079x (0.979-1.080) 0.486x (0.483-0.491)
av1/metadata 1.725x (1.676-1.734) 0.949x (0.908-0.958)
av1/metadata_itu_t35 1.719x (1.689-1.741) 0.948x (0.915-0.952)
av1/keyframe/16384 (not changed, #1303) 1.038x (1.032-1.042) 1.033x (1.031-1.037)
av1/keyframe/256 (not changed, #1303) 1.024x (1.007-1.045) 1.026x (1.007-1.029)

All four cases of #1304 are now below the 1.10 bound. Extraction is faster than C because C grows its DARRAYs OBU by OBU, while the packet is now one copy. The unchanged keyframe cases show that the two runs are comparable.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (the parity and Tier 1 tests already cover the output bytes)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change; the Rust libobs is not shipped by default)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

🤖 Generated with Claude Code

vpacher and others added 2 commits October 18, 2026 11:29
…rust#1304)

RED: 0/3. The shims make Rust heap allocations before they copy into
bmalloc: obs_extract_av1_headers 7 for a 256-byte keyframe packet (two
growing Vecs) and 2 without header OBUs, metadata_obu and
metadata_obu_itu_t35 1 each (an exact-size Vec). obs-av1.c allocates only
the outputs. Counting Rust heap allocations on the calling thread is
deterministic, unlike a time bound (as for obs-rust#1296).

Benchmarks before the fix (Apple M1 Max, record.py options, av1/ only,
5 paired passes; median Rust/C and paired range):
  av1/extract/16384     1.195x (1.182-1.227)
  av1/extract/256       1.079x (0.979-1.080)
  av1/metadata          1.725x (1.676-1.734)
  av1/metadata_itu_t35  1.719x (1.689-1.741)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Fixes obs-rust#1304)

GREEN: 3/3 allocation tests, 14/14 AV1 tests (parity proptests against
the obs-av1.c oracle unchanged), 78/78 obs-tests-codec, clippy -D
warnings, bench fixture checks.

The safe core now gives slices instead of owned buffers:
- av1::header_obus(data) yields the sequence header and metadata OBUs
  as slices of `data`. The packet output is all of `data` (the OBU walk
  covers every byte), so obs_extract_av1_headers copies it once.
- av1::metadata_obu_prefix returns the header byte, leb128 size and type
  in a 12-byte stack buffer; the shim copies prefix, payload and the
  trailing bits into one bmalloc buffer.
- packet::bmalloc_concat_iter fills one bmalloc buffer from an iterator
  of slices and panics instead of writing past it when the parts do not
  add up to the length.
extract_headers and metadata_obu keep their Vec API for the tests.

Benchmarks (Apple M1 Max, record.py options, av1/ only, 5 paired passes,
same load as the RED run; median Rust/C and paired range):
  av1/extract/16384     1.195x -> 0.662x (0.647-0.662)
  av1/extract/256       1.079x -> 0.486x (0.483-0.491)
  av1/metadata          1.725x -> 0.949x (0.908-0.958)
  av1/metadata_itu_t35  1.719x -> 0.948x (0.915-0.952)
  av1/keyframe/*        unchanged (1.026-1.033x, obs-rust#1303)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

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

2 participants