Browse Source

Removed IterationParameters type

pull/1574/head
Sergio Pedri 6 years ago
parent
commit
086040cd81
  1. 107
      src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs
  2. 12
      src/ImageSharp/Advanced/ParallelRowIterator.cs

107
src/ImageSharp/Advanced/ParallelRowIterator.Wrappers.cs

@ -17,51 +17,38 @@ namespace SixLabors.ImageSharp.Advanced
/// </content> /// </content>
public static partial class ParallelRowIterator 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<T> private readonly struct RowOperationWrapper<T>
where T : struct, IRowOperation where T : struct, IRowOperation
{ {
private readonly IterationParameters info; private readonly int minY;
private readonly int maxY;
private readonly int stepY;
private readonly T action; private readonly T action;
[MethodImpl(InliningOptions.ShortMethod)] [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; this.action = action;
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int i) 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; 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++) for (int y = yMin; y < yMax; y++)
{ {
@ -75,17 +62,26 @@ namespace SixLabors.ImageSharp.Advanced
where T : struct, IRowOperation<TBuffer> where T : struct, IRowOperation<TBuffer>
where TBuffer : unmanaged 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 MemoryAllocator allocator;
private readonly T action; private readonly T action;
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public RowOperationWrapper( public RowOperationWrapper(
in IterationParameters info, int minY,
int maxY,
int stepY,
int width,
MemoryAllocator allocator, MemoryAllocator allocator,
in T action) in T action)
{ {
this.info = info; this.minY = minY;
this.maxY = maxY;
this.stepY = stepY;
this.width = width;
this.allocator = allocator; this.allocator = allocator;
this.action = action; this.action = action;
} }
@ -93,16 +89,16 @@ namespace SixLabors.ImageSharp.Advanced
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int i) 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; return;
} }
int yMax = Math.Min(yMin + this.info.StepY, this.info.MaxY); int yMax = Math.Min(yMin + this.stepY, this.maxY);
using IMemoryOwner<TBuffer> buffer = this.allocator.Allocate<TBuffer>(this.info.Width); using IMemoryOwner<TBuffer> buffer = this.allocator.Allocate<TBuffer>(this.width);
Span<TBuffer> span = buffer.Memory.Span; Span<TBuffer> span = buffer.Memory.Span;
@ -116,27 +112,35 @@ namespace SixLabors.ImageSharp.Advanced
private readonly struct RowIntervalOperationWrapper<T> private readonly struct RowIntervalOperationWrapper<T>
where T : struct, IRowIntervalOperation where T : struct, IRowIntervalOperation
{ {
private readonly IterationParameters info; private readonly int minY;
private readonly int maxY;
private readonly int stepY;
private readonly T operation; private readonly T operation;
[MethodImpl(InliningOptions.ShortMethod)] [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; this.operation = operation;
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int i) 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; 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); var rows = new RowInterval(yMin, yMax);
// Skip the safety copy when invoking a potentially impure method on a readonly field // 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<TBuffer> where T : struct, IRowIntervalOperation<TBuffer>
where TBuffer : unmanaged 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 MemoryAllocator allocator;
private readonly T operation; private readonly T operation;
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public RowIntervalOperationWrapper( public RowIntervalOperationWrapper(
in IterationParameters info, int minY,
int maxY,
int stepY,
int width,
MemoryAllocator allocator, MemoryAllocator allocator,
in T operation) in T operation)
{ {
this.info = info; this.minY = minY;
this.maxY = maxY;
this.stepY = stepY;
this.width = width;
this.allocator = allocator; this.allocator = allocator;
this.operation = operation; this.operation = operation;
} }
@ -166,17 +179,17 @@ namespace SixLabors.ImageSharp.Advanced
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(int i) 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; 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); var rows = new RowInterval(yMin, yMax);
using IMemoryOwner<TBuffer> buffer = this.allocator.Allocate<TBuffer>(this.info.Width); using IMemoryOwner<TBuffer> buffer = this.allocator.Allocate<TBuffer>(this.width);
Unsafe.AsRef(in this.operation).Invoke(in rows, buffer.Memory.Span); Unsafe.AsRef(in this.operation).Invoke(in rows, buffer.Memory.Span);
} }

12
src/ImageSharp/Advanced/ParallelRowIterator.cs

@ -68,8 +68,7 @@ namespace SixLabors.ImageSharp.Advanced
int verticalStep = DivideCeil(rectangle.Height, numOfSteps); int verticalStep = DivideCeil(rectangle.Height, numOfSteps);
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
var info = new IterationParameters(top, bottom, verticalStep); var wrappingOperation = new RowOperationWrapper<T>(top, bottom, verticalStep, in operation);
var wrappingOperation = new RowOperationWrapper<T>(in info, in operation);
Parallel.For( Parallel.For(
0, 0,
@ -138,8 +137,7 @@ namespace SixLabors.ImageSharp.Advanced
int verticalStep = DivideCeil(height, numOfSteps); int verticalStep = DivideCeil(height, numOfSteps);
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
var info = new IterationParameters(top, bottom, verticalStep, width); var wrappingOperation = new RowOperationWrapper<T, TBuffer>(top, bottom, verticalStep, width, allocator, in operation);
var wrappingOperation = new RowOperationWrapper<T, TBuffer>(in info, allocator, in operation);
Parallel.For( Parallel.For(
0, 0,
@ -196,8 +194,7 @@ namespace SixLabors.ImageSharp.Advanced
int verticalStep = DivideCeil(rectangle.Height, numOfSteps); int verticalStep = DivideCeil(rectangle.Height, numOfSteps);
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
var info = new IterationParameters(top, bottom, verticalStep); var wrappingOperation = new RowIntervalOperationWrapper<T>(top, bottom, verticalStep, in operation);
var wrappingOperation = new RowIntervalOperationWrapper<T>(in info, in operation);
Parallel.For( Parallel.For(
0, 0,
@ -263,8 +260,7 @@ namespace SixLabors.ImageSharp.Advanced
int verticalStep = DivideCeil(height, numOfSteps); int verticalStep = DivideCeil(height, numOfSteps);
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps };
var info = new IterationParameters(top, bottom, verticalStep, width); var wrappingOperation = new RowIntervalOperationWrapper<T, TBuffer>(top, bottom, verticalStep, width, allocator, in operation);
var wrappingOperation = new RowIntervalOperationWrapper<T, TBuffer>(in info, allocator, in operation);
Parallel.For( Parallel.For(
0, 0,

Loading…
Cancel
Save