Skip to content

Commit d6a37de

Browse files
alex-jitbitCopilot
andcommitted
Cache common separators SearchValue, slices half-microsecond from benchmarks
Co-authored-by: Copilot <copilot@github.com>
1 parent 4a495ef commit d6a37de

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

CsvExport/CsvExport.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ public class CsvExport
7070
/// </summary>
7171
private readonly bool _includeHeaderRow;
7272

73-
private SearchValues<char> _searchValues;
73+
// cache common search values for the two most common separators to avoid SearchValues.Create (expensive)
74+
// slices half-microsecond from benchmarks
75+
private static readonly SearchValues<char> _commaSearchValues = SearchValues.Create(",\n\r\"");
76+
private static readonly SearchValues<char> _semicolonSearchValues = SearchValues.Create(";\n\r\"");
77+
78+
private readonly SearchValues<char> _searchValues;
7479

7580
/// <summary>
7681
/// Default encoding
@@ -97,9 +102,17 @@ public CsvExport(char columnSeparator = ',', bool includeColumnSeparatorDefiniti
97102
_separatorChar = columnSeparator;
98103
_includeColumnSeparatorDefinitionPreamble = includeColumnSeparatorDefinitionPreamble;
99104
_includeHeaderRow = includeHeaderRow;
100-
_searchValues = SearchValues.Create($"{columnSeparator}\n\r\"");
105+
_searchValues = GetSearchValues(columnSeparator);
101106
}
102107

108+
private static SearchValues<char> GetSearchValues(char columnSeparator) =>
109+
columnSeparator switch
110+
{
111+
',' => _commaSearchValues,
112+
';' => _semicolonSearchValues,
113+
_ => SearchValues.Create($"{columnSeparator}\n\r\"")
114+
};
115+
103116
/// <summary>
104117
/// Set a value on this column
105118
/// </summary>

0 commit comments

Comments
 (0)