Browse Source

Reduced number of memory allocations

pull/1068/head
Sergio Pedri 7 years ago
parent
commit
71cf6e84a7
  1. 19
      src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs

19
src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor{TPixel}.cs

@ -72,6 +72,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
int length = vectorSpan.Length; int length = vectorSpan.Length;
ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(vectorSpan); ref Vector4 vectorSpanRef = ref MemoryMarshal.GetReference(vectorSpan);
// Rent the shared buffer only once per parallel item
IMemoryOwner<float> bins = configuration.MemoryAllocator.Allocate<float>(levels * 4);
ref float binsRef = ref bins.GetReference();
ref int intensityBinRef = ref Unsafe.As<float, int>(ref binsRef);
ref float redBinRef = ref Unsafe.Add(ref binsRef, levels);
ref float blueBinRef = ref Unsafe.Add(ref redBinRef, levels);
ref float greenBinRef = ref Unsafe.Add(ref blueBinRef, levels);
for (int y = rows.Min; y < rows.Max; y++) for (int y = rows.Min; y < rows.Max; y++)
{ {
Span<TPixel> sourceRow = source.GetPixelRowSpan(y); Span<TPixel> sourceRow = source.GetPixelRowSpan(y);
@ -83,13 +92,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
int maxIntensity = 0; int maxIntensity = 0;
int maxIndex = 0; int maxIndex = 0;
using (IMemoryOwner<float> bins = configuration.MemoryAllocator.Allocate<float>(levels * 4, AllocationOptions.Clean)) // Clear the current shared buffer before processing each target pixel
{ bins.Memory.Span.Clear();
ref float binsRef = ref bins.GetReference();
ref int intensityBinRef = ref Unsafe.As<float, int>(ref binsRef);
ref float redBinRef = ref Unsafe.Add(ref binsRef, levels);
ref float blueBinRef = ref Unsafe.Add(ref redBinRef, levels);
ref float greenBinRef = ref Unsafe.Add(ref blueBinRef, levels);
for (int fy = 0; fy <= radius; fy++) for (int fy = 0; fy <= radius; fy++)
{ {
@ -134,7 +138,6 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
Unsafe.Add(ref vectorSpanRef, x) = new Vector4(red, green, blue, alpha); Unsafe.Add(ref vectorSpanRef, x) = new Vector4(red, green, blue, alpha);
} }
} }
}
PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan); PixelOperations<TPixel>.Instance.FromVector4Destructive(configuration, vectorSpan, targetRowSpan);
} }

Loading…
Cancel
Save