From 086040cd815d3929f333c4cd135d5b77cb68e16f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 27 Feb 2020 13:00:17 +0100 Subject: [PATCH] Removed IterationParameters type --- .../Advanced/ParallelRowIterator.Wrappers.cs | 107 ++++++++++-------- .../Advanced/ParallelRowIterator.cs | 12 +- 2 files changed, 64 insertions(+), 55 deletions(-) diff --git a/src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs b/src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs index 7c3861fdf..3f0f77ca3 100644 --- a/src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs +++ b/src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs @@ -17,51 +17,38 @@ namespace SixLabors.ImageSharp.Advanced /// public static partial class ParallelRowIterator { - private readonly struct IterationParameters - { - public readonly int MinY; - public readonly int MaxY; - public readonly int StepY; - public readonly int Width; - - public IterationParameters(int minY, int maxY, int stepY) - : this(minY, maxY, stepY, 0) - { - } - - public IterationParameters(int minY, int maxY, int stepY, int width) - { - this.MinY = minY; - this.MaxY = maxY; - this.StepY = stepY; - this.Width = width; - } - } - private readonly struct RowOperationWrapper where T : struct, IRowOperation { - private readonly IterationParameters info; + private readonly int minY; + private readonly int maxY; + private readonly int stepY; private readonly T action; [MethodImpl(InliningOptions.ShortMethod)] - public RowOperationWrapper(in IterationParameters info, in T action) + public RowOperationWrapper( + int minY, + int maxY, + int stepY, + in T action) { - this.info = info; + this.minY = minY; + this.maxY = maxY; + this.stepY = stepY; this.action = action; } [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int i) { - int yMin = this.info.MinY + (i * this.info.StepY); + int yMin = this.minY + (i * this.stepY); - if (yMin >= this.info.MaxY) + if (yMin >= this.maxY) { return; } - int yMax = Math.Min(yMin + this.info.StepY, this.info.MaxY); + int yMax = Math.Min(yMin + this.stepY, this.maxY); for (int y = yMin; y < yMax; y++) { @@ -75,17 +62,26 @@ namespace SixLabors.ImageSharp.Advanced where T : struct, IRowOperation where TBuffer : unmanaged { - private readonly IterationParameters info; + private readonly int minY; + private readonly int maxY; + private readonly int stepY; + private readonly int width; private readonly MemoryAllocator allocator; private readonly T action; [MethodImpl(InliningOptions.ShortMethod)] public RowOperationWrapper( - in IterationParameters info, + int minY, + int maxY, + int stepY, + int width, MemoryAllocator allocator, in T action) { - this.info = info; + this.minY = minY; + this.maxY = maxY; + this.stepY = stepY; + this.width = width; this.allocator = allocator; this.action = action; } @@ -93,16 +89,16 @@ namespace SixLabors.ImageSharp.Advanced [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int i) { - int yMin = this.info.MinY + (i * this.info.StepY); + int yMin = this.minY + (i * this.stepY); - if (yMin >= this.info.MaxY) + if (yMin >= this.maxY) { return; } - int yMax = Math.Min(yMin + this.info.StepY, this.info.MaxY); + int yMax = Math.Min(yMin + this.stepY, this.maxY); - using IMemoryOwner buffer = this.allocator.Allocate(this.info.Width); + using IMemoryOwner buffer = this.allocator.Allocate(this.width); Span span = buffer.Memory.Span; @@ -116,27 +112,35 @@ namespace SixLabors.ImageSharp.Advanced private readonly struct RowIntervalOperationWrapper where T : struct, IRowIntervalOperation { - private readonly IterationParameters info; + private readonly int minY; + private readonly int maxY; + private readonly int stepY; private readonly T operation; [MethodImpl(InliningOptions.ShortMethod)] - public RowIntervalOperationWrapper(in IterationParameters info, in T operation) + public RowIntervalOperationWrapper( + int minY, + int maxY, + int stepY, + in T operation) { - this.info = info; + this.minY = minY; + this.maxY = maxY; + this.stepY = stepY; this.operation = operation; } [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int i) { - int yMin = this.info.MinY + (i * this.info.StepY); + int yMin = this.minY + (i * this.stepY); - if (yMin >= this.info.MaxY) + if (yMin >= this.maxY) { return; } - int yMax = Math.Min(yMin + this.info.StepY, this.info.MaxY); + int yMax = Math.Min(yMin + this.stepY, this.maxY); var rows = new RowInterval(yMin, yMax); // Skip the safety copy when invoking a potentially impure method on a readonly field @@ -148,17 +152,26 @@ namespace SixLabors.ImageSharp.Advanced where T : struct, IRowIntervalOperation where TBuffer : unmanaged { - private readonly IterationParameters info; + private readonly int minY; + private readonly int maxY; + private readonly int stepY; + private readonly int width; private readonly MemoryAllocator allocator; private readonly T operation; [MethodImpl(InliningOptions.ShortMethod)] public RowIntervalOperationWrapper( - in IterationParameters info, + int minY, + int maxY, + int stepY, + int width, MemoryAllocator allocator, in T operation) { - this.info = info; + this.minY = minY; + this.maxY = maxY; + this.stepY = stepY; + this.width = width; this.allocator = allocator; this.operation = operation; } @@ -166,17 +179,17 @@ namespace SixLabors.ImageSharp.Advanced [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int i) { - int yMin = this.info.MinY + (i * this.info.StepY); + int yMin = this.minY + (i * this.stepY); - if (yMin >= this.info.MaxY) + if (yMin >= this.maxY) { return; } - int yMax = Math.Min(yMin + this.info.StepY, this.info.MaxY); + int yMax = Math.Min(yMin + this.stepY, this.maxY); var rows = new RowInterval(yMin, yMax); - using IMemoryOwner buffer = this.allocator.Allocate(this.info.Width); + using IMemoryOwner buffer = this.allocator.Allocate(this.width); Unsafe.AsRef(in this.operation).Invoke(in rows, buffer.Memory.Span); } diff --git a/src/ImageSharp/Advanced/ParallelRowIterator.cs b/src/ImageSharp/Advanced/ParallelRowIterator.cs index 86442ede7..fb85de986 100644 --- a/src/ImageSharp/Advanced/ParallelRowIterator.cs +++ b/src/ImageSharp/Advanced/ParallelRowIterator.cs @@ -68,8 +68,7 @@ namespace SixLabors.ImageSharp.Advanced int verticalStep = DivideCeil(rectangle.Height, numOfSteps); var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; - var info = new IterationParameters(top, bottom, verticalStep); - var wrappingOperation = new RowOperationWrapper(in info, in operation); + var wrappingOperation = new RowOperationWrapper(top, bottom, verticalStep, in operation); Parallel.For( 0, @@ -138,8 +137,7 @@ namespace SixLabors.ImageSharp.Advanced int verticalStep = DivideCeil(height, numOfSteps); var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; - var info = new IterationParameters(top, bottom, verticalStep, width); - var wrappingOperation = new RowOperationWrapper(in info, allocator, in operation); + var wrappingOperation = new RowOperationWrapper(top, bottom, verticalStep, width, allocator, in operation); Parallel.For( 0, @@ -196,8 +194,7 @@ namespace SixLabors.ImageSharp.Advanced int verticalStep = DivideCeil(rectangle.Height, numOfSteps); var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; - var info = new IterationParameters(top, bottom, verticalStep); - var wrappingOperation = new RowIntervalOperationWrapper(in info, in operation); + var wrappingOperation = new RowIntervalOperationWrapper(top, bottom, verticalStep, in operation); Parallel.For( 0, @@ -263,8 +260,7 @@ namespace SixLabors.ImageSharp.Advanced int verticalStep = DivideCeil(height, numOfSteps); var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; - var info = new IterationParameters(top, bottom, verticalStep, width); - var wrappingOperation = new RowIntervalOperationWrapper(in info, allocator, in operation); + var wrappingOperation = new RowIntervalOperationWrapper(top, bottom, verticalStep, width, allocator, in operation); Parallel.For( 0,