From ae1e2eda45b77c4915663be801c9205bf973f8da Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Jun 2017 12:27:17 +1000 Subject: [PATCH 1/3] First attempt --- .../ResamplingWeightedProcessor.Weights.cs | 46 ++++++++++++++----- .../Transforms/ResizeProfilingBenchmarks.cs | 4 +- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index b5266c9bd8..9c37b69e65 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -27,31 +27,54 @@ namespace ImageSharp.Processing.Processors public int Left; /// - /// The span of weights pointing to . + /// The length of the weights window /// - public Span Span; + public int Length; + + /// + /// The index in the destination buffer + /// + private readonly int destIndex; + + /// + /// The weights buffer . + /// + private readonly Buffer2D buffer; /// /// Initializes a new instance of the struct. /// + /// The destination index in the buffer /// The local left index - /// The span + /// The span + /// The length of the window [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal WeightsWindow(int left, Span span) + internal WeightsWindow(int index, int left, Buffer2D buffer, int length) { + this.destIndex = index; this.Left = left; - this.Span = span; + this.buffer = buffer; + this.Length = length; } /// - /// Gets an unsafe float* pointer to the beginning of . + /// Gets an unsafe float* pointer to the beginning of . /// - public ref float Ptr => ref this.Span.DangerousGetPinnableReference(); + public ref float Ptr + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return ref this.GetWindowSpan().DangerousGetPinnableReference(); + } + } /// - /// Gets the lenghth of the weights window + /// Gets the span representing the portion of the that this window covers /// - public int Length => this.Span.Length; + /// The + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span GetWindowSpan() => this.buffer.GetRowSpan(this.destIndex).Slice(this.Left, this.Length); /// /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. @@ -139,7 +162,7 @@ namespace ImageSharp.Processing.Processors /// internal class WeightsBuffer : IDisposable { - private Buffer2D dataBuffer; + private readonly Buffer2D dataBuffer; /// /// Initializes a new instance of the class. @@ -174,8 +197,7 @@ namespace ImageSharp.Processing.Processors /// The weights public WeightsWindow GetWeightsWindow(int destIdx, int leftIdx, int rightIdx) { - Span span = this.dataBuffer.GetRowSpan(destIdx).Slice(leftIdx, rightIdx - leftIdx + 1); - return new WeightsWindow(leftIdx, span); + return new WeightsWindow(destIdx, leftIdx, this.dataBuffer, rightIdx - leftIdx + 1); } } } diff --git a/tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs index 3f8a75b92b..8b57586f49 100644 --- a/tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processing/Transforms/ResizeProfilingBenchmarks.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Tests.Processing.Transforms { + using System; using System.IO; using System.Text; @@ -49,9 +50,10 @@ namespace ImageSharp.Tests.Processing.Transforms foreach (ResamplingWeightedProcessor.WeightsWindow window in weights.Weights) { + Span span = window.GetWindowSpan(); for (int i = 0; i < window.Length; i++) { - float value = window.Span[i]; + float value = span[i]; bld.Append(value); bld.Append("| "); } From 2d0bdfd97a74c288c86abf7bfb5327cbea88a8fe Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 1 Jun 2017 12:44:12 +0200 Subject: [PATCH 2/3] replaced WeightsWindow.destIndex with flatStartIndex --- .../ResamplingWeightedProcessor.Weights.cs | 27 +++++++++---------- .../Transforms/ResamplingWeightedProcessor.cs | 6 ++--- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index 9c37b69e65..b7e2304fad 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -34,12 +34,12 @@ namespace ImageSharp.Processing.Processors /// /// The index in the destination buffer /// - private readonly int destIndex; + private readonly int flatStartIndex; /// - /// The weights buffer . + /// The buffer containing the weights values. /// - private readonly Buffer2D buffer; + private readonly Buffer buffer; /// /// Initializes a new instance of the struct. @@ -51,22 +51,19 @@ namespace ImageSharp.Processing.Processors [MethodImpl(MethodImplOptions.AggressiveInlining)] internal WeightsWindow(int index, int left, Buffer2D buffer, int length) { - this.destIndex = index; + this.flatStartIndex = (index * buffer.Width) + left; this.Left = left; this.buffer = buffer; this.Length = length; } /// - /// Gets an unsafe float* pointer to the beginning of . + /// Gets a reference to the first item of the window. /// - public ref float Ptr + /// The reference to the first item of the window + public ref float GetStartReference() { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return ref this.GetWindowSpan().DangerousGetPinnableReference(); - } + return ref this.buffer[this.flatStartIndex]; } /// @@ -74,7 +71,7 @@ namespace ImageSharp.Processing.Processors /// /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span GetWindowSpan() => this.buffer.GetRowSpan(this.destIndex).Slice(this.Left, this.Length); + public Span GetWindowSpan() => this.buffer.Slice(this.flatStartIndex, this.Length); /// /// Computes the sum of vectors in 'rowSpan' weighted by weight values, pointed by this instance. @@ -85,7 +82,7 @@ namespace ImageSharp.Processing.Processors [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector4 ComputeWeightedRowSum(Span rowSpan, int sourceX) { - ref float horizontalValues = ref this.Ptr; + ref float horizontalValues = ref this.GetStartReference(); int left = this.Left; ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); @@ -112,7 +109,7 @@ namespace ImageSharp.Processing.Processors [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector4 ComputeExpandedWeightedRowSum(Span rowSpan, int sourceX) { - ref float horizontalValues = ref this.Ptr; + ref float horizontalValues = ref this.GetStartReference(); int left = this.Left; ref Vector4 vecPtr = ref Unsafe.Add(ref rowSpan.DangerousGetPinnableReference(), left + sourceX); @@ -140,7 +137,7 @@ namespace ImageSharp.Processing.Processors [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector4 ComputeWeightedColumnSum(Buffer2D firstPassPixels, int x, int sourceY) { - ref float verticalValues = ref this.Ptr; + ref float verticalValues = ref this.GetStartReference(); int left = this.Left; // Destination color components diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs index 757b0889a5..7245b961f8 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs @@ -112,7 +112,7 @@ namespace ImageSharp.Processing.Processors WeightsWindow ws = result.GetWeightsWindow(i, left, right); result.Weights[i] = ws; - ref float weights = ref ws.Ptr; + ref float weightsBaseRef = ref ws.GetStartReference(); for (int j = left; j <= right; j++) { @@ -120,7 +120,7 @@ namespace ImageSharp.Processing.Processors sum += weight; // weights[j - left] = weight: - Unsafe.Add(ref weights, j - left) = weight; + Unsafe.Add(ref weightsBaseRef, j - left) = weight; } // Normalise, best to do it here rather than in the pixel loop later on. @@ -129,7 +129,7 @@ namespace ImageSharp.Processing.Processors for (int w = 0; w < ws.Length; w++) { // weights[w] = weights[w] / sum: - ref float wRef = ref Unsafe.Add(ref weights, w); + ref float wRef = ref Unsafe.Add(ref weightsBaseRef, w); wRef = wRef / sum; } } From 0fd3f3bf24eab44dd0b83f626c9c74dd2b8fc03f Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 1 Jun 2017 12:51:50 +0200 Subject: [PATCH 3/3] inline all the things --- .../Processors/Transforms/ResamplingWeightedProcessor.Weights.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index b7e2304fad..8aef87ec85 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -61,6 +61,7 @@ namespace ImageSharp.Processing.Processors /// Gets a reference to the first item of the window. /// /// The reference to the first item of the window + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref float GetStartReference() { return ref this.buffer[this.flatStartIndex];