Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: avoid too large buffer allocations #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hannomargelo
Copy link

@hannomargelo hannomargelo commented Feb 20, 2025

I noticed that displaying an png was taking a significant amount of memory.

After some profiling I found too issues that cause too much memory allocations:

  1. The rows_frame and rows_buffer are png_bytep arrays. C++ will already allocate the elements to have a size of png_bytep. Us multiplying the height with the sizeof(png_bytep) is actually not needed.
    • (However, I think sizeof(png_bytep) returns 1 anyway. However, for correctness I added this change.
  2. The main improvement is to only pass height * width as frame byte size to the ApngFrame:
    • Before it was size = height * row_bytes where row_bytes is basically width * channels (4)
    • But the ApngFrames internal pixel array is already a uint32_t. Meaning one value already includes the precomputed value for all channels (see saveFrame).
    • Thus the ApngFrames internal pixel array is oversized by 4x. By changing it to width * height its size is correct

We had an extreme case where the animation (and rest of the app) before took ~1.7GB:

CleanShot 2025-02-20 at 10 34 29

With this change we were able to bring it down to ~700mb, a 2.3x improvement:

CleanShot 2025-02-20 at 10 36 15

Related to:

Important

This performance improvement was sponsored by Discord 🌟

@CLAassistant
Copy link

CLAassistant commented Feb 20, 2025

CLA assistant check
All committers have signed the CLA.

@hannomargelo
Copy link
Author

@CLAassistant recheck (signed)

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.

3 participants