Skip to content
Open
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
2 changes: 1 addition & 1 deletion performance/CsvHelper.Benchmarks/BenchmarkMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ internal class BenchmarkMain
{
static void Main(string[] args)
{
_ = BenchmarkRunner.Run<BenchmarkEnumerateRecords>();
_ = BenchmarkSwitcher.FromAssembly(typeof(BenchmarkMain).Assembly).Run(args);
}
}
81 changes: 81 additions & 0 deletions performance/CsvHelper.Benchmarks/BenchmarkShouldQuote.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using BenchmarkDotNet.Attributes;

using CsvHelper.Configuration;

using System;
using System.Globalization;
using System.IO;

namespace CsvHelper.Benchmarks;

public class BenchmarkShouldQuote
{
private const int FieldCount = 10000;
private readonly StringWriter stringWriter = new();
private string[] fields = null!;
private static readonly Type StringType = typeof(string);
[GlobalSetup]
public void GlobalSetup()
{
// Pre-compute a mix of typical fields. Real CsvWriter is created per iteration below.
var random = new Random(42);
this.fields = new string[FieldCount];
var chars = new char[10];
for (int i = 0; i < FieldCount; i++)
{
string value;
var pick = i % 10;
if (pick == 0)
{
// Field that should be quoted (contains comma)
value = "a,b,c";
}
else if (pick == 1)
{
// Field that should be quoted (contains quote)
value = "he said \"hi\"";
}
else if (pick < 6)
{
// Random lowercase strings (no special chars)
for (int j = 0; j < 10; j++)
{
chars[j] = (char)random.Next('a', 'z' + 1);
}

value = new string (chars);
}
else
{
// Integer-as-string
value = random.Next().ToString(CultureInfo.InvariantCulture);
}

this.fields[i] = value;
}
}

[GlobalCleanup]
public void GlobalCleanup()
{
this.stringWriter.Dispose();
}

[Benchmark]
public void ShouldQuote()
{
using var csv = new CsvWriter(this.stringWriter, CultureInfo.InvariantCulture, true);
int quotedCount = 0;
var f = this.fields;
for (int i = 0; i < f.Length; i++)
{
var args = new ShouldQuoteArgs(f[i], StringType, csv);
if (ConfigurationFunctions.ShouldQuote(args))
{
quotedCount++;
}
}

_ = quotedCount;
}
}
55 changes: 55 additions & 0 deletions performance/CsvHelper.Benchmarks/BenchmarkWriteRecords.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using BenchmarkDotNet.Attributes;

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace CsvHelper.Benchmarks;

public class BenchmarkWriteRecords
{
private const int entryCount = 10000;
private readonly MemoryStream stream = new();
private List<Simple> records = new();
public class Simple
{
public int Id { get; set; }
public string Name { get; set; }
}

[GlobalSetup]
public void GlobalSetup()
{
var random = new Random(42); // Pick a known seed to keep things consistent
var chars = new char[10];
string getRandomString()
{
for (int i = 0; i < 10; ++i)
chars[i] = (char)random.Next('a', 'z' + 1);
return new string (chars);
}

this.records = new List<Simple>(entryCount);
for (int i = 0; i < entryCount; ++i)
{
this.records.Add(new Simple() { Id = random.Next(), Name = getRandomString() });
}
}

[GlobalCleanup]
public void GlobalCleanup()
{
this.stream.Dispose();
}

[Benchmark]
public void WriteRecords()
{
this.stream.Position = 0;
this.stream.SetLength(0);
using var streamWriter = new StreamWriter(this.stream, null, -1, true);
using var csv = new CsvWriter(streamWriter, CultureInfo.InvariantCulture, true);
csv.WriteRecords(this.records);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.0" />
<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="18.7.37220.1" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 34 additions & 6 deletions src/CsvHelper/Configuration/ConfigurationFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,42 @@ public static bool ShouldQuote(ShouldQuoteArgs args)
return false;
}

var length = field.Length;

// Cheapest checks first: leading/trailing space.
if (field[0] == ' ' || field[length - 1] == ' ')
{
return true;
}

var quote = config.Quote;
var delimiter = config.Delimiter;
var isNewLineSet = config.IsNewLineSet;

// Fast path: common configuration (single-char delimiter, default newline handling).
// Scan the field once, comparing against quote, delimiter, '\r', and '\n'.
if (!isNewLineSet && delimiter.Length == 1)
{
var delimiterChar = delimiter[0];
for (int i = 0; i < length; i++)
{
var c = field[i];
if (c == quote || c == delimiterChar || c == '\r' || c == '\n')
{
return true;
}
}

return false;
}

// Slow path: multi-char delimiter or custom NewLine.
var shouldQuote =
(
field[0] == ' ' // Starts with a space
|| field[field.Length - 1] == ' ' // Ends with a space
|| field.Contains(config.Quote) // Contains quote
|| !config.IsNewLineSet && field.IndexOfAny(lineEndingChars) > -1 // Contains line ending characters
|| config.IsNewLineSet && field.Contains(config.NewLine) // Contains newline
|| (config.Delimiter.Length > 0 && field.Contains(config.Delimiter)) // Contains delimiter
field.Contains(quote) // Contains quote
|| !isNewLineSet && field.IndexOfAny(lineEndingChars) > -1 // Contains line ending characters
|| isNewLineSet && field.Contains(config.NewLine) // Contains newline
|| (delimiter.Length > 0 && field.Contains(delimiter)) // Contains delimiter
);

return shouldQuote;
Expand Down
Loading