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

PixelOperations<TPixel>.From<RgbaVector> speedup #1435

Merged
merged 1 commit into from
Nov 21, 2020

Conversation

Sergio0694
Copy link
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 practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

When using any of the generic Image.CloneAs<TPixel> APIs, if the target format was RgbaVector, the selected code paths was not optimized to take advantage of the fact that RgbaVector is blittable to Vector4. In particular, this PixelOperations<TPixel>.To method was called:

public virtual void From<TSourcePixel>(
Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels,
Span<TPixel> destinationPixels)
where TSourcePixel : unmanaged, IPixel<TSourcePixel>
{
const int SliceLength = 1024;
int numberOfSlices = sourcePixels.Length / SliceLength;
using (IMemoryOwner<Vector4> tempVectors = configuration.MemoryAllocator.Allocate<Vector4>(SliceLength))
{
Span<Vector4> vectorSpan = tempVectors.GetSpan();
for (int i = 0; i < numberOfSlices; i++)
{
int start = i * SliceLength;
ReadOnlySpan<TSourcePixel> s = sourcePixels.Slice(start, SliceLength);
Span<TPixel> d = destinationPixels.Slice(start, SliceLength);
PixelOperations<TSourcePixel>.Instance.ToVector4(configuration, s, vectorSpan);
this.FromVector4Destructive(configuration, vectorSpan, d);
}
int endOfCompleteSlices = numberOfSlices * SliceLength;
int remainder = sourcePixels.Length - endOfCompleteSlices;
if (remainder > 0)
{
ReadOnlySpan<TSourcePixel> s = sourcePixels.Slice(endOfCompleteSlices);
Span<TPixel> d = destinationPixels.Slice(endOfCompleteSlices);
vectorSpan = vectorSpan.Slice(0, remainder);
PixelOperations<TSourcePixel>.Instance.ToVector4(configuration, s, vectorSpan);
this.FromVector4Destructive(configuration, vectorSpan, d);
}
}
}

You can see the method is virtual but was not overridden by RgbaVector, so this slower implementation was used, which required both chunking of the target span as well as doing an extra copy to a temporary buffer. This PR adds a new override for this method to PixelOperations<RgbaVector> that simply does a direct ToVector4 copy on the reinterpreted target span.

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.

Lovely stuff! Will merge once tests are finished. 👍

@codecov
Copy link

codecov bot commented Nov 21, 2020

Codecov Report

Merging #1435 (93e9d9d) into master (a9e2b4c) will decrease coverage by 0.00%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1435      +/-   ##
==========================================
- Coverage   83.63%   83.63%   -0.01%     
==========================================
  Files         733      733              
  Lines       31919    31922       +3     
  Branches     3590     3590              
==========================================
  Hits        26697    26697              
- Misses       4508     4511       +3     
  Partials      714      714              
Flag Coverage Δ
unittests 83.63% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ions/PixelOperations/RgbaVector.PixelOperations.cs 90.00% <0.00%> (-10.00%) ⬇️

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 a9e2b4c...93e9d9d. Read the comment docs.

@JimBobSquarePants JimBobSquarePants merged commit f7ef221 into master Nov 21, 2020
@JimBobSquarePants JimBobSquarePants deleted the sp/rgbavector-conversion-speedup branch November 21, 2020 16:48
JimBobSquarePants added a commit that referenced this pull request Mar 13, 2021
PixelOperations<TPixel>.From<RgbaVector> speedup
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.

2 participants