Skip to content

Commit

Permalink
skip ArrayPool<byte>.Shared.Return when buffer is null (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcarbon authored and saucecontrol committed Jan 7, 2020
1 parent a55840f commit b334866
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/MagicScaler/Magic/ConversionTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ unsafe private void copyPixelsDirect(in PixelArea prc, int cbStride, int cbBuffe

public void Dispose()
{
ArrayPool<byte>.Shared.Return(lineBuff ?? Array.Empty<byte>());
if (lineBuff is null)
return;

ArrayPool<byte>.Shared.Return(lineBuff);
lineBuff = null;
}

Expand Down
10 changes: 8 additions & 2 deletions src/MagicScaler/Magic/ConvolutionTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ public virtual void Dispose()
SrcBuff?.Dispose();
WorkBuff?.Dispose();

ArrayPool<byte>.Shared.Return(lineBuff ?? Array.Empty<byte>());
if (lineBuff is null)
return;

ArrayPool<byte>.Shared.Return(lineBuff);
lineBuff = null;
}

Expand Down Expand Up @@ -261,7 +264,10 @@ public override void Dispose()
{
base.Dispose();

ArrayPool<byte>.Shared.Return(blurBuff ?? Array.Empty<byte>());
if (blurBuff is null)
return;

ArrayPool<byte>.Shared.Return(blurBuff);
blurBuff = null!;
}

Expand Down
6 changes: 5 additions & 1 deletion src/MagicScaler/Magic/HybridScaleTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,11 @@ unsafe private void process(byte* ipstart, byte* opstart, uint stride, bool rup)

public void Dispose()
{
ArrayPool<byte>.Shared.Return(lineBuff ?? Array.Empty<byte>());
if (lineBuff is null)
return;

ArrayPool<byte>.Shared.Return(lineBuff);

lineBuff = null!;
}

Expand Down
5 changes: 4 additions & 1 deletion src/MagicScaler/Magic/KernelMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ unsafe public static KernelMap<T> CreateBlur(int size, double radius, int ichann

public void Dispose()
{
ArrayPool<byte>.Shared.Return(map ?? Array.Empty<byte>());
if (map is null)
return;

ArrayPool<byte>.Shared.Return(map);
map = null!;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/MagicScaler/Magic/OrientationTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ public void Dispose()
{
srcBuff?.Dispose();

ArrayPool<byte>.Shared.Return(lineBuff ?? Array.Empty<byte>());
if (lineBuff is null)
return;

ArrayPool<byte>.Shared.Return(lineBuff);
lineBuff = null;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/MagicScaler/Magic/PlanarConversionTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ unsafe private void copyPixelsAvx(byte* bstart, byte* opstart, uint bstride, uin

public void Dispose()
{
ArrayPool<byte>.Shared.Return(lineBuff ?? Array.Empty<byte>());
if (lineBuff is null)
return;

ArrayPool<byte>.Shared.Return(lineBuff);
lineBuff = null!;
}
}
Expand Down

0 comments on commit b334866

Please sign in to comment.