Skip to content

Consider optimizing StreamsComparer for happy path #3829

@stevenaw

Description

@stevenaw

StreamsComparer currently compares the bytes of two streams and reports on the index of the first difference. It does this by reading the streams 4096 bytes at a time into buffers and comparing the arrays byte-by-byte.

Some of the new .NET APIs allow for vectorized equality checks on Span<byte>. It may be worth investigating if there's a performance benefit to check for buffer equality via MemoryExtenions.SequenceEquals() and falling back to a byte-by-byte check to determine the failure point if the two buffers are known to be unequal.

for (long readByte = 0; readByte < xStream.Length; readByte += BUFFER_SIZE)
{
binaryReaderExpected.Read(bufferExpected, 0, BUFFER_SIZE);
binaryReaderActual.Read(bufferActual, 0, BUFFER_SIZE);
for (int count = 0; count < BUFFER_SIZE; ++count)
{
if (bufferExpected[count] != bufferActual[count])

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions