Skip to content

Commit

Permalink
Add Sep - the fastest yet (#51)
Browse files Browse the repository at this point in the history
* Add Sep

* use sep 0.1.0
  • Loading branch information
nietras authored Oct 26, 2023
1 parent dec46cb commit 0c6fac0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions NCsvPerf/CsvReadable/Benchmarks/PackageAssetsSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ public void RecordParser()
Execute(new RecordParser(ActivationMethod.ILEmit));
}

[Benchmark]
public void Sep()
{
Execute(new Sep(ActivationMethod.ILEmit));
}

[Benchmark]
public void ServiceStack_Text()
{
Expand Down
46 changes: 46 additions & 0 deletions NCsvPerf/CsvReadable/Implementations/Sep.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.IO;
using nietras.SeparatedValues;

namespace Knapcode.NCsvPerf.CsvReadable
{
/// <summary>
/// Package: https://www.nuget.org/packages/Sep/
/// Source: https://github.com/nietras/Sep
/// </summary>
public class Sep : ICsvReader
{
private readonly ActivationMethod _activationMethod;

public Sep(ActivationMethod activationMethod)
{
_activationMethod = activationMethod;
}

public List<T> GetRecords<T>(MemoryStream stream) where T : ICsvReadable, new()
{
var activate = ActivatorFactory.Create<T>(_activationMethod);
var allRecords = new List<T>();

using var reader = nietras.SeparatedValues.Sep.Reader(o => o with
{
HasHeader = false,
CreateToString = SepToString.PoolPerCol(maximumStringLength: 128),
})
.From(stream);

// Due to how NCsvPerf code is factored and that Sep uses ref
// structs, the code below is different from normal Sep usage.
Func<int, string> toString = reader.ToString;
foreach (var row in reader)
{
var record = activate();
record.Read(toString);
allRecords.Add(record);
}

return allRecords;
}
}
}
1 change: 1 addition & 0 deletions NCsvPerf/NCsvPerf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<PackageReference Include="NReco.Csv" Version="1.0.1" />
<PackageReference Include="Open.Text.CSV" Version="3.3.0" />
<PackageReference Include="RecordParser" Version="1.3.0" />
<PackageReference Include="Sep" Version="0.1.0" />
<PackageReference Include="ServiceStack.Text" Version="6.7.0" />
<PackageReference Include="Sky.Data.Csv" Version="2.5.0" />
<PackageReference Include="SoftCircuits.CsvParser" Version="4.0.0" />
Expand Down

0 comments on commit 0c6fac0

Please sign in to comment.