Skip to content

Commit

Permalink
Merge pull request #21 from ronnygunawan/streaming
Browse files Browse the repository at this point in the history
Streaming serializing & deserializing
  • Loading branch information
ronnygunawan authored Nov 19, 2023
2 parents af25355 + 4650bf6 commit 024e5dc
Show file tree
Hide file tree
Showing 7 changed files with 797 additions and 11 deletions.
28 changes: 21 additions & 7 deletions Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public string CsvSerializerV2Serialize() {
return Csv.CsvSerializer.Serialize(_data);
}

[Benchmark]
public string CsvSerializerStreamingSerialize() {
using MemoryStream memoryStream = new();
using StreamWriter streamWriter = new(memoryStream, Encoding.UTF8);
Csv.CsvSerializer.Serialize(streamWriter, _data);
memoryStream.Position = 0;
using StreamReader streamReader = new(memoryStream, Encoding.UTF8);
return streamReader.ReadToEnd();
}

[Benchmark]
public string CsvHelperSerialize() {
using MemoryStream memoryStream = new();
Expand Down Expand Up @@ -209,21 +219,25 @@ public Model[] CsvSerializerV2Deserialize() {
}

[Benchmark]
public List<Model> CsvHelperDeserialize() {
public void CsvSerializerStreamingDeserialize() {
using MemoryStream memoryStream = new(Encoding.UTF8.GetBytes(_csv));
using StreamReader streamReader = new(memoryStream);
foreach (Model _ in Csv.CsvSerializer.Deserialize<Model>(streamReader, hasHeaders: true)) { }
}

[Benchmark]
public void CsvHelperDeserialize() {
using MemoryStream memoryStream = new(Encoding.UTF8.GetBytes(_csv));
using StreamReader streamReader = new(memoryStream);
using CsvHelper.CsvReader csvReader = new(streamReader, CultureInfo.InvariantCulture);
return csvReader.GetRecords<Model>().ToList();
foreach (Model _ in csvReader.GetRecords<Model>()) { }
}

[Benchmark]
public List<Model> RecordParserRead() {
List<Model> items = new();
public void RecordParserRead() {
foreach (string line in _csv.Split("\r\n").Skip(1)) {
Model item = RecordParserReader.Parse(line.AsSpan());
items.Add(item);
_ = RecordParserReader.Parse(line.AsSpan());
}
return items;
}
}

Expand Down
2 changes: 1 addition & 1 deletion CsvSerializer/CsvSerializer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<RepositoryUrl>https://github.com/ronnygunawan/csv-serializer</RepositoryUrl>
<PackageTags>csv serializer deserializer parser core</PackageTags>
<Description>Fast CSV to object serializer and deserializer.</Description>
<Version>2.0.4</Version>
<Version>2.0.5</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
Expand Down
Loading

0 comments on commit 024e5dc

Please sign in to comment.