Skip to content

Commit

Permalink
Reading performance improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Close committed Feb 21, 2018
1 parent 789df87 commit 05292ef
Show file tree
Hide file tree
Showing 32 changed files with 347 additions and 601 deletions.
2 changes: 1 addition & 1 deletion src/CsvHelper.Performance/CsvHelper.Performance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net45</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
31 changes: 27 additions & 4 deletions src/CsvHelper.Performance/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This file is a part of CsvHelper and is dual licensed under MS-PL and Apache 2.0.
// See LICENSE.txt for details or visit http://www.opensource.org/licenses/ms-pl.html for MS-PL and http://opensource.org/licenses/Apache-2.0 for Apache 2.0.
// https://github.com/JoshClose/CsvHelper
using CsvHelper.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -13,18 +14,18 @@ class Program
{
static void Main( string[] args )
{
//WriteField( 50, 1000000);
WriteRecords( 1000000 );

//Parse();
//ReadGetField();
//ReadGetRecords();

WriteField( 50, 1000000);
WriteRecords( 1000000 );
}

static string GetFilePath()
{
var homePath = Environment.ExpandEnvironmentVariables( "%HOMEDRIVE%%HOMEPATH%" );
var filePath = Path.Combine( homePath, "Documents", "large.csv" );
var filePath = Path.Combine( homePath, "Documents", "performance.csv" );
return filePath;
}

Expand Down Expand Up @@ -200,6 +201,28 @@ static void ReadGetRecords()
Console.WriteLine( stopwatch.Elapsed );
}

private class Data
{
public int Id { get; set; }

public string Name { get; set; }

public int Age { get; set; }

public DateTimeOffset Birthday { get; set; }
}

private class DataMap : ClassMap<Data>
{
public DataMap()
{
Map( m => m.Id ).Index( 0 );
Map( m => m.Name ).Index( 1 );
Map( m => m.Age ).Index( 2 );
Map( m => m.Birthday ).Index( 3 );
}
}

private class Columns50
{
public int Column1 { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper.Tests/Configuration/ClassMapBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private class BuilderRowFake : IReaderRow
public string[] CurrentRecord { get; }
public int Row { get; }

public IReadingContext Context
public ReadingContext Context
{
get
{
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper.Tests/Mocks/ParserMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ParserMock : IParser, IEnumerable<string[]>
private readonly Queue<string[]> rows;
private ReadingContext context;

public IReadingContext Context => context;
public ReadingContext Context => context;

public IParserConfiguration Configuration { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper.Tests/Mocks/SerializerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public List<string[]> Records
get { return records; }
}

public IWritingContext Context { get; }
public WritingContext Context { get; }

public SerializerMock( bool throwExceptionOnWrite = false )
{
Expand Down
13 changes: 11 additions & 2 deletions src/CsvHelper.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.26430.6
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6445B2A3-9E05-4ABF-AE2E-C875773B277A}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -20,7 +20,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CsvHelper", "CsvHelper\CsvH
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CsvHelper.Tests", "CsvHelper.Tests\CsvHelper.Tests.csproj", "{6859ECD8-81AE-4E74-A926-5726AAD7EE81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CsvHelper.Performance", "CsvHelper.Performance\CsvHelper.Performance.csproj", "{9DC1A5EB-4E87-4449-B5B2-241ACD5121E1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CsvHelper.Performance", "CsvHelper.Performance\CsvHelper.Performance.csproj", "{9DC1A5EB-4E87-4449-B5B2-241ACD5121E1}"
EndProject
Global
GlobalSection(Performance) = preSolution
Expand Down Expand Up @@ -103,4 +103,13 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F48D3EE-214D-45F4-AC6A-3E95C3A25E9F}
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions src/CsvHelper/BadDataException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public class BadDataException : CsvHelperException
/// Initializes a new instance of the <see cref="BadDataException"/> class.
/// </summary>
/// <param name="context">The reading context.</param>
public BadDataException( IReadingContext context ) : base( context ) { }
public BadDataException( ReadingContext context ) : base( context ) { }

/// <summary>
/// Initializes a new instance of the <see cref="BadDataException"/> class
/// with a specified error message.
/// </summary>
/// <param name="context">The reading context.</param>
/// <param name="message">The message that describes the error.</param>
public BadDataException( IReadingContext context, string message ) : base( context, message ) { }
public BadDataException( ReadingContext context, string message ) : base( context, message ) { }

/// <summary>
/// Initializes a new instance of the <see cref="BadDataException"/> class
Expand All @@ -34,6 +34,6 @@ public BadDataException( IReadingContext context, string message ) : base( conte
/// <param name="context">The reading context.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public BadDataException( IReadingContext context, string message, Exception innerException ) : base( context, message, innerException ) { }
public BadDataException( ReadingContext context, string message, Exception innerException ) : base( context, message, innerException ) { }
}
}
6 changes: 3 additions & 3 deletions src/CsvHelper/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Configuration : IReaderConfiguration, IWriterConfiguration
/// You can supply your own function to do other things like logging the issue instead of throwing an exception.
/// Arguments: isValid, headerNames, headerNameIndex, context
/// </summary>
public virtual Action<bool, string[], int, IReadingContext> HeaderValidated { get; set; } = ( isValid, headerNames, headerNameIndex, context ) =>
public virtual Action<bool, string[], int, ReadingContext> HeaderValidated { get; set; } = ( isValid, headerNames, headerNameIndex, context ) =>
{
if( isValid )
{
Expand All @@ -72,7 +72,7 @@ public class Configuration : IReaderConfiguration, IWriterConfiguration
/// like logging the issue instead of throwing an exception.
/// Arguments: headerNames, index, context
/// </summary>
public virtual Action<string[], int, IReadingContext> MissingFieldFound { get; set; } = ( headerNames, index, context ) =>
public virtual Action<string[], int, ReadingContext> MissingFieldFound { get; set; } = ( headerNames, index, context ) =>
{
var messagePostfix = $"You can ignore missing fields by setting {nameof( MissingFieldFound )} to null.";
Expand All @@ -91,7 +91,7 @@ public class Configuration : IReaderConfiguration, IWriterConfiguration
/// instead of throwing an exception.
/// Arguments: context
/// </summary>
public virtual Action<IReadingContext> BadDataFound { get; set; } = context =>
public virtual Action<ReadingContext> BadDataFound { get; set; } = context =>
{
throw new BadDataException( context, $"You can ignore bad data by setting {nameof( BadDataFound )} to null." );
};
Expand Down
2 changes: 1 addition & 1 deletion src/CsvHelper/Configuration/IParserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface IParserConfiguration
/// instead of throwing an exception.
/// Arguments: context
/// </summary>
Action<IReadingContext> BadDataFound { get; set; }
Action<ReadingContext> BadDataFound { get; set; }

/// <summary>
/// Gets or sets the character used to denote
Expand Down
4 changes: 2 additions & 2 deletions src/CsvHelper/Configuration/IReaderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public interface IReaderConfiguration : IParserConfiguration
/// You can supply your own function to do other things like logging the issue instead of throwing an exception.
/// Arguments: isValid, headerNames, headerNameIndex, context
/// </summary>
Action<bool, string[], int, IReadingContext> HeaderValidated { get; set; }
Action<bool, string[], int, ReadingContext> HeaderValidated { get; set; }

/// <summary>
/// Gets or sets the function that is called when a missing field is found. The default function will
/// throw a <see cref="MissingFieldException"/>. You can supply your own function to do other things
/// like logging the issue instead of throwing an exception.
/// Arguments: headerNames, index, context
/// </summary>
Action<string[], int, IReadingContext> MissingFieldFound { get; set; }
Action<string[], int, ReadingContext> MissingFieldFound { get; set; }

/// <summary>
/// Gets or sets the function that is called when a reading exception occurs.
Expand Down
4 changes: 2 additions & 2 deletions src/CsvHelper/CsvFieldReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace CsvHelper
/// </summary>
public partial class CsvFieldReader : IFieldReader
{
private IReadingContext context;
private ReadingContext context;
private bool disposed;

/// <summary>
/// Gets the reading context.
/// </summary>
public virtual IReadingContext Context => context;
public virtual ReadingContext Context => context;

/// <summary>
/// Gets a value indicating if the buffer is empty.
Expand Down
6 changes: 3 additions & 3 deletions src/CsvHelper/CsvHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<Authors>Josh Close</Authors>

<!-- Build -->
<Version>6.1.1</Version>
<FileVersion>6.0.0.0</FileVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion>
<Version>7.0.0</Version>
<FileVersion>7.0.0.0</FileVersion>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
<LangVersion>7.1</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
16 changes: 8 additions & 8 deletions src/CsvHelper/CsvHelperException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class CsvHelperException : Exception
/// <summary>
/// Gets the context used when reading.
/// </summary>
public IReadingContext ReadingContext { get; private set; }
public ReadingContext ReadingContext { get; private set; }

/// <summary>
/// Gets the context used when writing.
/// </summary>
public IWritingContext WritingContext { get; private set; }
public WritingContext WritingContext { get; private set; }

/// <summary>
/// Initializes a new instance of the CsvHelperException class.
Expand All @@ -44,15 +44,15 @@ internal protected CsvHelperException( string message, Exception innerException
/// <summary>
/// Initializes a new instance of the <see cref="CsvHelperException"/> class.
/// </summary>
public CsvHelperException( IReadingContext context )
public CsvHelperException( ReadingContext context )
{
ReadingContext = context;
}

/// <summary>
/// Initializes a new instance of the <see cref="CsvHelperException"/> class.
/// </summary>
public CsvHelperException( IWritingContext context )
public CsvHelperException( WritingContext context )
{
WritingContext = context;
}
Expand All @@ -63,7 +63,7 @@ public CsvHelperException( IWritingContext context )
/// </summary>
/// <param name="context">The reading context.</param>
/// <param name="message">The message that describes the error.</param>
public CsvHelperException( IReadingContext context, string message ) : base( message )
public CsvHelperException( ReadingContext context, string message ) : base( message )
{
ReadingContext = context;
}
Expand All @@ -76,7 +76,7 @@ public CsvHelperException( IReadingContext context, string message ) : base( mes
/// <param name="context">The reading context.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public CsvHelperException( IReadingContext context, string message, Exception innerException ) : base( message, innerException )
public CsvHelperException( ReadingContext context, string message, Exception innerException ) : base( message, innerException )
{
ReadingContext = context;
}
Expand All @@ -87,7 +87,7 @@ public CsvHelperException( IReadingContext context, string message, Exception in
/// </summary>
/// <param name="context">The writing context.</param>
/// <param name="message">The message that describes the error.</param>
public CsvHelperException( IWritingContext context, string message ) : base( message )
public CsvHelperException( WritingContext context, string message ) : base( message )
{
WritingContext = context;
}
Expand All @@ -100,7 +100,7 @@ public CsvHelperException( IWritingContext context, string message ) : base( mes
/// <param name="context">The writing context.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
public CsvHelperException( IWritingContext context, string message, Exception innerException ) : base( message, innerException )
public CsvHelperException( WritingContext context, string message, Exception innerException ) : base( message, innerException )
{
WritingContext = context;
}
Expand Down
Loading

0 comments on commit 05292ef

Please sign in to comment.