Repository navigation
Description
What
The decompress half of libobs/media-io/format-conversion.c is slower in Rust than the C it replaces, and the C here is already scalar — so this is not a SIMD gap (#3004 covers the compress side, where the C is SSE2). The measured cost tracks the per-pixel Source::read / Sink::write calls, not the arithmetic.
Baselines from #3029 (docs/rust-port/benchmarks/baseline-2026-10-30-linux-x86_64.json and -macos-arm64.json, record.py 5 complete runs, Rust/C medians, commit ccce18950; below 1 means Rust is faster):
| Case | Linux x86_64 (Ryzen 9 5950X, 1 core) | macOS arm64 (M1 Max) |
|---|---|---|
format/i420_to_uyvx/1080p |
3.20 | 1.46 |
format/nv12_to_uyvx/1080p |
2.45 | 1.04 |
format/uyvy_to_uyvx/1080p |
1.53 | 1.15 |
format/yuy2_to_uyvx/1080p |
1.58 | 1.13 |
Both sides of each pair write the same bytes (the bench asserts it before timing), so the gap is execution cost, not extra work. The C columns differ only on the compress side; for these four cases im == _c_scalar.
Absolute Linux numbers: i420_to_uyvx 914 µs (Rust) vs 285 µs (C) per 1080p frame; nv12_to_uyvx 431 µs vs 176 µs.
Where the time goes
Every access is a generic call per pixel into a [u8]-implemented trait, which bounds-checks and copies a fixed-size array (rust/obs-media-io/src/format_conversion.rs:20-42):
decompress_420(:223, inner loop:244-256): per 2 pixels, 2 reads of one chroma byte + 2 reads of one luma byte + 2 writes of 4 bytes — 3 trait calls per output pixel, 1 byte each on the read side.decompress_nv12(:264): same shape, oneu16read per 2 pixels instead of twou8reads.decompress_422(:305): one 4-byte read and two 4-byte writes per 2 pixels.
The C (libobs/media-io/format-conversion.c.h) holds row pointers for the row pair and does *(uint32_t *)dst = y << 16 | uv — no call, no bounds check. That difference, not arithmetic, is the 3.2× — the same measurement the compress side shows from the other direction (Rust is 10× faster than the plain-C __m128i stand-ins at uyvx_to_i420, so the Rust scalar loop itself is fine).
Proposed
Hoist the row pair out of the pixel loop: take the row slices once per y, then index them directly — u32::from_ne_bytes(row[i..i+4].try_into().unwrap()) on the write side and plain row[i] on the read side. Keep the offset/addressing semantics (rows in_linesize apart, start_y / 2 .. end_y / 2, width_d2 = in_linesize[0] / 2) and the C's order of overlapping reads/writes, which #3031 showed matters. If the generic Source/Sink form is kept for the parity tests, add a byte/word fast path to the traits so the FFI shims stay zero-cost instead of restructuring every caller.
Acceptance criteria
- All four decompress cases at ≤ 1.10× the C on Linux x86_64 and macOS arm64 in the
formatcategory ofrecord.py(the project bound, meta: resolve OBS Rust profiling regressions and validate recording performance #1295); before and after numbers in the PR. format_conversion_parity.rsstill passes unchanged on the C, the C-scalar stand-ins and the shim (it is the oracle here, so parity is exact).- No behaviour change on the media-io: decompress_422() converts twice the row it is given #3031 overlap fixture:
decompress_422converts exactly one row per row.
Failing-test sketch
RED: a benchmark-bound check in record.py's format category that fails while format/i420_to_uyvx/1080p Rust/C > 1.10, plus (if the traits change) a Tier 1 test that the new byte/word accessors return the same bytes as the generic ones on a row pair.
Notes
- No caller in OBS. As with perf: obs-media-io: SIMD format conversion (Rust is scalar where the C is SSE2) #3004, upstream removed the CPU YUV conversion (
e5b004fd4); these six functions are exported API informat-conversion.hfor plugins. So this is a plugin-API quality issue, not a runtime path — henceP3, notP1. - Not a SIMD candidate: the C is scalar and bit-exact output is expected, so vectorization would only be a second step after the access pattern is fixed.
- Refs perf(bench): Record the SIMD candidates and repair the benchmark recorder (#3003) #3029 (the baseline and its findings), meta: perf: portable SIMD in the Rust port (tracking) #3009 (SIMD meta), perf: obs-media-io: SIMD format conversion (Rust is scalar where the C is SSE2) #3004 (compress side), media-io: decompress_422() converts twice the row it is given #3031 (the overlap bug this must not reintroduce).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInvestigating
Activity
zackees commentedon Oct 30, 2026
Claiming: investigation — obs-media-io decompress fast path (#3111)
Plan:
format/*_to_uyvx/1080pRust/C ratios on this host withrecord.py --category format(Linux x86_64).decompress_420/decompress_nv12/decompress_422so the row pair is hoisted out of the pixel loop and the per-pixel genericSource/Sinkcalls become word-sized accesses, keeping the C's order of reads and writes (media-io: decompress_422() converts twice the row it is given #3031 overlap fixture).formatbench category (Rust/C > 1.10 fails); GREEN: the restructured loops,format_conversion_parity.rsunchanged, before/after ratios in the PR.I will
/readythis after step 1 records the baseline.zackees commentedon Oct 31, 2026
Progress — the measurement path was broken, now fixed; the rewrite is up as a draft.
record.pycould not run at all on master. libobs: Port the hotkey binding press count to obs-hotkey (#1836) #3075 exportedobs_hotkey_binding_press()/_release()fromobs-hotkeywhile theport_comparisonbench still defined bench: port_comparison does not link on Windows (18 undefined libobs symbols) #3025's stand-ins for them, so the bench binary was a duplicate-symbol link error (rust-lld, andlld-linkthe same way). Filed as perf(bench): port_comparison no longer links on master — duplicate obs_hotkey_binding_press/_release after #3075 #3112, fixed in perf(bench): drop the port_comparison stand-ins obs-hotkey now exports (#3112) #3113 (merged 2026-10-30T23:48Z): the stand-ins are gone andci/test_bench_libobs_stand_ins.pynow fails if a future port exports a name the bench still defines.u8reads + twou32writes per output pixel, with the C's two adjacentuint32_tstores emitted as one 8-byte write; writes stay offset-addressed because output rows can overlap (media-io: decompress_422() converts twice the row it is given #3031). All 284obs-media-iolib tests pass, including the C parity cases and the overlap fixture.record.pyruns —uyvx_to_i4201.30 vs 3.62 — so the per-case spread swamps the change. Best-of-twoi420_to_uyvx1.43 -> 0.93 andyuy2_to_uyvx1.28 -> 1.02 do repeat;nv12_to_uyvx1.29 -> 1.17 improved but is not at the bound yet.What it needs from a maintainer, both small: a quiet
record.py --category formaton the 5950X baseline machine, and a decision on whether #3114 should also add the missing ratio gate torecord.py(it recordsthreshold_rust_over_c: 1.10but asserts nothing, so #3111's "failing-test sketch" has no home yet). Claim is held; I will re-measure on a quieter tick and take thenv12_to_uyvxsecond pass if it still misses.zackees commentedon Oct 31, 2026
Blocked: the fix is written and its parity tests pass (#3114, still a draft), and what remains is one quiet measurement.
record.py --category formaton this worker host cannot decide it: the box runs four worker checkouts (load average 27 at 17:16Z), the untouched compress cases moved 25% between two runs, anduyvy_to_uyvx"regressed" whileyuy2_to_uyvx(the same code) improved. The two signals that repeat —i420_to_uyvx1.43 → 0.93 andyuy2_to_uyvx1.28 → 1.02 — are both at or below the 1.10 bound;nv12_to_uyvximproved 1.29 → 1.17 and may need a second pass.Needed from the maintainer: one run of
record.py --category formaton the recorded Linux baseline machine (5950X-class), or a window where this host is otherwise idle.PR #3114is otherwise complete: parity green (284 tests,format_conversion_parityincluded), no test or public header changed.perf(media-io): read a decompress row once instead of per pixel (#3111)
zackees commentedon Oct 31, 2026
Blocked: one more measurement attempt on master, and the host still cannot decide the bound.
Worker tick (
obs-studio4), onorigin/master88d49a917(the commit #3114 merged into),record.py --category format --repeats 3 --cpu 15, Ryzen 7 3700X, load average 8-15 during the run. Ratios are Rust/C medians with the range across the three complete runs:format/i420_to_uyvx/1080pformat/nv12_to_uyvx/1080pformat/yuy2_to_uyvx/1080pformat/uyvy_to_uyvx/1080pformat/uyvx_to_i420/1080p(untouched compress case)The untouched compress case moved 2.79-4.33 across three runs on the same binary, so the spread is host load, not code: this run cannot be used as before/after evidence in either direction. It also disagrees with the PR's own two runs (i420 0.93 there, 1.63 here), which is the same noise seen from the other side.
nv12_to_uyvxis the only case whose median stays above 1.10 here, but its own range crosses 1.0.Nothing to land: #3114 already carries the row-hoist fix and its parity tests, and a second pass on
decompress_nv12would need a measurement that can distinguish it from this noise. Still needed: one run ofrecord.py --category formaton the recorded 5950X-class Linux baseline host (or a window where this box is otherwise idle), then a decision on whethernv12_to_uyvxneeds the second pass.zackees commentedon Oct 31, 2026
Progress — a correction to this issue's premise, measured.
Worker tick (
obs-studio4), same host as my 06:51Z comment (Ryzen 7 3700X, GCC 15.2.0), but with the oracle at the shipped optimization level: #3130 clamps theobs-c-oracleC to-O2(RelWithDebInfo) instead of the bench profile's-O3, which vectorized loops the shipped C leaves scalar. That is the run my earlier ratios were measured against, so they overstated the gap.uv run rust/tools/benchmarks/record.py --cpu 7 --repeats 5 --category format --output ...ondea01fd00(#3130's head),c_flags: -O2 -ffp-contract=off -fno-strict-aliasing, median Rust/C over the five paired repeats:format/i420_to_uyvx/1080pformat/nv12_to_uyvx/1080pformat/uyvy_to_uyvx/1080pformat/yuy2_to_uyvx/1080pAll four decompress-to-UYVX paths are now at or within 5 % of the 1.10 bound (#3114), and
i420_to_uyvxis faster than the shipped C. The 1.5-3.2x in this issue's title was measured against a-O3oracle, i.e. against a C nobody ships.The direction that is still real is the other one, compress from UYVX, which this issue does not cover:
format/uyvx_to_i444/1080pformat/uyvx_to_i420/1080pformat/uyvx_to_nv12/1080pThat is #3004's territory (SIMD conversion where the C is SSE2), not this issue's per-pixel
Source::read/Sink::writecost.What I need from a maintainer: whether to close this as a measurement artifact of the
-O3oracle (once #3130 lands) and keep the compress gaps in #3004, or to keep it open and retarget it at the compress direction. I have not closed it myself, and #3114 stays as it is; its parity tests are valid either way.perf(bench): drop the port_comparison stand-ins obs-hotkey now exports (
perf(media-io): read a decompress row once instead of per pixel (obs-…
perf(media-io): borrow each UYVX block instead of copying it (obs-rus…
zackees commentedon Nov 7, 2026
Claiming: investigation — pool slot obs-studio3, remeasure current decompress paths after #3114 and the shipped -O2 oracle correction (#3130). No active assignee or competing decompress PR found. Plan: (1) record three pinned format repeats on this Ryzen host; (2) only pursue the slowest one or two paths if the measurement distinguishes a real gap; (3) preserve bit-exact parity and land nothing below a measured ~10% gain. Read #3176 and closed #3667/#3004 evidence; no weaker acceptance bound or target/cache pruning. First PR only after a reproducible target and RED evidence.
zackees commentedon Nov 7, 2026
Releasing #3111 investigation — no code or PR; current host cannot establish a remaining optimization target reliably.
Read #3176, #3667 and #3004 notes, checked claims and open/merged perf PRs. #3114 already landed the input-row hoist; #3130 corrected the C oracle from -O3 to shipped -O2. On current master 13d19fb, two back-to-back unchanged
record.py --cpu 7 --repeats 3 --category formatinvocations (Ryzen 7 3700X, GCC 15.2.0, Rust 1.98.1, oracle -O2 -ffp-contract=off -fno-strict-aliasing):Metric: existing recorder median Rust time / median C time over three complete repeats. Both commands exit 0 and run the benchmark's whole-output equality checks against C and C-scalar before timing. No --max-ratio acceptance gate was claimed. UYVY was the first run's slowest path; its ratio improved 13.7% without editing anything. NV12 moved the opposite way by 17.2%. Host load average observed 31–63 on 16 logical CPUs; affinity does not isolate its SMT sibling or competing tasks.
BLOCKED for this pool assignment: original 1.5–3.2x premise does not reproduce on current master, and these measurements cannot distinguish the requested ~10% optimization from host variation. No candidate was accepted, no threshold weakened, no source/test/header/plugin/workflow changes, and no target pruning or soldr gc. Need an idle/reserved physical core or quiet named host to establish a remaining gap before RED/GREEN work. No new Tier 3 suite run or ARM acceptance measurement is claimed. Raw JSON is retained with the pool response (i3111-baseline.json and i3111-baseline-repeat.json).