Uh oh!
There was an error while loading. Please reload this page.
Hoist some of the calculations from loops of 3DMoments() - #1818
Conversation
Codecov Report
@@ Coverage Diff @@## master #1818 +/- ##
=======================================
Coverage 87% 87% =======================================
Files 936 936 Lines 48164 48191 +27 Branches 6037 6037 =======================================
+ Hits 41970 42097 +127 + Misses 5190 5093 -97 + Partials 1004 1001 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
JimBobSquarePants
commented
Nov 11, 2021
@kunalspathak Thanks for this it is very interesting. I've just read your linked runtime issue also. If we can add some supplementary comments to explain the optimization and refactor some of the variable naming (prefer |
kunalspathak
commented
Nov 11, 2021
I have also noticed other places where similar technique could be use although I didn't measure its performance.
, once of the reference is : Here, Another interesting method is |
| { | ||
| using var memoryStream = new MemoryStream(); | ||
| var options = new PngEncoder { Quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }) }; | ||
| var options = new PngEncoder { Quantizer = new WuQuantizer(new QuantizerOptions { Dither = null }), ColorType = PngColorType.Palette }; |
There was a problem hiding this comment.
I made this change so I can get perf coverage for it. Should I keep it or revert it?
There was a problem hiding this comment.
Yep. That's an oversight from us.
Very happy to see what other optimizations you can bring as long as they are well commented. Thanks! 👍
There was a problem hiding this comment.
Sure, happy to contribute. I think it will be more productive for us to contribute or investigate the performance issues if these benchmarks are in our system. Any progress on #1795?
There was a problem hiding this comment.
Leaving that one for @antonfirsov Likely after we complete #1730 and release V2.
kunalspathak
commented
Nov 12, 2021
This should be ready for review now. |
kunalspathak
commented
May 15, 2022
This workaround will not be needed anymore because codegen will take care of it as part of dotnet/runtime#68061. Here is the diff for the earlier version of C# code: Full diff: https://www.diffchecker.com/DKGAZKq0 |
JimBobSquarePants
commented
May 15, 2022
That’s .NET 7 though yeah? We’ll be sticking to LTS release targets from now on so just 6 for now. I am planning on revising many parts of the codebase though to simplify things where possible now we have a much more modern single target. |
kunalspathak
commented
May 15, 2022
Yes.
Sure, this was just FYI :) |
JimBobSquarePants
commented
May 15, 2022
It’s really good to know!! |

Prerequisites
Description
The 4-level nested for loop in 3DMoments is very expensive, the inner most loop executes almost 1.1M times. We call
GetPaletteIndex()method that does bunch of calculation using index variables of all the 4-loops.ImageSharp/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs
Lines 418 to 444 in 255226b
There is a potential to hoist some of the invariants calculation outside the loop. I verified the benchmark and see around 2-3% improvement.
Here is the assembly difference: https://www.diffchecker.com/I4uqCtBO