-
-
Notifications
You must be signed in to change notification settings - Fork 891
Limit ResizeProcessor memory consumption #888
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
Conversation
which has been renamed from CalculateResizeWorkerWindowCount()
JimBobSquarePants
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seriously impressive work here. I'm honestly amazed!
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <returns>The <see cref="Rectangle"/></returns> | ||
| public static Rectangle FullRectangle<T>(this Buffer2D<T> buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these methods here are very nice!
| this PixelConversionModifiers originalModifiers, | ||
| bool compand) => | ||
| compand | ||
| ? originalModifiers | PixelConversionModifiers.Scale | PixelConversionModifiers.SRgbCompand |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent! This solves the complexity issues
| /// When sliding the window, the contents of the bottom window band are copied to the new top band. | ||
| /// For more details, and visual explanation, see "ResizeWorker.pptx". | ||
| /// </summary> | ||
| internal class ResizeWorker<TPixel> : IDisposable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow... I would have never have come up with this!
| // Method | Job | Runtime | SourceToDest | Mean | Error | StdDev | Ratio | RatioSD | Gen 0/1k Op | Gen 1/1k Op | Gen 2/1k Op | Allocated Memory/Op | | ||
| // ----------------------------------------- |----- |-------- |------------- |----------:|----------:|----------:|------:|--------:|------------:|------------:|------------:|--------------------:| | ||
| // SystemDrawing | Clr | Clr | 3032-400 | 120.11 ms | 1.435 ms | 0.0786 ms | 1.00 | 0.00 | - | - | - | 1638 B | | ||
| // 'ImageSharp, MaxDegreeOfParallelism = 1' | Clr | Clr | 3032-400 | 75.32 ms | 34.143 ms | 1.8715 ms | 0.63 | 0.02 | - | - | - | 16384 B | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.NET Framework is faster?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also surprised, but happens quite often when you go full unsafe, dropping all the span indexers. It is also backed by RyuJIT.
| [InlineData(10, 1, 3, 6, 3)] | ||
| [InlineData(2, 2, 0, 1, 1)] | ||
| [InlineData(5, 1, 1, 3, 2)] | ||
| public void CopyColumns(int width, int height, int startIndex, int destIndex, int columnCount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good tests!
Codecov Report
@@ Coverage Diff @@
## master #888 +/- ##
==========================================
+ Coverage 88.98% 89.02% +0.04%
==========================================
Files 1018 1024 +6
Lines 44537 44786 +249
Branches 3220 3230 +10
==========================================
+ Hits 39632 39872 +240
- Misses 4194 4202 +8
- Partials 711 712 +1
Continue to review full report at Codecov.
|
Limit ResizeProcessor memory consumption
Prerequisites
Description
This PR fixes #642 by limiting the memory consumption of
ResizeProcessorto a value defined inConfiguration.WorkingBufferSizeHintInBytes. Instead of allocating the whole first pass buffer, a sliding window is implemented byResizeWorker<T>. The height of the window is a multiple of the vertical kernel's maximum diameter depending onConfiguration.WorkingBufferSizeHintInBytes.The implementation is most likely very different from MagicScaler's one (haven't seen it's code). It was a goal to keep the changes under control, the new code is basically a refactor of the old one.
The optimization is visually explained in ResizeWorker.pptx:
Results
There is no regression in speed. (Actually, we are faster now for .NET 4.72 than before.)
The resizing of a
4000 x 4000image to300 x 300has been memory profiled using JetBrains dotMemory. The resize operation has been executed 50 times in a single process.Memory profile before the optimization
Timeline:


Typical snapshot:

Memory profile after the optimization
Timeline:

(The second peak has been only triggered because I made the snapshot. Can anyone explain this?)
Typical snapshot:
