Repository navigation
obs-graphics: Speed up the LZW decoder's stack decode (#2504) - #2505
Merged
Merged
Conversation
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]>
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
3176a9ee5Bench:lzw/decode_stack_1080pinport_comparisondecodes the 1080p stream oflzw/decode_map_1080pthroughlzw_decodeuntil the end of the data. Before the fix: Rust 10.50 ms, C 8.86 ms.4c9dc6b75Fix inrust/obs-graphics/src/lzw.rs, three changes, each measured on its own:OutwithStackOutandMapOut) 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.LZW_CODE_MAX(12) bits, and so are theextendslinks made from them; a stack position is belowused + 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.read_codewas 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):lzw/decode_stack_1080plzw/decode_map_1080pNo change in results: Tier 1
render::lzw(15/15) and Tier 3render::lzw_parity(10/10, with 20 000 random call sequences against the unmodified C) pass unchanged, and so do the cargoimage_filetests.Tier 2 (Linux x86_64, Debian 13)
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_filetimeandtest_media_gif_last_framepass on the hybrid libobs and onlibobs-c-reference(6/6 each).Checklist
master🤖 Generated with Claude Code