Skip to content

Commit

Permalink
Use latest C# features (#17)
Browse files Browse the repository at this point in the history
* Use latest C# features

* Update README
  • Loading branch information
shibayan authored Mar 4, 2022
1 parent 7c19212 commit 3a01492
Show file tree
Hide file tree
Showing 14 changed files with 737 additions and 770 deletions.
5 changes: 3 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ csharp_new_line_between_query_expression_clauses = true
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_case_contents_when_block = true
csharp_indent_switch_labels = true
csharp_indent_labels = one_less_than_current

Expand Down Expand Up @@ -83,7 +83,7 @@ dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
csharp_using_directive_placement = outside_namespace:suggestion
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true
csharp_prefer_braces = true:refactoring
csharp_prefer_braces = true:error
csharp_preserve_single_line_blocks = true:none
csharp_preserve_single_line_statements = false:none
csharp_prefer_static_local_function = true:suggestion
Expand Down Expand Up @@ -131,6 +131,7 @@ csharp_style_conditional_delegate_call = true:suggestion
csharp_style_prefer_index_operator = false:none
csharp_style_prefer_range_operator = false:none
csharp_style_pattern_local_over_anonymous_function = false:none
csharp_style_namespace_declarations = file_scoped:warning

# Space preferences
csharp_space_after_cast = false
Expand Down
69 changes: 34 additions & 35 deletions CsvHelper.FastDynamic.Performance/Internal/CsvReaderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,55 @@
using System.Collections.Generic;
using System.Linq;

namespace CsvHelper.FastDynamic.Performance.Internal
namespace CsvHelper.FastDynamic.Performance.Internal;

internal static class CsvReaderExtension
{
internal static class CsvReaderExtension
{
internal static IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords(this CsvReader csvReader)
=> csvReader.EnumerateDictionaryRecords().ToArray();
internal static IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords(this CsvReader csvReader)
=> csvReader.EnumerateDictionaryRecords().ToArray();

internal static IEnumerable<IDictionary<string, object>> EnumerateDictionaryRecords(this CsvReader csvReader)
internal static IEnumerable<IDictionary<string, object>> EnumerateDictionaryRecords(this CsvReader csvReader)
{
if (csvReader.Configuration.HasHeaderRecord && csvReader.HeaderRecord == null)
{
if (csvReader.Configuration.HasHeaderRecord && csvReader.HeaderRecord == null)
if (!csvReader.Read())
{
if (!csvReader.Read())
{
yield break;
}

csvReader.ReadHeader();
yield break;
}

var headerRecord = csvReader.HeaderRecord
.Select((x, i) => csvReader.Configuration.PrepareHeaderForMatch(new PrepareHeaderForMatchArgs(x, i)))
.ToArray();
csvReader.ReadHeader();
}

var headerRecord = csvReader.HeaderRecord
.Select((x, i) => csvReader.Configuration.PrepareHeaderForMatch(new PrepareHeaderForMatchArgs(x, i)))
.ToArray();

while (csvReader.Read())
while (csvReader.Read())
{
Dictionary<string, object> record;

try
{
Dictionary<string, object> record;
record = new Dictionary<string, object>(headerRecord.Length);

try
for (var i = 0; i < headerRecord.Length; i++)
{
record = new Dictionary<string, object>(headerRecord.Length);

for (var i = 0; i < headerRecord.Length; i++)
{
record[headerRecord[i]] = csvReader.Parser[i];
}
record[headerRecord[i]] = csvReader.Parser[i];
}
catch (Exception ex)
{
var readerException = new ReaderException(csvReader.Context, "An unexpected error occurred.", ex);

if (csvReader.Configuration.ReadingExceptionOccurred?.Invoke(new ReadingExceptionOccurredArgs(readerException)) ?? true)
{
throw readerException;
}
}
catch (Exception ex)
{
var readerException = new ReaderException(csvReader.Context, "An unexpected error occurred.", ex);

continue;
if (csvReader.Configuration.ReadingExceptionOccurred?.Invoke(new ReadingExceptionOccurredArgs(readerException)) ?? true)
{
throw readerException;
}

yield return record;
continue;
}

yield return record;
}
}
}
15 changes: 4 additions & 11 deletions CsvHelper.FastDynamic.Performance/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using BenchmarkDotNet.Running;

namespace CsvHelper.FastDynamic.Performance
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<ReaderBenchmark>();
BenchmarkRunner.Run<WriterBenchmark>();
}
}
}
using CsvHelper.FastDynamic.Performance;

BenchmarkRunner.Run<ReaderBenchmark>();
BenchmarkRunner.Run<WriterBenchmark>();
55 changes: 27 additions & 28 deletions CsvHelper.FastDynamic.Performance/ReaderBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,41 @@

using CsvHelper.FastDynamic.Performance.Internal;

namespace CsvHelper.FastDynamic.Performance
namespace CsvHelper.FastDynamic.Performance;

[MemoryDiagnoser]
public class ReaderBenchmark
{
[MemoryDiagnoser]
public class ReaderBenchmark
{
private const string SampleCsvFile = @".\sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv";
private const string SampleCsvFile = @".\sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv";

public ReaderBenchmark()
{
_sampleCsvData = File.ReadAllText(SampleCsvFile);
}
public ReaderBenchmark()
{
_sampleCsvData = File.ReadAllText(SampleCsvFile);
}

private readonly string _sampleCsvData;
private readonly string _sampleCsvData;

[Benchmark]
public IReadOnlyList<dynamic> GetRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);
[Benchmark]
public IReadOnlyList<dynamic> GetRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);

return csvReader.GetRecords<dynamic>().ToArray();
}
return csvReader.GetRecords<dynamic>().ToArray();
}

[Benchmark]
public IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);
[Benchmark]
public IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);

return csvReader.GetDictionaryRecords();
}
return csvReader.GetDictionaryRecords();
}

[Benchmark]
public IReadOnlyList<dynamic> GetDynamicRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);
[Benchmark]
public IReadOnlyList<dynamic> GetDynamicRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);

return csvReader.GetDynamicRecords();
}
return csvReader.GetDynamicRecords();
}
}
51 changes: 21 additions & 30 deletions CsvHelper.FastDynamic.Performance/WriterBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,36 @@

using BenchmarkDotNet.Attributes;

using CsvHelper.FastDynamic.Performance.Internal;
namespace CsvHelper.FastDynamic.Performance;

namespace CsvHelper.FastDynamic.Performance
[MemoryDiagnoser]
public class WriterBenchmark
{
[MemoryDiagnoser]
public class WriterBenchmark
{
private const string SampleCsvFile = @".\sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv";
private const string SampleCsvFile = @".\sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv";

public WriterBenchmark()
public WriterBenchmark()
{
using (var csvReader = new CsvReader(new StreamReader(SampleCsvFile), CultureInfo.InvariantCulture))
{
using (var csvReader = new CsvReader(new StreamReader(SampleCsvFile), CultureInfo.InvariantCulture))
{
_dynamicCsvData = csvReader.GetDynamicRecords();
}

using (var csvReader = new CsvReader(new StreamReader(SampleCsvFile), CultureInfo.InvariantCulture))
{
_dictionaryCsvData = csvReader.GetDictionaryRecords();
}
_dynamicCsvData = csvReader.GetDynamicRecords();
}
}

private readonly IReadOnlyList<dynamic> _dynamicCsvData;
private readonly IReadOnlyList<IDictionary<string, object>> _dictionaryCsvData;
private readonly IReadOnlyList<dynamic> _dynamicCsvData;

[Benchmark]
public void WriteRecords_DynamicObject()
{
using var csvWriter = new CsvWriter(new StringWriter(), CultureInfo.InvariantCulture);
[Benchmark]
public void WriteRecords_DynamicObject()
{
using var csvWriter = new CsvWriter(new StringWriter(), CultureInfo.InvariantCulture);

csvWriter.WriteRecords(_dynamicCsvData);
}
csvWriter.WriteRecords(_dynamicCsvData);
}

[Benchmark]
public void WriteDynamicRecords_DynamicObject()
{
using var csvWriter = new CsvWriter(new StringWriter(), CultureInfo.InvariantCulture);
[Benchmark]
public void WriteDynamicRecords_DynamicObject()
{
using var csvWriter = new CsvWriter(new StringWriter(), CultureInfo.InvariantCulture);

csvWriter.WriteDynamicRecords(_dynamicCsvData);
}
csvWriter.WriteDynamicRecords(_dynamicCsvData);
}
}
Loading

0 comments on commit 3a01492

Please sign in to comment.