-
Notifications
You must be signed in to change notification settings - Fork 756
Closed
Labels
Milestone
Description
In .net 8, instead of allocating the byte array on the heap, if we were to allocate it on stack using stackalloc it can provide some performance improvements based on the amount of memory allocated and therefore the GC runs.
The changes are as follows (for line 42)
byte[] bufferExpected = new byte[BUFFER_SIZE];
byte[] bufferActual = new byte[BUFFER_SIZE];to
Span<byte> bufferExpected = stackalloc byte[BUFFER_SIZE];
Span<byte> bufferActual = stackalloc byte[BUFFER_SIZE];This would also require some changes on line 68 as BinaryReader.Read(Span<>) doesn't exist in .net framework (https://learn.microsoft.com/en-us/dotnet/api/system.io.binaryreader.read?view=net-8.0) - so was thinking of using the #IF NETFRAMEWORK flag to keep the current implementation for .net framework.
Here is the performance benchmark in .net 8 for both of these methods:
BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.4529/22H2/2022Update)
AMD Ryzen 5 5600X, 1 CPU, 12 logical and 6 physical cores
.NET SDK 8.0.300
[Host] : .NET 8.0.5 (8.0.524.21615), X64 RyuJIT AVX2
| Method | Size | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
|------------- |------ |---------:|----------:|----------:|------:|--------:|-------:|-------:|----------:|------------:|
| NewByteArray | 32768 | 1.784 μs | 0.0346 μs | 0.0517 μs | 1.00 | 0.00 | 0.5112 | 0.0114 | 8560 B | 1.00 |
| StackAlloc | 32768 | 1.344 μs | 0.0267 μs | 0.0408 μs | 0.75 | 0.03 | 0.0191 | - | 320 B | 0.04 |
Benchmark
using BenchmarkDotNet.Attributes;
BenchmarkRunner.Run<ByteAllocationBenchmark>();
[MemoryDiagnoser]
public class ByteAllocationBenchmark
{
private const int BUFFER_SIZE = 4096;
[Params(4096 * 8)]
public int Size { get; set; }
private Stream? XStream { get; set; }
private Stream? YStream { get; set; }
[GlobalSetup]
public void Setup()
{
var buffer = new byte[BUFFER_SIZE];
XStream = new MemoryStream(Size);
for (var i = 0; i < Size; i += BUFFER_SIZE)
XStream.Write(buffer, 0, BUFFER_SIZE);
XStream.Seek(0, SeekOrigin.Begin);
YStream = new MemoryStream(Size);
for (var i = 0; i < Size; i += BUFFER_SIZE)
YStream.Write(buffer, 0, BUFFER_SIZE);
YStream.Seek(0, SeekOrigin.Begin);
}
[GlobalCleanup]
public void Cleanup()
{
XStream?.Dispose();
YStream?.Dispose();
}
[Benchmark(Baseline = true)]
public bool NewByteArray()
{
var equal = Equal_Original(XStream!, YStream!, out _);
return equal == EqualMethodResult.ComparedEqual;
}
[Benchmark]
public bool StackAlloc()
{
var equal = Equal_Enhanced(XStream!, YStream!, out _);
return equal == EqualMethodResult.ComparedEqual;
}
public static EqualMethodResult Equal_Original(Stream xStream, Stream yStream, out long? failurePoint)
{
failurePoint = null;
bool bothSeekable = xStream.CanSeek && yStream.CanSeek;
if (bothSeekable && xStream.Length != yStream.Length)
return EqualMethodResult.ComparedNotEqual;
byte[] bufferExpected = new byte[BUFFER_SIZE];
byte[] bufferActual = new byte[BUFFER_SIZE];
BinaryReader binaryReaderExpected = new BinaryReader(xStream);
BinaryReader binaryReaderActual = new BinaryReader(yStream);
long expectedPosition = bothSeekable ? xStream.Position : default;
long actualPosition = bothSeekable ? yStream.Position : default;
try
{
if (xStream.CanSeek)
{
binaryReaderExpected.BaseStream.Seek(0, SeekOrigin.Begin);
}
if (yStream.CanSeek)
{
binaryReaderActual.BaseStream.Seek(0, SeekOrigin.Begin);
}
int readExpected = 1;
int readActual = 1;
long readByte = 0;
while (readExpected > 0 && readActual > 0)
{
readExpected = binaryReaderExpected.Read(bufferExpected, 0, BUFFER_SIZE);
readActual = binaryReaderActual.Read(bufferActual, 0, BUFFER_SIZE);
if (MemoryExtensions.SequenceEqual<byte>(bufferExpected, bufferActual))
{
readByte += BUFFER_SIZE;
continue;
}
for (int count = 0; count < BUFFER_SIZE; ++count)
{
if (bufferExpected[count] != bufferActual[count])
{
failurePoint = readByte + count;
return EqualMethodResult.ComparedNotEqual;
}
}
readByte += BUFFER_SIZE;
}
}
finally
{
if (xStream.CanSeek)
{
xStream.Position = expectedPosition;
}
if (yStream.CanSeek)
{
yStream.Position = actualPosition;
}
}
return EqualMethodResult.ComparedEqual;
}
public static EqualMethodResult Equal_Enhanced(Stream xStream, Stream yStream, out long? failurePoint)
{
failurePoint = null;
bool bothSeekable = xStream.CanSeek && yStream.CanSeek;
if (bothSeekable && xStream.Length != yStream.Length)
return EqualMethodResult.ComparedNotEqual;
Span<byte> bufferExpected = stackalloc byte[BUFFER_SIZE];
Span<byte> bufferActual = stackalloc byte[BUFFER_SIZE];
BinaryReader binaryReaderExpected = new BinaryReader(xStream);
BinaryReader binaryReaderActual = new BinaryReader(yStream);
long expectedPosition = bothSeekable ? xStream.Position : default;
long actualPosition = bothSeekable ? yStream.Position : default;
try
{
if (xStream.CanSeek)
{
binaryReaderExpected.BaseStream.Seek(0, SeekOrigin.Begin);
}
if (yStream.CanSeek)
{
binaryReaderActual.BaseStream.Seek(0, SeekOrigin.Begin);
}
int readExpected = 1;
int readActual = 1;
long readByte = 0;
while (readExpected > 0 && readActual > 0)
{
readExpected = binaryReaderExpected.Read(bufferExpected);
readActual = binaryReaderActual.Read(bufferActual);
if (MemoryExtensions.SequenceEqual<byte>(bufferExpected, bufferActual))
{
readByte += BUFFER_SIZE;
continue;
}
for (int count = 0; count < BUFFER_SIZE; ++count)
{
if (bufferExpected[count] != bufferActual[count])
{
failurePoint = readByte + count;
return EqualMethodResult.ComparedNotEqual;
}
}
readByte += BUFFER_SIZE;
}
}
finally
{
if (xStream.CanSeek)
{
xStream.Position = expectedPosition;
}
if (yStream.CanSeek)
{
yStream.Position = actualPosition;
}
}
return EqualMethodResult.ComparedEqual;
}
/// <summary>
/// Result of the Equal comparison method.
/// </summary>
public enum EqualMethodResult
{
/// <summary>
/// Method does not support the instances being compared.
/// </summary>
TypesNotSupported,
/// <summary>
/// Method is appropriate for the data types, but doesn't support the specified tolerance.
/// </summary>
ToleranceNotSupported,
/// <summary>
/// Method is appropriate and the items are considered equal.
/// </summary>
ComparedEqual,
/// <summary>
/// Method is appropriate and the items are considered different.
/// </summary>
ComparedNotEqual,
/// <summary>
/// Method is appropriate but the class has cyclic references.
/// </summary>
ComparisonPending
}
}