Skip to content

Buffer API changes, Optimization and refactor of ResizeProcessors - #141

Merged
antonfirsov merged 13 commits into
masterfrom
speedup-resize
Mar 21, 2017
Merged

antonfirsov merged 13 commits into
masterfrom
speedup-resize

Conversation

@antonfirsov

@antonfirsov antonfirsov commented Mar 18, 2017 •

Copy link
Copy Markdown
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following matches the existing coding patterns and practise as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Buffer API changes

  • Refactored BufferPointer<T> to BufferSpan<T>. It's API is very similar to System.Span now.
  • Introduced PinnedImageBuffer<T>: a specialization of PinnedBuffer<T> that holds 2D area of data. It's very similar to PixelAccessor<T>, but the elements are not constrained to be IPixel<T>-s.
  • Introduced an IPinnedImageBuffer<T> interface for common functionalities of PixelAcccessor<T> and PinnedImageBuffer<T>. (See PinnedImageBufferExtensions)

ResizeProcessor changes

  • Implemented first 2 points of Improve ResizeProcessor performance #139
    • Weights data is stored in a contigous memory area now (see WeightsWindow and WeightsBuffer)
    • In the final implementation the execution time dropped about 30%
  • Merged ResizeProcessor and CompandResizeProcessor
  • Removed several code duplications

Benchmarks results

CPU specs:
Processor=Intel(R) Core(TM) i7-4810MQ CPU 2.80GHz, ProcessorCount=8
(4 physical cores)

Before:

Method Mean StdDev Scaled Scaled-StdDev Allocated
'System.Drawing Resize' 52.4481 ms 0.7776 ms 1.00 0.00 512 B
'ImageSharp Resize' 35.2544 ms 1.1755 ms 0.67 0.02 17 MB
'ImageSharp Compand Resize' 65.6250 ms 1.3946 ms 1.25 0.03 17 MB

After:

Method Mean StdErr StdDev Scaled Scaled-StdDev Allocated
'System.Drawing Resize' 51.4692 ms 0.1478 ms 0.5330 ms 1.00 0.00 512 B
'ImageSharp Resize' 24.4749 ms 0.3067 ms 1.2647 ms 0.48 0.02 17.02 MB
'ImageSharp Compand Resize' 43.8705 ms 0.1408 ms 0.5451 ms 0.85 0.01 17.02 MB

A fully sequential version of "after" on the same machine (no Parallel.For-s at all):

Method Mean StdDev Median Scaled Scaled-StdDev Allocated
'System.Drawing Resize' 52.3989 ms 1.3285 ms 52.0098 ms 1.00 0.00 512 B
'ImageSharp Resize' 58.7112 ms 1.5875 ms 57.6037 ms 1.12 0.04 16.9 MB
'ImageSharp Compand Resize' 149.1699 ms 0.1799 ms 149.1820 ms 2.85 0.07 16.9 MB

Conclusion: considering that I have 4 physical cores, the 2.85/0.85 = 3.35 parallel speedup is quite good on the Compand stuff. It means, we should find an efficient way to keep the parallelism for non-server users.

@antonfirsov antonfirsov changed the title Additional buffer features, Optimization and refactor of ResizeProcessors Buffer API changes, Optimization and refactor of ResizeProcessors Mar 18, 2017
@codecov-io

codecov-io commented Mar 18, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #141 into master will increase coverage by 0.1%.
The diff coverage is 93.61%.

@@            Coverage Diff            @@
##           master     #141     +/-   ##
=========================================
+ Coverage   87.72%   87.83%   +0.1%     
=========================================
  Files         455      463      +8     
  Lines       19789    20041    +252     
  Branches     1474     1478      +4     
=========================================
+ Hits        17360    17603    +243     
- Misses       2020     2032     +12     
+ Partials      409      406      -3
Impacted Files Coverage Δ
src/ImageSharp/Image/PixelArea{TColor}.cs 78.57% <ø> (ø) ⬆️
...ts/Processors/Filters/ResizeProfilingBenchmarks.cs 0% <0%> (ø)
...harp.Drawing/Brushes/Processors/BrushApplicator.cs 21.05% <0%> (ø) ⬆️
...rocessing/Processors/Transforms/ResizeProcessor.cs 96.58% <100%> (+0.66%) ⬆️
tests/ImageSharp.Tests/Common/PinnedBufferTests.cs 100% <100%> (ø) ⬆️
src/ImageSharp/Processing/Transforms/Resize.cs 69.56% <100%> (-2.44%) ⬇️
tests/ImageSharp.Tests/Common/BufferSpanTests.cs 100% <100%> (ø)
...ImageSharp.Drawing/Brushes/RecolorBrush{TColor}.cs 98.11% <100%> (ø) ⬆️
.../Colors/PackedPixel/BulkPixelOperations{TColor}.cs 100% <100%> (ø) ⬆️
src/ImageSharp/Image/PixelAccessor{TColor}.cs 82.48% <100%> (ø) ⬆️
... and 32 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2d8836b...cd1e7b3. Read the comment docs.

@tocsoft

tocsoft commented Mar 19, 2017

Copy link
Copy Markdown
Member

I can't see anything thing in here that worries me. Looking good 👍

@JimBobSquarePants JimBobSquarePants left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great. I really like the way you've restructured the weights buffer!

Just to note, the reason for the duplication before was that the compand if inside the loop was actually a significant factor on performance within the resize algorithm. I don't know if that is still the case.

That output table you produced is very interesting.

/// <param name="destinationSize">The size of the destination window</param>
public WeightsBuffer(int sourceSize, int destinationSize)
{
this.dataBuffer = new PinnedImageBuffer<float>(sourceSize, destinationSize);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason for not using the static method CreateClean to generate the clean buffer?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No it was occasional. Could be more readable with CreateClean().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool. I would say so. Intent is really obviously then.

@antonfirsov

Copy link
Copy Markdown
Member Author

I think it's enough to move out if-s from the inner loop. We can benchmark a version where they are outside the outer loops, but I don't think it's worth the efforts.

@JimBobSquarePants

JimBobSquarePants commented Mar 20, 2017 •

Copy link
Copy Markdown
Member

I'll have a wee go just in case. Scratch that, didn't read your comment properly. Let's merge this! :shipit:

@antonfirsov
antonfirsov merged commit c004204 into master Mar 21, 2017
@antonfirsov antonfirsov mentioned this pull request Mar 21, 2017
6 of 9 tasks
@antonfirsov
antonfirsov deleted the speedup-resize branch March 27, 2017 23:10
antonfirsov added a commit to antonfirsov/ImageSharp that referenced this pull request Nov 11, 2019
Buffer API changes, Optimization and refactor of ResizeProcessors
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.

4 participants