Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/PbfLite.Benchmark/PbfBlockReaderCollectionsBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;

namespace PbfLite.Benchmark;

[MemoryDiagnoser]
public class PbfBlockReaderCollectionsBenchmarks
{
private static readonly byte[] UIntCollectionData = new byte[] { 0x0F, 0x00, 0x80, 0x01, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x80, 0x01 };
Expand All @@ -12,7 +15,7 @@ public uint ReadUIntCollectionIntoBuffer()
{
var reader = PbfBlockReader.Create(UIntCollectionData);

reader.ReadUIntCollection(WireType.String, UIntBuffer);
reader.ReadUIntCollection(WireType.String, UIntBuffer.AsSpan());

return UIntBuffer[0];
}
Expand All @@ -24,11 +27,22 @@ public uint ReadUIntSingleItemCollectionIntoBuffer()
{
var reader = PbfBlockReader.Create(UIntSingleItemCollection);

reader.ReadUIntCollection(WireType.VarInt, UIntBuffer);
reader.ReadUIntCollection(WireType.VarInt, UIntBuffer.AsSpan());

return UIntBuffer[0];
}

[Benchmark]
public uint ReadUIntCollectionIntoList()
{
var reader = PbfBlockReader.Create(UIntCollectionData);
var collection = new List<uint>();

reader.ReadUIntCollection(WireType.String, collection);

return collection[0];
}

private static readonly byte[] ULongCollectionData = new byte[] { 0x19, 0x00, 0x80, 0x01, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x01, 0x80, 0x80, 0x80, 0x80, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 };
private static readonly ulong[] ULongBuffer = new ulong[6];

Expand Down
14 changes: 7 additions & 7 deletions src/PbfLite.Tests/CollectionsRoundTripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ public void UIntCollection_RoundTrip_SingleElement()
{
var originalData = new uint[] { 42 };
var buffer = new byte[10];

// Write
var writer = PbfBlockWriter.Create(buffer);
writer.WriteUIntCollection(originalData);

// Read
var reader = PbfBlockReader.Create(writer.Block);
var readBuffer = new uint[1];
var result = reader.ReadUIntCollection(WireType.String, readBuffer);

SpanAssert.Equal<uint>(originalData, result);
}

[Fact]
public void UIntCollection_RoundTrip_MultipleElements()
{
var originalData = new uint[] { 0, 128, 16384, 2097152 };
var buffer = new byte[50];

// Write
var writer = PbfBlockWriter.Create(buffer);
writer.WriteUIntCollection(originalData.AsSpan());

// Read
var reader = PbfBlockReader.Create(writer.Block);
var readBuffer = new uint[originalData.Length];
var result = reader.ReadUIntCollection(WireType.String, readBuffer);

SpanAssert.Equal<uint>(originalData, result);
}
}
Loading
Loading