Skip to content

obs-graphics: Speed up the LZW decoder's stack decode (#2504) - #2505

Merged
zackees merged 2 commits into
masterfrom
perf/2504-lzw-decode-stack
Oct 26, 2026
Merged

zackees merged 2 commits into
masterfrom
perf/2504-lzw-decode-stack

Conversation

@vpacher

@vpacher vpacher commented Oct 26, 2026

Copy link
Copy Markdown
Member

Fixes #2504

The Rust LZW decoder of #2466 was 18.5 % slower than the C through lzw_decode (the output stack, which libnsgif uses for interlaced and clipped frames). This PR adds the missing bench and removes the gap, without a change in results. Promised in #2503.

What changed

  1. 3176a9ee5 Bench: lzw/decode_stack_1080p in port_comparison decodes the 1080p stream of lzw/decode_map_1080p through lzw_decode until the end of the data. Before the fix: Rust 10.50 ms, C 8.86 ms.

  2. 4c9dc6b75 Fix in rust/obs-graphics/src/lzw.rs, three changes, each measured on its own:

    • The writer is a generic parameter (trait Out with StackOut and MapOut) in place of an enum matched for every value, so each writer has its own loop, as the C passes its writer as a separate inline function. Stack: 10.50 → 10.37 ms.
    • Table and stack indices are masked to the table size. Codes are at most LZW_CODE_MAX (12) bits, and so are the extends links made from them; a stack position is below used + count, at most 4096. The mask changes no index and lets the compiler drop the bounds checks in the value loop. Stack: 10.37 → 9.65 ms.
    • The code reader's fast path reads its three bytes in one slice; the old byte-by-byte read (0 past the buffer) stays for where the slice does not fit. read_code was about a quarter of the profile. Stack: 9.65 → 9.11 ms.

    An #[inline] on the hot functions changed nothing (the compiler already inlines them) and is not in the PR.

Results

port_comparison (M1 Max, three runs after the fix):

Rust C Before
lzw/decode_stack_1080p 9.07 to 9.36 ms 8.99 to 9.13 ms +18.5 %
lzw/decode_map_1080p 9.74 to 10.09 ms 10.35 to 10.71 ms +2.5 %

No change in results: Tier 1 render::lzw (15/15) and Tier 3 render::lzw_parity (10/10, with 20 000 random call sequences against the unmodified C) pass unchanged, and so do the cargo image_file tests.

Tier 2 (Linux x86_64, Debian 13)

  • Lint lane (ci/local_gate.py --lane lint) passes.
  • test_nsgif_decode, test_nsgif_decode_rust, test_image_file, test_image_file_version, test_image_file_version_filetime and test_media_gif_last_frame pass on the hybrid libobs and on libobs-c-reference (6/6 each).

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: not applicable (a performance fix; the bench comes first, and the existing Tier 1 and Tier 3 tests show that results do not change)
  • User-visible change: not applicable (faster GIF decode, same pixels)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes: not applicable
  • Intentional differences from C: none added

🤖 Generated with Claude Code

vpacher and others added 2 commits October 26, 2026 17:55
port_comparison measured the LZW decoder of #2466 only through
lzw_decode_map, the full-width frame. An interlaced or clipped frame
decodes through lzw_decode and the output stack (the complex path of
libnsgif's frame decode, #2501). lzw/decode_stack_1080p decodes the same
1080p stream through lzw_decode until the end of the data.

M1 Max, before the fix: Rust 10.50 ms, C 8.86 ms (+18.5 %);
lzw/decode_map_1080p Rust 10.41 ms, C 10.16 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…2504)

Three changes, each measured on its own, no change in results:

- The writer (output stack or colour map) is a generic parameter
  (trait Out with StackOut and MapOut) in place of an enum matched for
  every value, so each writer has its own loop, as the C passes its
  writer as a separate inline function.
- Table and stack indices are masked to the table size. Codes are at most
  LZW_CODE_MAX (12) bits, and so are the extends links made from them;
  a stack position is below used + count, at most 4096. The mask changes
  no index and lets the compiler drop the bounds checks in the value loop.
- The code reader's fast path reads its three bytes in one slice, with
  the old byte-by-byte read (0 past the buffer) only where the slice does
  not fit.

port_comparison (M1 Max, three runs): lzw/decode_stack_1080p Rust 9.07
to 9.36 ms, C 8.99 to 9.13 ms (was +18.5 %); lzw/decode_map_1080p Rust
9.74 to 10.09 ms, C 10.35 to 10.71 ms (was +2.5 %). Tier 1 render::lzw
and Tier 3 render::lzw_parity (20 000 random call sequences against the
unmodified C) pass unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vpacher
vpacher requested a review from zackees as a code owner October 26, 2026 16:55
@zackees

zackees commented Oct 26, 2026

Copy link
Copy Markdown
Member

Coordinator review: a performance-only change to the Rust LZW decoder. The output writer is generic instead of an enum, and the new unsafe is limited to the bench's FFI calls. The parity tests against the C are unchanged and still pass, and the bench numbers for each step are posted. Queuing.

@zackees
zackees enabled auto-merge October 26, 2026 17:02
@zackees
zackees added this pull request to the merge queue Oct 26, 2026
Merged via the queue into master with commit 0eb612a Oct 26, 2026
36 checks passed
@zackees
zackees deleted the perf/2504-lzw-decode-stack branch October 26, 2026 17:16
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.

perf: obs-graphics: lzw_decode (output stack) is 18 % slower than the C

2 participants