Skip to content

Reduce the number of memory allocations in lossless WebP encoder #2940

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

Merged
merged 8 commits into from
Jun 13, 2025

Conversation

SladeThe
Copy link
Contributor

@SladeThe SladeThe commented Jun 6, 2025

This PR closes #2934

The results of benchmark #55 EncodeWebp:

Before

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
'ImageSharp Webp Lossless' 105.31 ms 24.582 ms 1.347 ms 2000.0000 1800.0000 1400.0000 20490.55 KB

After

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
'ImageSharp Webp Lossless' 100.61 ms 15.196 ms 0.833 ms 2000.0000 2000.0000 2000.0000 3904.68 KB

Pin the refs (without the capacity guard)

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
'ImageSharp Webp Lossless' 97.43 ms 9.375 ms 0.514 ms 2000.0000 2000.0000 2000.0000 3904.98 KB

@CLAassistant
Copy link

CLAassistant commented Jun 6, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@JimBobSquarePants JimBobSquarePants left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick read so far but looking good. I'll have to pull it down to review properly.

{
PixOrCopy v = backwardRefsEnumerator.Current;
int ix = ((y >> histoBits) * histoXSize) + (x >> histoBits);
histograms[ix].AddSinglePixOrCopy(v, false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method could be updated to take the struct via in to avoid the copy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the change. But if there is any performance difference, it's hard to notice. The structure is not that big.

Copy link
Member

@antonfirsov antonfirsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good after skimming through all changes.

@JimBobSquarePants
Copy link
Member

I'm thinking of backporting this to V3. @antonfirsov what do you think?

@antonfirsov
Copy link
Member

The change looks simple and safe enough to backport.

@JimBobSquarePants JimBobSquarePants merged commit 1e58ba9 into SixLabors:main Jun 13, 2025
8 checks passed
@JimBobSquarePants
Copy link
Member

Thanks for this @SladeThe it's very much appreciated!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lossless WebP encoder allocates A LOT
4 participants