Repository navigation
perf(gif): decode a gif once across rayon workers - #239
Merged
Merged
Conversation
53 tasks
LeadcodeDev
force-pushed
the
perf/gif-decode-stampede
branch
2 times, most recently
from
September 22, 2026 08:34
4d15a91 to
c89ddad
Compare
Triggering scenario: a scenario with one gif component, rendered to MP4. encode_with_ffmpeg (ffmpeg.rs:582) dispatches the FIRST chunk of batch_size = rayon::current_num_threads() * 2 frames — all frames 0..N of the same scene — to par_iter() at once. Every one of those workers calls Gif::paint_content, every one misses the still-empty gif_cache, and every one runs decode_composed_frames independently. Unlike icons and videos, GIFs are NOT preloaded (preload.rs only walks Component::Icon and Component::Video), so the first decode genuinely happens inside the parallel section. Each decode is itself unbounded: decode_composed_frames pushes one full-canvas RGBA copy per GIF frame with no cap (gif.rs:141). A 600-frame 800x600 GIF is 1.15 GB per decode; on a 16-core machine (batch_size 32) that is ~37 GB attempted concurrently, then 31 of the 32 results are thrown away when insert overwrites. The process OOM-kills before frame 33. Fix: Decode GIFs in the preload pass alongside icons and videos (preload.rs), so the parallel section only ever hits a warm cache. If lazy decode must stay, gate it on a per-key single-flight primitive (e.g. DashMap<String, Arc<OnceLock<GifData>>> — insert the empty cell under the shard lock, then let exactly one worker fill it) and cap total decoded frames per source. Refs #220
LeadcodeDev
force-pushed
the
perf/gif-decode-stampede
branch
from
September 22, 2026 08:44
c89ddad to
1c12b20
Compare
LeadcodeDev
added a commit
that referenced
this pull request
Sep 22, 2026
Triggering scenario: a scenario with one gif component, rendered to MP4. encode_with_ffmpeg (ffmpeg.rs:582) dispatches the FIRST chunk of batch_size = rayon::current_num_threads() * 2 frames — all frames 0..N of the same scene — to par_iter() at once. Every one of those workers calls Gif::paint_content, every one misses the still-empty gif_cache, and every one runs decode_composed_frames independently. Unlike icons and videos, GIFs are NOT preloaded (preload.rs only walks Component::Icon and Component::Video), so the first decode genuinely happens inside the parallel section. Each decode is itself unbounded: decode_composed_frames pushes one full-canvas RGBA copy per GIF frame with no cap (gif.rs:141). A 600-frame 800x600 GIF is 1.15 GB per decode; on a 16-core machine (batch_size 32) that is ~37 GB attempted concurrently, then 31 of the 32 results are thrown away when insert overwrites. The process OOM-kills before frame 33. Fix: Decode GIFs in the preload pass alongside icons and videos (preload.rs), so the parallel section only ever hits a warm cache. If lazy decode must stay, gate it on a per-key single-flight primitive (e.g. DashMap<String, Arc<OnceLock<GifData>>> — insert the empty cell under the shard lock, then let exactly one worker fill it) and cap total decoded frames per source. Refs #220
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.
Severity Medium, category quality. Location:
crates/rustmotion-components/src/gif.rs:176Impact
Triggering scenario: a scenario with one
gifcomponent, rendered to MP4.encode_with_ffmpeg(ffmpeg.rs:582) dispatches the FIRST chunk ofbatch_size = rayon::current_num_threads() * 2frames — all frames 0..N of the same scene — topar_iter()at once. Every one of those workers callsGif::paint_content, every one misses the still-emptygif_cache, and every one runsdecode_composed_framesindependently. Unlike icons and videos, GIFs are NOT preloaded (preload.rsonly walksComponent::IconandComponent::Video), so the first decode genuinely happens inside the parallel section. Each decode is itself unbounded:decode_composed_framespushes one full-canvas RGBA copy per GIF frame with no cap (gif.rs:141). A 600-frame 800x600 GIF is 1.15 GB per decode; on a 16-core machine (batch_size 32) that is ~37 GB attempted concurrently, then 31 of the 32 results are thrown away wheninsertoverwrites. The process OOM-kills before frame 33.Fix
Decode GIFs in the preload pass alongside icons and videos (
preload.rs), so the parallel section only ever hits a warm cache. If lazy decode must stay, gate it on a per-key single-flight primitive (e.g.DashMap<String, Arc<OnceLock<GifData>>>— insert the empty cell under the shard lock, then let exactly one worker fill it) and cap total decoded frames per source.Evidence the audit read
Part of the September 2026 audit remediation chantier. Refs #220 (RM-20).