Skip to content

meta: perf: portable SIMD in the Rust port (tracking) #3009

Open
4 of 6 issues completed
Open

Description

@zackees

Context

Tracks the places where portable SIMD in Rust can make the port measurably faster. It comes from the tree survey on #756 (#756 (comment)) and #756's decisions. Claim an individual sub-issue, not this meta issue. GitHub's native sub-issue list tracks progress.

Rules (from #756)

  • Portable, stable Rust only. No std::simd and no nightly until it stabilizes on the pinned toolchain.
  • A scalar fallback plus runtime CPU detection. x86_64 (SSE2/AVX2) and aarch64 (NEON).
  • ISA-specific code only in the crate's src/platform/.
  • Tier 3 parity is bit-exact unless the C reference itself uses SIMD (docs/rust-port/testing-policy.md:296).
  • Benchmark first in rust/tools/benchmarks/record.py (chore: perf: benchmark the SIMD candidates in record.py (format conversion, volume meter, audio mix) #3003). Every kernel PR states before and after numbers on a named machine.
  • RED then GREEN, one kernel per PR.

Sub-issues, in order

  1. chore: perf: benchmark the SIMD candidates in record.py (format conversion, volume meter, audio mix) #3003: benchmark the candidates (format conversion, volume meter, audio mix). Blocks all the others. P1
  2. perf: obs-media-io: SIMD format conversion (Rust is scalar where the C is SSE2) #3004: SIMD format conversion in rust/obs-media-io. Runs every frame, and the Rust is scalar where the C is SSE2. Highest gain. P1
  3. perf: obs-audio-controls: SIMD volume meter peak and true peak #3005: volume meter peak and true peak. Every source, every tick; the C is SSE. P2
  4. perf: audio: verify or force vectorization of mix add and output clamp #3006: check the assembly and force vectorization of mix add and output clamp. Must stay bit-exact. P3
  5. perf: libobs: vectorize scene audio mixing and source volume multiply when ported #3007: vectorize scene audio mixing and source volume multiply when they are ported. Still C today. P3
  6. perf: guard: Rust replacements for swresample and swscale must ship with SIMD #3008: guard so that Rust replacements for swresample/swscale ship with SIMD from day one. P3

Considered, not worth it

  • Volume meter RMS (rust/obs-audio-controls/src/volmeter.rs:252): a float reduction against scalar C, so reordering breaks bit-exactness. Only revisit if the policy changes.
  • Dynamics filters, EQ (plugins/obs-filters): state carries from one sample to the next, they run only when a user adds them, and they aren't bit-exact. Gain filter and noise-suppress float/s16 conversion: low gain.
  • Downmix and PulseAudio monitor volume: low gain, per packet.
  • Frame, plane and encoder row copies: already memcpy.
  • GIF/LZW decode: inherently serial.
  • FreeType glyph blit, 3D LUT load: run only on user action.
  • Game-capture B5G6R5 copy: already SSE2, C only.

Refs #756, #1295, #668.

Sub-issues

Sub-issues

4 of 6 Issues completed
Loading 6 sub-issues

Activity

  1. added
    enhancement
    on Oct 30, 2026
  2. zackees commented on Oct 30, 2026

    @zackees
    MemberAuthor

    Baseline is in (#3029, which fixes #3003): Rust/C medians on a Ryzen 9 5950X (pinned) and an M1 Max. It changes the order:

    1. perf: obs-audio-controls: SIMD volume meter peak and true peak #3005, volume meter sample_peak, is now first. It is 7.2x slower than the SSE C on x86 (1.33x on aarch64) and runs for every source, every audio tick. The likely cause is that [f32; 4] with f32::max never becomes maxps (f32::max has its own NaN rule). The fix may be as small as matching the C's comparison, with no intrinsics at all. true_peak is already equal or faster.
    2. perf: obs-media-io: SIMD format conversion (Rust is scalar where the C is SSE2) #3004, format conversion, drops. None of the six functions has a caller in OBS anymore (upstream e5b004f removed CPU YUV conversion); they are exported API for plugins only. The compress side is 4-5x slower than SSE2 C but sits on no OBS path. The decompress side is 1.5-3.2x slower than scalar C, so the per-pixel Source::read/Sink::write is the cost, not missing SIMD. Rewriting it to work on whole rows is cheaper than SIMD and should come first, if at all. Moving it from P1 to P3.
    3. perf: audio: verify or force vectorization of mix add and output clamp #3006, mix add and clamp: close as not worth it. The mix add is equal on both machines, and the whole tick with the clamp is already 3.3x faster in Rust on x86.
    4. New: media-io: decompress_422() converts twice the row it is given #3031, a latent C bug in decompress_422 (it converts twice the row). No OBS caller.

    #3007 and #3008 are unchanged.

  3. vpacher commented on Oct 30, 2026

    @vpacher
    Member

    For information, not a baseline: the same three categories built for x86-64-v3 (AVX2, FMA, BMI2) on both sides, to see what a newer target alone would change. No release builds for it (there is no target-cpu or -march in the repo), so this does not change the order above.

    Setup: master 93b5e64, record.py --category format --category volmeter --category audio_mix --repeats 3 --cpu 27 on the Ryzen 9 5950X of #3029, two runs back to back: v1 with no flags (as shipped), v3 with RUSTFLAGS="-C target-cpu=x86-64-v3" and CFLAGS="-march=x86-64-v3" (both recorded in the JSON). The v1 run matches the #3029 baseline (uyvx_to_i420 3.98 against 4.00, sample_peak 7.17 against 7.16). With 3 repeats, differences under about 5 % are noise.

    Case Rust/C v1 Rust/C v3 Rust v3/v1 C v3/v1
    format/uyvx_to_i420 3.98 4.26 1.00 0.94
    format/uyvx_to_nv12 5.23 5.38 1.00 0.98
    format/uyvx_to_i444 3.88 3.90 1.00 1.00
    format/i420_to_uyvx 3.17 2.78 1.02 1.16
    format/nv12_to_uyvx 2.29 1.92 1.02 1.21
    format/uyvy_to_uyvx 1.52 1.11 0.62 0.85
    format/yuy2_to_uyvx 1.53 0.80 0.42 0.80
    volmeter/sample_peak/1024 7.17 4.56 0.63 1.00
    volmeter/true_peak/1024 0.97 0.76 0.79 1.00
    audio_mix/mix_whole_tick 1.04 0.93 0.83 0.92
    audio_mix/output_tick/stereo_6_mixes 0.30 0.25 0.82 0.99

    (v3/v1 below 1: that side is faster with v3.)

    What it shows:

    • perf: obs-media-io: SIMD format conversion (Rust is scalar where the C is SSE2) #3004 compress (uyvx_to_*): v3 changes nothing for the Rust (1.00). The 4 to 5 times gap is not the instruction set: the per-pixel Source::read/Sink::write keeps LLVM from vectorizing the loop at any level. Closing it needs a different loop, not a flag.
    • The 4:2:2 decompress vectorizes with AVX2: the Rust is 1.6 to 2.4 times faster with v3 and passes the C for YUY2 (0.80). LLVM can vectorize that loop when it has wider registers.
    • The I420 and NV12 decompress C gets 16 to 21 % slower with -march=x86-64-v3 (GCC); the Rust does not change. A newer -march is not a free gain for the C side either.
    • perf: obs-audio-controls: SIMD volume meter peak and true peak #3005 sample_peak: v3 makes the Rust 37 % faster but leaves it 4.6 times slower than the C, whose SSE intrinsics do not change. That fits the f32::max NaN rule as the cause; a newer target alone does not fix it.
    • Audio mix and tick: small gains on both sides.

    Windows (MSVC): the bench did not link on Windows (#3025); #3039 fixes that and adds docs/rust-port/benchmarks/baseline-2026-10-30-windows-x86_64.json (same CPU, MSVC 14.51, at the shipped target; under review, not merged yet). Rust/C there: the compress gap is the same as with GCC (uyvx_to_i420 4.16, uyvx_to_nv12 4.93, uyvx_to_i444 3.92); the scalar decompress C is slower with MSVC, so the Rust is equal or faster (nv12_to_uyvx 0.95, uyvy_to_uyvx 0.53, yuy2_to_uyvx 0.50); sample_peak is 3.73, true_peak 0.83. On MSVC the _c_scalar oracle is native SSE2 too, so both C columns are SSE2 there.

    So for the order above: no case for a different release target, #3005's fix belongs in the code (it is slower than the C with every compiler and target measured), and #3004's gap is structural.

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

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions