Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Cursively benchmark code. #1

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion benchmark.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dotnet build -c Release source/CsvBenchmark.sln
bin\release\net5.0\CsvBenchmark.exe
bin\release\net5.0\CsvBenchmark.exe --iterationTime 1000
51 changes: 42 additions & 9 deletions source/CsvBenchmark/CsvReaderBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,29 @@ public void MgholamFastCSV()

class CursivelyStringVisitor : CsvReaderVisitorBase
{
readonly bool doPooling;
readonly byte[] bytes = new byte[1024];
int bytesUsed = 0;

// in any realistic scenario we'd need to at least know the column oridnal to do anything with the record
int ordinal = 0;

public CursivelyStringVisitor(bool doPooling)
{
this.doPooling = doPooling;
}

public override void VisitEndOfField(System.ReadOnlySpan<byte> chunk)
{
var str = Encoding.UTF8.GetString(chunk);
if (bytesUsed != 0)
{
chunk.CopyTo(bytes.AsSpan(bytesUsed, chunk.Length));
chunk = new ReadOnlySpan<byte>(bytes, 0, bytesUsed + chunk.Length);
bytesUsed = 0;
}
var str = chunk.Length == 1 && chunk[0] < 128 && doPooling
airbreather marked this conversation as resolved.
Show resolved Hide resolved
? pool[chunk[0]]
: Encoding.UTF8.GetString(chunk);
ordinal++;
}

Expand All @@ -217,17 +234,20 @@ public override void VisitEndOfRecord()

public override void VisitPartialFieldContents(System.ReadOnlySpan<byte> chunk)
{
chunk.CopyTo(bytes.AsSpan(bytesUsed, chunk.Length));
bytesUsed += chunk.Length;
}
}

[Benchmark]
public void CursivelyCsv()
[Arguments(false)]
[Arguments(true)]
public void CursivelyCsv(bool doPooling)
{
var s = TestData.GetUtf8Stream();
var proc = new CursivelyStringVisitor();
var d = TestData.GetUtf8Array();
var proc = new CursivelyStringVisitor(doPooling);
CsvSyncInput
.ForStream(s)
.WithMinReadBufferByteCount(BufferSize)
.ForMemory(d)
.Process(proc);
}

Expand Down Expand Up @@ -352,6 +372,9 @@ public void NRecoSelect()

class CursivelySelectVisitor : CsvReaderVisitorBase
{
readonly byte[] bytes = new byte[1024];
int bytesUsed = 0;

int ordinal = 0;
int row = 0;

Expand All @@ -361,6 +384,12 @@ class CursivelySelectVisitor : CsvReaderVisitorBase

public override void VisitEndOfField(ReadOnlySpan<byte> chunk)
{
if (bytesUsed != 0)
{
chunk.CopyTo(bytes.AsSpan(bytesUsed, chunk.Length));
chunk = new ReadOnlySpan<byte>(bytes, 0, bytesUsed + chunk.Length);
bytesUsed = 0;
}
if (row != 0) // skip the header row
{
switch (ordinal)
Expand Down Expand Up @@ -398,17 +427,21 @@ public override void VisitEndOfRecord()

public override void VisitPartialFieldContents(System.ReadOnlySpan<byte> chunk)
{
if (row > 0)
{
chunk.CopyTo(bytes.AsSpan(bytesUsed, chunk.Length));
bytesUsed += chunk.Length;
}
}
}

[Benchmark]
public void CursivelyCsvSelect()
{
var s = TestData.GetUtf8Stream();
var d = TestData.GetUtf8Array();
var proc = new CursivelySelectVisitor();
CsvSyncInput
.ForStream(s)
.WithMinReadBufferByteCount(BufferSize)
.ForMemory(d)
.Process(proc);
}

Expand Down
5 changes: 5 additions & 0 deletions source/CsvBenchmark/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public static Stream GetUtf8Stream()
return new MemoryStream(CachedUtfData);
}

public static ReadOnlyMemory<byte> GetUtf8Array()
{
return CachedUtfData;
}

public static DbDataReader GetData()
{

Expand Down