Skip to content

perf: obs-media-io: the Rust decompress paths are 1.5-3.2x slower than the scalar C, and the cost is per-pixel Source::read/Sink::write #3111

Open

Description

@zackees

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, one u16 read per 2 pixels instead of two u8 reads.
  • 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

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

Activity

  1. added
    enhancement
    P3
    size:M
    on Oct 30, 2026
  2. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Claiming: investigation — obs-media-io decompress fast path (#3111)

    Plan:

    1. Reproduce the four format/*_to_uyvx/1080p Rust/C ratios on this host with record.py --category format (Linux x86_64).
    2. Restructure decompress_420 / decompress_nv12 / decompress_422 so the row pair is hoisted out of the pixel loop and the per-pixel generic Source/Sink calls 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).
    3. RED: a ratio bound in the format bench category (Rust/C > 1.10 fails); GREEN: the restructured loops, format_conversion_parity.rs unchanged, before/after ratios in the PR.

    I will /ready this after step 1 records the baseline.

  3. added
    needs-investigation
    on Oct 30, 2026
  4. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Progress — the measurement path was broken, now fixed; the rewrite is up as a draft.

    1. record.py could not run at all on master. libobs: Port the hotkey binding press count to obs-hotkey (#1836) #3075 exported obs_hotkey_binding_press()/_release() from obs-hotkey while the port_comparison bench 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, and lld-link the 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 and ci/test_bench_libobs_stand_ins.py now fails if a future port exports a name the bench still defines.
    2. The decompress rewrite is up as a draft, perf(media-io): read a decompress row once instead of per pixel (#3111) #3114. One row slice per row instead of two u8 reads + two u32 writes per output pixel, with the C's two adjacent uint32_t stores 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 284 obs-media-io lib tests pass, including the C parity cases and the overlap fixture.
    3. The ≤ 1.10 bound is not demonstrated, which is why perf(media-io): read a decompress row once instead of per pixel (#3111) #3114 is a draft. On this worker host (Ryzen 7 3700X, four concurrent worker checkouts) the untouched compress cases move 25% between two record.py runs — uyvx_to_i420 1.30 vs 3.62 — so the per-case spread swamps the change. Best-of-two i420_to_uyvx 1.43 -> 0.93 and yuy2_to_uyvx 1.28 -> 1.02 do repeat; nv12_to_uyvx 1.29 -> 1.17 improved but is not at the bound yet.

    What it needs from a maintainer, both small: a quiet record.py --category format on the 5950X baseline machine, and a decision on whether #3114 should also add the missing ratio gate to record.py (it records threshold_rust_over_c: 1.10 but 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 the nv12_to_uyvx second pass if it still misses.

  5. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Blocked: the fix is written and its parity tests pass (#3114, still a draft), and what remains is one quiet measurement.

    record.py --category format on 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, and uyvy_to_uyvx "regressed" while yuy2_to_uyvx (the same code) improved. The two signals that repeat — i420_to_uyvx 1.43 → 0.93 and yuy2_to_uyvx 1.28 → 1.02 — are both at or below the 1.10 bound; nv12_to_uyvx improved 1.29 → 1.17 and may need a second pass.

    Needed from the maintainer: one run of record.py --category format on the recorded Linux baseline machine (5950X-class), or a window where this host is otherwise idle. PR #3114 is otherwise complete: parity green (284 tests, format_conversion_parity included), no test or public header changed.

  6. added
    claim-blocked
    and removed
    claim-blocked
    on Oct 31, 2026
  7. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Blocked: one more measurement attempt on master, and the host still cannot decide the bound.

    Worker tick (obs-studio4), on origin/master 88d49a917 (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:

    Case median run range
    format/i420_to_uyvx/1080p 1.63 1.63-2.02
    format/nv12_to_uyvx/1080p 1.35 0.85-1.99
    format/yuy2_to_uyvx/1080p 3.54 2.82-3.54
    format/uyvy_to_uyvx/1080p 2.68 2.41-3.88
    format/uyvx_to_i420/1080p (untouched compress case) 3.68 2.79-4.33

    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_uyvx is 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_nv12 would need a measurement that can distinguish it from this noise. Still needed: one run of record.py --category format on the recorded 5950X-class Linux baseline host (or a window where this box is otherwise idle), then a decision on whether nv12_to_uyvx needs the second pass.

  8. added
    claim-blocked
    on Oct 31, 2026
  9. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    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 the obs-c-oracle C 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 ... on dea01fd00 (#3130's head), c_flags: -O2 -ffp-contract=off -fno-strict-aliasing, median Rust/C over the five paired repeats:

    Case Rust/C C ns Rust ns
    format/i420_to_uyvx/1080p 0.60 1,687,837 1,016,576
    format/nv12_to_uyvx/1080p 1.04 1,428,661 1,489,168
    format/uyvy_to_uyvx/1080p 1.06 1,674,597 1,769,158
    format/yuy2_to_uyvx/1080p 1.15 1,555,484 1,785,043

    All four decompress-to-UYVX paths are now at or within 5 % of the 1.10 bound (#3114), and i420_to_uyvx is faster than the shipped C. The 1.5-3.2x in this issue's title was measured against a -O3 oracle, 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:

    Case Rust/C C ns Rust ns
    format/uyvx_to_i444/1080p 2.09 1,612,911 3,364,717
    format/uyvx_to_i420/1080p 2.52 1,205,264 3,036,962
    format/uyvx_to_nv12/1080p 3.88 810,190 3,141,503

    That is #3004's territory (SIMD conversion where the C is SSE2), not this issue's per-pixel Source::read/Sink::write cost.

    What I need from a maintainer: whether to close this as a measurement artifact of the -O3 oracle (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.

  10. removed
    claim-blocked
    on Oct 31, 2026
  11. removed their assignment
    on Nov 1, 2026
  12. zackees commented on Nov 7, 2026

    @zackees
    MemberAuthor

    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.

  13. zackees commented on Nov 7, 2026

    @zackees
    MemberAuthor

    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 format invocations (Ryzen 7 3700X, GCC 15.2.0, Rust 1.98.1, oracle -O2 -ffp-contract=off -fno-strict-aliasing):

    1080p path first Rust/C aggregate second unchanged aggregate first paired range second paired range
    I420→UYVX 1.0399 1.0243 0.9161–1.0652 0.9613–1.2883
    NV12→UYVX 0.9862 1.1554 0.8762–1.2518 1.0060–1.2984
    UYVY→UYVX 1.2279 1.0598 1.1483–1.3620 0.8504–1.3587
    YUY2→UYVX 0.9817 1.1211 0.6263–1.3265 1.0083–1.1856

    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).

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

    P3enhancementneeds-investigationsize:M

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions