Skip to content

perf(media-io): read a decompress row once instead of per pixel (#3111) - #3114

Merged
zackees merged 1 commit into
masterfrom
perf/3111-decompress-rows
Oct 31, 2026
Merged

zackees merged 1 commit into
masterfrom
perf/3111-decompress-rows

Conversation

@zackees

@zackees zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member

What

decompress_420, decompress_nv12 and decompress_422 called the generic Source/Sink traits per pixel — for i420_to_uyvx, two u8 reads and two u32 writes per output pixel, each bounds-checked and copied through a fixed-size array — where the C walks row pointers and stores *(uint32_t *)dst = y << 16 | uv. That per-pixel call traffic, not arithmetic, is the 1.5-3.2x gap #3111 measured.

Refs #3111.

Change

  • Source gains bytes(offset, len): the row as a slice, built once per row. RawSource builds it from the caller's pointer under the same safety contract as read(); [u8] returns a subslice, so it panics the same way on an out-of-bounds row.
  • The pixel loops index that slice. pair() emits the C's two adjacent uint32_t stores as one 8-byte write, and decompress_422's dw/dup pair likewise.
  • The write side stays offset-addressed: output rows may overlap each other (media-io: decompress_422() converts twice the row it is given #3031), so two row slices would alias. The order of stores is the C's.
  • width_d2 == 0 now continues: a zero-length row slice would be taken at an offset that can be past the plane.

Parity

obs-media-io lib tests, 284 passed / 0 failed — including format_conversion_parity (decompress_420_matches_c, decompress_nv12_matches_c, decompress_422_matches_c, decompress_422_one_row, decompress_422_wrapping_row_offset) and the zero-width case. No test or public header changed.

cargo fmt --check -p obs-media-io clean.

Numbers — read the caveat

record.py --category format, two runs per side, --cpu 7, on this worker host (Ryzen 7 3700X under four concurrent worker checkouts). Ratios, rust/C, best of two:

Case before after
i420_to_uyvx 1.43 0.93
nv12_to_uyvx 1.29 1.17
yuy2_to_uyvx 1.28 1.02
uyvy_to_uyvx 1.04 1.35

This host cannot demonstrate #3111's ≤ 1.10 bound. The compress cases, which this PR does not touch, moved 25% between the two runs (uyvx_to_i420 1.30 vs 3.62, uyvx_to_i444 1.61 vs 2.01), so the spread above is dominated by load, and the uyvy_to_uyvx "regression" is the same code as yuy2_to_uyvx. The two signals that do repeat — i420_to_uyvx and yuy2_to_uyvx — both land at/below 1.10, and nv12_to_uyvx improved but is not there yet.

Left as a draft for that reason: it needs one quiet measurement on a 5950X-class host (record.py --category format, the recorded Linux baseline machine) before it can be called done, and nv12_to_uyvx may need a second pass.

Notes

@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

CI on f1df3193f is fully green: Format 🔍 / checks, Fast Gate ⚡, Rust Tier 2 Linux 🐧 (build, tests ON/OFF, all serial parts) and CI Gate ✅ all pass; everything else skips.

Still a draft for the one thing CI cannot show and this host cannot measure: the ≤ 1.10x bound of #3111 on a quiet 5950X-class machine. record.py --category format there, then either this goes ready as-is or nv12_to_uyvx gets the second pass (it is the one case that improved 1.29 -> 1.17 but still misses).

Local gate stamp: none. The only host-visible input is rust/obs-media-io, and the PR's own Rust Tier 2 + Fast Gate lanes carry it; ./unittest on this box would only re-run the same lanes against a loaded host.

@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Measurement attempt today (2026-10-31, worker tick, same host, --cpu 7): the numbers in the PR body above are not reproducible, and this host cannot settle the bound.

1. record.py --category format --repeats 5 on f1df3193f (5 complete paired runs, medians, paired_run_ratio_range = min/max of the per-run ratios):

Case PR body says today, median today, per-run range
i420_to_uyvx 1.43 -> 0.93 1.77 1.28-2.07
nv12_to_uyvx 1.29 -> 1.17 1.20 0.91-2.22
yuy2_to_uyvx 1.28 -> 1.02 2.22 1.85-3.19
uyvy_to_uyvx 1.04 -> 1.35 2.20 2.14-3.17

The two untouched *_c_scalar controls in the same runs drift the same way (1.02-3.59), so the spread is the host, not the code: --measurement-time 0.4 medians on a box running other worker checkouts.

2. A load-independent metric does not rescue it. I wrapped Criterion's --profile-time 3 per variant in perf stat -e instructions,cycles (each benchmark function alone, rust then C, ratio of counters), on this branch and on origin/master, and repeated the branch run once with no source change:

Case master ins branch ins (run 1) branch ins (run 2) master cyc branch cyc (run 1) branch cyc (run 2)
i420_to_uyvx 1.75 1.42 1.50 1.54 0.99 1.33
nv12_to_uyvx 1.19 1.48 1.72 0.91 1.04 1.04
yuy2_to_uyvx 1.06 1.90 2.35 1.21 1.00 1.08
uyvy_to_uyvx 1.40 1.51 1.94 0.97 1.02 0.99

Two runs of the same binary differing by 30-45% on instruction ratio for conversions this PR does not touch means the counters are being skewed (SMT sibling and a second worker's compile on this host), not that the branch regressed yuy2_to_uyvx. No conclusion about ≤ 1.10 can be drawn from any of it.

Still a draft: the evidence has to come from a quiet host, and nv12_to_uyvx is the one case that has never shown ≤ 1.10 under any method. Do not spend another tick re-measuring here; the number to produce is record.py --category format --repeats 5 on the baseline 5950X machine.

@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Blocked: the code and CI are done; only the <= 1.10x evidence for #3111 is missing, and this host cannot produce it. The 5-repeat paired runs and the perf stat instruction/cycle counters above disagree between two runs of the same binary, so no ratio from this box is admissible, and nv12_to_uyvx has never shown <= 1.10 under any method here.

What unblocks it: one record.py --category format --repeats 5 on the pinned 5950X baseline machine (or any quiet x86_64 host with a stable clock), pasted into this PR. Nothing else is outstanding: f1df3193f is green on Format, Fast Gate, Rust Tier 2 Linux (all parts) and CI Gate.

Re-measuring on this checkout again is wasted work; the earlier comment says so and this tick confirms it.

@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Evidence-lane attempt today (worker tick): a load-independent measurement instead of Criterion wall time.

The plan: run one conversion a fixed number of times over one 1080p fixture and count retired user instructions with perf stat -e instructions:u, then take the slope over two iteration counts so process start-up and warm-up cancel. Retired instructions do not move with the clock or with the other three worker checkouts, so it would settle the <= 1.10x question this host cannot answer in ns.

It did not produce numbers: the local soldr daemon died mid-build, twice, and the build never linked the driver:

soldr: ERROR: lost the connection to the soldr daemon (route soldr-daemon-...)
       after 0.0s: Broken pipe (os error 32)

Two things worth knowing before the next attempt on this box:

  • The daemon that came back after soldr daemon start reported compile jobs: 1 (from default) while the CLI resolves 10, so every crate compiled one at a time (soldr daemon stop is what applies the new limit). A serialized shared daemon is also a plausible contributor to the 25-50% spread between the two record.py runs already in this thread.
  • The route directory for this checkout carries both v0.9.41 and v0.9.47 daemon generations while the CLI is 0.9.46, so there is version skew in the broker/daemon pair; soldr daemon status alternates between BrokenPipe and a live status.

Nothing changed in the tree. Still blocked on the same thing as the comment above: one quiet record.py --category format --repeats 5 run (or an instruction-count run once the daemon is stable) on a host with a stable clock.

The three decompress conversions called the generic Source/Sink traits
per pixel: for i420_to_uyvx that was two u8 reads and two u32 writes per
output pixel, each one bounds-checked and copied through a fixed-size
array, against a C loop that walks row pointers. Measured 1.5-3.2x the C
(#3029's baselines).

Source gains bytes(offset, len): the row as a slice, once per row, and
the pixel loops index it. RawSource builds the slice from the caller's
pointer under the same safety contract as read(). The write side stays
offset-addressed, because output rows may overlap each other (#3031), but
each pair of adjacent pixels is now one 8-byte store like the C's two
adjacent uint32_t stores, in the C's order.

Parity is unchanged: obs-media-io's 284 lib tests pass, including
format_conversion_parity against the C, the _c_scalar stand-ins and the
shim, and decompress_422_one_row (the #3031 overlap fixture).

Local format ratios (rust/C, best of two record.py runs on a loaded
Ryzen 7 3700X; the untouched compress cases move 25% between runs, so
this box cannot demonstrate the 1.10 bound):
  i420_to_uyvx  1.43 -> 0.93
  nv12_to_uyvx  1.29 -> 1.17
  uyvy_to_uyvx  1.04 -> 1.35
  yuy2_to_uyvx  1.28 -> 1.02
@zackees
zackees force-pushed the perf/3111-decompress-rows branch from f1df319 to 7fedebd Compare October 31, 2026 04:36
@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Rebased onto master (0c899b4bf) and re-verified on the new HEAD 7fedebd29:

  • soldr cargo test -p obs-media-io — 284 passed, 0 failed (includes the format_conversion_parity decompress cases and decompress_420_overlapping_rows_keep_c_order).
  • ./lint — == lane lint: ok.
  • PR CI on 7fedebd29 — 13 pass, 0 fail (Fast Gate, Rust Tier 2 Linux all parts, Format, CI Gate). Everything else is skipping by scope, as before.
  • docs/features.md: still no line (no caller in OBS).

The instruction-count lane does not settle it either — closing that avenue

I retried the load-independent idea from the comment above now that the soldr daemon is healthy (soldr daemon status: 0.9.47 daemon, 0.9.47 CLI, 10 compile jobs, protocol 25 — the version skew and the 1-job limit are both gone). The bench binary is port_comparison-e8ac49e952048f86, 4 cases × 2 sides, taskset -c 7 perf stat -e instructions:u.

Two-sample slope (samples 40 vs 160, (i2-i1)/(n2-n1)) returned negative deltas for three of four cases (i420_to_uyvx rust −0.9 M, uyvy_to_uyvx rust −79 M), so this script shape is unusable: Criterion picks a different iteration batch per sample size, so "iterations" are not the same unit across the two runs.

One run per side at identical settings (samples 40, divide the process-wide counter by the reported iteration count) does produce numbers, and they are not believable:

Case rust M instr/iter C M instr/iter rust/C
i420_to_uyvx 19.75 8.93 2.21
nv12_to_uyvx 18.21 10.33 1.76
yuy2_to_uyvx 34.30 8.43 4.07
uyvy_to_uyvx 35.34 8.43 4.19

The counter itself is impossible on this host: 32.4 × 10⁹ user instructions for a run whose 1640 iterations take ~1.8 s (≈18 G instr/s), and the derived 1.8–4.2× ratios contradict the ~1.0–1.2× wall-time ratios for the same binary in the same session. Whatever instructions:u is counting here (perf 7.2.2 on 6.18.48, perf_event_paranoid=-1) it is not this process's retired instructions, so no conclusion can be drawn from it — in either direction. Scripts kept out of the tree (~/dev/obs-rust/instr3111.py, instr3111_single.py), nothing committed.

So the PR still stands where it was: code and CI done, <= 1.10x needs one record.py --category format --repeats 5 on the pinned 5950X baseline machine. This host has no metric — wall time, cycles, or instructions — that survives its own load. Still a draft for that reason; not re-measuring here again.

@zackees
zackees marked this pull request as ready for review October 31, 2026 05:31
@zackees
zackees added this pull request to the merge queue Oct 31, 2026
@zackees

zackees commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

Handed off to the coordinator — green CI Gate, auto-merge enabled from worker tick (obs-studio4).

Merged via the queue into master with commit 88d49a9 Oct 31, 2026
40 checks passed
@zackees
zackees deleted the perf/3111-decompress-rows branch October 31, 2026 05:39
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In progress to Done in obs-rust Multiview Oct 31, 2026
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.

1 participant