Skip to content

PNG: Avoid allocations when disposing APNG frames - #3102

Merged
197g merged 2 commits into
image-rs:mainfrom
pnn64:main
Aug 26, 2026
Merged

PNG: Avoid allocations when disposing APNG frames#3102
197g merged 2 commits into
image-rs:mainfrom
pnn64:main

Conversation

@pnn64

@pnn64 pnn64 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

This PR changes APNG background and previous-frame disposal to operate directly on the underlying image rows. Previously, these paths created temporary buffers before clearing or restoring a region, resulting in unnecessary allocations and memory copies. The new implementation performs the same work directly in the frame buffers without changing the public API or using unsafe code.

The existing six WPT APNG reference tests pass. Local measurements produced the following results:

Disposal mode Allocations Allocated bytes Reallocations Latency
Background 20 → 19 331,504 → 306,928 10 → 1 ~30% lower
Previous 20 → 19 315,120 → 306,928 1 → 1 ~16% lower

The benchmarks were run locally and are not included in this PR.

Tested with cargo test --test reference_images -- wpt and cargo clippy --lib -- -D warnings.

AI disclosure: I used Codex with GPT-5.6 Sol at xhigh reasoning effort to help identify the allocation, implement the optimization, and construct the local benchmarks. I reviewed and tested the resulting change.

Comment thread src/codecs/png.rs Outdated
@197g
197g merged commit a20d0bb into image-rs:main Aug 26, 2026
23 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants