Skip to content

Commit

Permalink
Added v12
Browse files Browse the repository at this point in the history
v12 uses the stringbuilder passed into the raw stream handling. This drops allocations from 32kb to 16kb.
  • Loading branch information
indy-singh committed Jun 7, 2018
1 parent 0c04c6b commit e8f4bee
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,5 @@ paket-files/

# JetBrains Rider
.idea/
*.sln.iml
*.sln.iml
StringsAreEvil/example-input.csv
5 changes: 4 additions & 1 deletion StringsAreEvil/ILineParser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace StringsAreEvil
using System.Text;

namespace StringsAreEvil
{
public interface ILineParser
{
void ParseLine(string line);
void ParseLine(char[] line);
void Dump();
void ParseLine(StringBuilder line);
}
}
6 changes: 6 additions & 0 deletions StringsAreEvil/LineParserV01.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace StringsAreEvil
{
Expand Down Expand Up @@ -34,5 +35,10 @@ public void Dump()
{
File.WriteAllLines(@"..\..\v01.txt", list.Select(x => x.ToString()));
}

public void ParseLine(StringBuilder line)
{

}
}
}
9 changes: 8 additions & 1 deletion StringsAreEvil/LineParserV02.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace StringsAreEvil
using System.Text;

namespace StringsAreEvil
{
/// <summary>
/// Stats:-
Expand Down Expand Up @@ -27,5 +29,10 @@ public void ParseLine(char[] line)
public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}
}
}
9 changes: 8 additions & 1 deletion StringsAreEvil/LineParserV03.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace StringsAreEvil
using System.Text;

namespace StringsAreEvil
{
/// <summary>
/// Stats:-
Expand Down Expand Up @@ -28,5 +30,10 @@ public void ParseLine(char[] line)
public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}
}
}
5 changes: 5 additions & 0 deletions StringsAreEvil/LineParserV04.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private decimal ParseSectionAsDecimal(int start, int end, string line)
{
var sb = new StringBuilder();
Expand Down
5 changes: 5 additions & 0 deletions StringsAreEvil/LineParserV05.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private decimal ParseSectionAsDecimal(int start, int end, string line)
{
_stringBuilder.Clear();
Expand Down
5 changes: 5 additions & 0 deletions StringsAreEvil/LineParserV06.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private decimal ParseSectionAsDecimal(int start, int end, string line)
{
_stringBuilder.Clear();
Expand Down
5 changes: 5 additions & 0 deletions StringsAreEvil/LineParserV07.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private decimal ParseSectionAsDecimal(int start, int end, string line)
{
_stringBuilder.Clear();
Expand Down
6 changes: 6 additions & 0 deletions StringsAreEvil/LineParserV08.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Text;

namespace StringsAreEvil
{
Expand Down Expand Up @@ -51,6 +52,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private decimal ParseSectionAsDecimal(string line, int start, int end)
{
decimal value = 0;
Expand Down
9 changes: 8 additions & 1 deletion StringsAreEvil/LineParserV09.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace StringsAreEvil
using System.Text;

namespace StringsAreEvil
{
/// <summary>
/// Stats:-
Expand Down Expand Up @@ -34,6 +36,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private static decimal ParseSectionAsDecimal(string line, int numberOfCommasToSkip)
{
decimal val = 0;
Expand Down
9 changes: 8 additions & 1 deletion StringsAreEvil/LineParserV10.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace StringsAreEvil
using System.Text;

namespace StringsAreEvil
{
/// <summary>
/// Stats:-
Expand Down Expand Up @@ -32,6 +34,11 @@ public void Dump()
{
}

public void ParseLine(StringBuilder line)
{

}

private static decimal ParseSectionAsDecimal(string line, int numberOfCommasToSkip)
{
decimal val = 0;
Expand Down
6 changes: 6 additions & 0 deletions StringsAreEvil/LineParserV11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace StringsAreEvil
{
Expand Down Expand Up @@ -41,6 +42,11 @@ public void Dump()
File.WriteAllLines(@"..\..\v11.txt", list.Select(x => x.ToString()));
}

public void ParseLine(StringBuilder line)
{

}

private static decimal ParseSectionAsDecimal(char[] line, int numberOfCommasToSkip)
{
decimal val = 0;
Expand Down
148 changes: 148 additions & 0 deletions StringsAreEvil/LineParserV12.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace StringsAreEvil
{
/// <summary>
/// Stats:-
/// Took: 6,328 ms
/// Allocated: 16 kb
/// Peak Working Set: 10,780 kb
///
/// Change:-
/// Passed into the stringbuilder
/// </summary>
public sealed class LineParserV12 : ILineParser
{
private List<ValueHolderAsStruct> list = new List<ValueHolderAsStruct>();

public void ParseLine(string line)
{
}

public void ParseLine(char[] line)
{

}

public void Dump()
{
File.WriteAllLines(@"..\..\v11.txt", list.Select(x => x.ToString()));
}

public void ParseLine(StringBuilder line)
{
if (line[0] == 'M' && line[1] == 'N' && line[2] == 'O')
{
int elementId = ParseSectionAsInt(line, 1); // equal to parts[1] - element id
int vehicleId = ParseSectionAsInt(line, 2); // equal to parts[2] - vehicle id
int term = ParseSectionAsInt(line, 3); // equal to parts[3] - term
int mileage = ParseSectionAsInt(line, 4); // equal to parts[4] - mileage
decimal value = ParseSectionAsDecimal(line, 5); // equal to parts[5] - value
var valueHolder = new ValueHolderAsStruct(elementId, vehicleId, term, mileage, value);
//list.Add(valueHolder);

}
}

private static decimal ParseSectionAsDecimal(StringBuilder line, int numberOfCommasToSkip)
{
decimal val = 0;
bool seenDot = false;
ulong fractionCounter = 10;
int counter = 0;
bool flip = false;

for (var index = 0; index < line.Length; index++)
{
// move along the line until we have skipped the required amount of commas
if (line[index] == ',')
{
counter++;

if (counter > numberOfCommasToSkip)
{
break;
}
continue;
}

// we have skipped enough commas, the next section before the upcoming comma is what we are interested in
if (counter == numberOfCommasToSkip)
{
// the number is a negative means we have to flip it at the end.
if (line[index] == '-')
{
flip = true;
continue;
}

if (line[index] == '.')
{
seenDot = true;
continue;
}

// before the . eg; 12.34 this looks for the 12
if (char.IsNumber(line[index]) && seenDot == false)
{
val *= 10;
val += line[index] - '0';
continue;
}

// after the . eg; 12.34 this looks for the 34
if (char.IsNumber(line[index]) && seenDot == true)
{
val += decimal.Divide(line[index] - '0', fractionCounter);
fractionCounter *= 10;
continue;
}
}
}

return flip ? -val : val;
}

private static int ParseSectionAsInt(StringBuilder line, int numberOfCommasToSkip)
{
int val = 0;
int counter = 0;
bool flip = false;

for (var index = 0; index < line.Length; index++)
{
// move along the line until we have skipped the required amount of commas
if (line[index] == ',')
{
counter++;

if (counter > numberOfCommasToSkip)
{
break;
}

continue;
}

// we have skipped enough commas, the next section before the upcoming comma is what we are interested in
if (counter == numberOfCommasToSkip)
{
// the number is a negative means we have to flip it at the end.
if (line[index] == '-')
{
flip = true;
continue;
}

val *= 10;
val += line[index] - '0';
}
}

return flip ? -val : val;
}
}
}
Loading

0 comments on commit e8f4bee

Please sign in to comment.