Browse Source

Minor performance refactorings.

Moved the division out of the loop, division is expensive.
pull/548/head
woutware 8 years ago
parent
commit
a06600380a
  1. 10
      src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs
  2. 6
      src/ImageSharp/Formats/Png/PngEncoderCore.cs

10
src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs

@ -100,6 +100,8 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors
using (BasicArrayBuffer<float> scanline = source.MemoryManager.AllocateFake<float>(scanlineWidth)) using (BasicArrayBuffer<float> scanline = source.MemoryManager.AllocateFake<float>(scanlineWidth))
{ {
bool scanlineDirty = true; bool scanlineDirty = true;
float subpixelFraction = 1f / subpixelCount;
float subpixelFractionPoint = subpixelFraction / subpixelCount;
for (int y = minY; y < maxY; y++) for (int y = minY; y < maxY; y++)
{ {
if (scanlineDirty) if (scanlineDirty)
@ -113,9 +115,8 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors
scanlineDirty = false; scanlineDirty = false;
} }
float subpixelFraction = 1f / subpixelCount; float yPlusOne = y + 1;
float subpixelFractionPoint = subpixelFraction / subpixelCount; for (float subPixel = (float)y; subPixel < yPlusOne; subPixel += subpixelFraction)
for (float subPixel = (float)y; subPixel < y + 1; subPixel += subpixelFraction)
{ {
int pointsFound = region.Scan(subPixel + offset, buffer.Array, 0); int pointsFound = region.Scan(subPixel + offset, buffer.Array, 0);
if (pointsFound == 0) if (pointsFound == 0)
@ -197,8 +198,7 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Processors
private static void QuickSort(Span<float> data) private static void QuickSort(Span<float> data)
{ {
int hi = Math.Min(data.Length - 1, data.Length - 1); QuickSort(data, 0, data.Length - 1);
QuickSort(data, 0, hi);
} }
private static void QuickSort(Span<float> data, int lo, int hi) private static void QuickSort(Span<float> data, int lo, int hi)

6
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -321,9 +321,6 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <returns>The <see cref="T:byte[]"/></returns> /// <returns>The <see cref="T:byte[]"/></returns>
private IManagedByteBuffer GetOptimalFilteredScanline() private IManagedByteBuffer GetOptimalFilteredScanline()
{ {
Span<byte> scanSpan = this.rawScanline.Span;
Span<byte> prevSpan = this.previousScanline.Span;
// Palette images don't compress well with adaptive filtering. // Palette images don't compress well with adaptive filtering.
if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8)
{ {
@ -331,6 +328,9 @@ namespace SixLabors.ImageSharp.Formats.Png
return this.result; return this.result;
} }
Span<byte> scanSpan = this.rawScanline.Span;
Span<byte> prevSpan = this.previousScanline.Span;
// This order, while different to the enumerated order is more likely to produce a smaller sum // This order, while different to the enumerated order is more likely to produce a smaller sum
// early on which shaves a couple of milliseconds off the processing time. // early on which shaves a couple of milliseconds off the processing time.
UpFilter.Encode(scanSpan, prevSpan, this.up.Span, out int currentSum); UpFilter.Encode(scanSpan, prevSpan, this.up.Span, out int currentSum);

Loading…
Cancel
Save