Skip to content

Commit

Permalink
Merge pull request #1 from dterziev/feature/net8
Browse files Browse the repository at this point in the history
Upgrade to .NET 8
  • Loading branch information
dterziev authored Oct 1, 2023
2 parents 883da56 + e46353a commit d11d7ba
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.13.2" />
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.8" />
<PackageVersion Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.8" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.5" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageVersion Include="xunit" Version="2.5.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.1" />
</ItemGroup>
</Project>
15 changes: 13 additions & 2 deletions EnumerableDataReaderAdapter.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2019
# Visual Studio Version 17
VisualStudioVersion = 17.8.34112.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{19031506-5145-42F6-9AB6-AE7C1E4ACF80}"
EndProject
Expand All @@ -15,6 +15,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EnumerableDataReaderAdapter
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EnumerableDataReaderAdapter.Benchmarks", "benchmarks\EnumerableDataReaderAdapter.Benchmarks\EnumerableDataReaderAdapter.Benchmarks.csproj", "{80F9D7ED-93C8-4805-89A4-3B581AB00F34}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C5F1FAE8-92F2-4FF8-B6C8-A2AFE3A67F8B}"
ProjectSection(SolutionItems) = preProject
.gitattributes = .gitattributes
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
LICENSE = LICENSE
nuget.config = nuget.config
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
89 changes: 35 additions & 54 deletions benchmarks/EnumerableDataReaderAdapter.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public DataStructure(

[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net60)]
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net70)]
[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net80)]
[RPlotExporter, RankColumn]
[MemoryDiagnoser]
public class EnumerableDataReaderBenchmark
{
private DataStructure[] data = default!;
private object[] buffer = default!;
private DataStructure[] _data = default!;
private object[] _buffer = default!;

static readonly ColumnMappings<DataStructure> _mappingDelegates = new ColumnMappings<DataStructure>()
.Add(nameof(DataStructure.StringField), typeof(string), p => p.StringField)
Expand All @@ -48,137 +49,117 @@ public class EnumerableDataReaderBenchmark
[GlobalSetup]
public void Setup()
{
data = Enumerable.Range(1, N)
_data = Enumerable.Range(1, N)
.Select(i => new DataStructure($"{i}", i, (i % 2) == 0 ? (int?)null : i))
.ToArray();
buffer = new object[3];
_buffer = new object[3];
}

[Benchmark(Baseline = true)]
public void DefaultMapping_ByColumnIndex()
{
using (var reader = data.ToDataReader())
{
while (reader.Read()) GetDataByColumnIndex(reader);
}
using var reader = _data.ToDataReader();
while (reader.Read()) GetDataByColumnIndex(reader);
}

[Benchmark]
public void MappingExpressions_ByColumnIndex()
{
using (var reader = data.ToDataReader(
using var reader = _data.ToDataReader(
mapping => mapping
.Add(p => p.StringField)
.Add(p => p.IntField)
.Add(p => p.NullableIntField)
))
{
while (reader.Read()) GetDataByColumnIndex(reader);
}
);
while (reader.Read()) GetDataByColumnIndex(reader);
}

[Benchmark]
public void MappingExpressions_ByColumnIndex_CachedMapping()
{
using (var reader = data.ToDataReader(_mappingExpressions))
{
while (reader.Read()) GetDataByColumnIndex(reader);
}
using var reader = _data.ToDataReader(_mappingExpressions);
while (reader.Read()) GetDataByColumnIndex(reader);
}

[Benchmark]
public void MappingDelegates_ByColumnIndex()
{
using (var reader = data.ToDataReader(
using var reader = _data.ToDataReader(
mapping => mapping
.Add(nameof(DataStructure.StringField), typeof(string), p => p.StringField)
.Add(nameof(DataStructure.IntField), typeof(int), p => p.IntField)
.Add(nameof(DataStructure.NullableIntField), typeof(int?), p => p.NullableIntField)
))
{
while (reader.Read()) GetDataByColumnIndex(reader);
}
);
while (reader.Read()) GetDataByColumnIndex(reader);
}

[Benchmark]
public void MappingDelegates_ByColumnIndex_CachedMapping()
{
using (var reader = data.ToDataReader(_mappingDelegates))
{
while (reader.Read()) GetDataByColumnIndex(reader);
}
using var reader = _data.ToDataReader(_mappingDelegates);
while (reader.Read()) GetDataByColumnIndex(reader);
}

[Benchmark()]
public void DefaultMapping_ByColumnName()
{
using (var reader = data.ToDataReader())
{
while (reader.Read()) GetDataByColumnName(reader);
}
using var reader = _data.ToDataReader();
while (reader.Read()) GetDataByColumnName(reader);
}

[Benchmark]
public void MappingExpressions_ByColumnName()
{
using (var reader = data.ToDataReader(
using var reader = _data.ToDataReader(
mapping => mapping
.Add(p => p.StringField)
.Add(p => p.IntField)
.Add(p => p.NullableIntField)
))
{
while (reader.Read()) GetDataByColumnName(reader);
}
);
while (reader.Read()) GetDataByColumnName(reader);
}

[Benchmark]
public void MappingExpressions_ByColumnName_CachedMapping()
{
using (var reader = data.ToDataReader(_mappingExpressions))
{
while (reader.Read()) GetDataByColumnName(reader);
}
using var reader = _data.ToDataReader(_mappingExpressions);
while (reader.Read()) GetDataByColumnName(reader);
}


[Benchmark]
public void MappingDelegates_ByColumnName()
{
using (var reader = data.ToDataReader(
using var reader = _data.ToDataReader(
mapping => mapping
.Add(nameof(DataStructure.StringField), typeof(string), p => p.StringField)
.Add(nameof(DataStructure.IntField), typeof(int), p => p.IntField)
.Add(nameof(DataStructure.NullableIntField), typeof(int?), p => p.NullableIntField)
))
{
while (reader.Read()) GetDataByColumnName(reader);
}
);
while (reader.Read()) GetDataByColumnName(reader);
}

[Benchmark]
public void MappingDelegates_ByColumnName_CachedMapping()
{
using (var reader = data.ToDataReader(_mappingDelegates))
{
while (reader.Read()) GetDataByColumnName(reader);
}
using var reader = _data.ToDataReader(_mappingDelegates);
while (reader.Read()) GetDataByColumnName(reader);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GetDataByColumnIndex(IDataReader reader)
{
buffer[0] = reader[0];
buffer[1] = reader[1];
buffer[2] = reader[2];
_buffer[0] = reader[0];
_buffer[1] = reader[1];
_buffer[2] = reader[2];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GetDataByColumnName(IDataReader reader)
{
buffer[0] = reader[nameof(DataStructure.StringField)];
buffer[1] = reader[nameof(DataStructure.IntField)];
buffer[2] = reader[nameof(DataStructure.NullableIntField)];
_buffer[0] = reader[nameof(DataStructure.StringField)];
_buffer[1] = reader[nameof(DataStructure.IntField)];
_buffer[2] = reader[nameof(DataStructure.NullableIntField)];
}

}
Expand Down
13 changes: 4 additions & 9 deletions src/EnumerableDataReaderAdapter/ColumnMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static ColumnMappings()
}).ToArray();
}

private List<(string ColumnName, Type ColumnType, Func<T, object?> ValueGetter)> _mappings = new();
private readonly List<(string ColumnName, Type ColumnType, Func<T, object?> ValueGetter)> _mappings = new();

public ColumnMappings<T> Add(string columnName, Type type, Func<T, object?> valueGetter)
{
Expand Down Expand Up @@ -68,15 +68,10 @@ public ColumnMappings<T> Add(Expression<Func<T, object?>> propertyExpression)
}

var body = propertyExpression.Body;
var memberExpression = body is UnaryExpression ue
var memberExpression = (body is UnaryExpression ue
? ue.Operand as MemberExpression
: body as MemberExpression;

if (memberExpression is null)
{
throw new ArgumentException("Expected a member expression.", nameof(propertyExpression));
}

: body as MemberExpression)
?? throw new ArgumentException("Expected a member expression.", nameof(propertyExpression));
_mappings.Add(
(ColumnName: memberExpression.Member.Name,
ColumnType: memberExpression.Type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

</Project>
8 changes: 4 additions & 4 deletions src/EnumerableDataReaderAdapter/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private sealed class EnumerableReaderAdapter<T> : DbDataReader
private bool _isClosed = false;
private IEnumerator<T> _enumerator;
private T _current = default!;
private Lazy<Dictionary<string, int>> _columnLookup;
private readonly Lazy<Dictionary<string, int>> _columnLookup;
private long _rowCount = 0;

public EnumerableReaderAdapter(
Expand Down Expand Up @@ -68,7 +68,7 @@ public override IEnumerator GetEnumerator()

public override DataTable GetSchemaTable()
{
DataTable table = new DataTable("SchemaTable");
DataTable table = new("SchemaTable");
table.Columns.Add(SchemaTableColumn.ColumnName, typeof(string));
table.Columns.Add(SchemaTableColumn.ColumnOrdinal, typeof(int));
table.Columns.Add(SchemaTableColumn.ColumnSize, typeof(int));
Expand Down Expand Up @@ -155,9 +155,9 @@ public override int GetValues(object?[] values)
public override Type GetFieldType(int i) => null!;
public override bool GetBoolean(int i) => (bool)GetValue(i);
public override byte GetByte(int i) => (byte)GetValue(i);
public override long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferoffset, int length) => throw new NotSupportedException();
public override long GetBytes(int i, long fieldOffset, byte[]? buffer, int bufferOffset, int length) => throw new NotSupportedException();
public override char GetChar(int i) => (char)GetValue(i);
public override long GetChars(int i, long fieldoffset, char[]? buffer, int bufferoffset, int length) => throw new NotSupportedException();
public override long GetChars(int i, long fieldOffset, char[]? buffer, int bufferOffset, int length) => throw new NotSupportedException();
public override Guid GetGuid(int i) => (Guid)GetValue(i);
public override short GetInt16(int i) => (short)GetValue(i);
public override int GetInt32(int i) => (int)GetValue(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit d11d7ba

Please sign in to comment.