Skip to content

Commit

Permalink
Substring appears to allocate faster than actually allocating, conver…
Browse files Browse the repository at this point in the history
…ting some operations to substring accordingly
  • Loading branch information
kevin-montrose committed Feb 26, 2012
1 parent 965f3cd commit c4af501
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions FluentStringParser/Directives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ internal override void Emit(ILGenerator il)
il.MarkLabel(finished);
}

[ExcludeFromCodeCoverage]
internal override Action<string, T> GetOnFailure()
{
return (s, o) => { };
Expand Down
10 changes: 9 additions & 1 deletion FluentStringParser/ILHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,15 @@ internal static void NewParseImpl(ILGenerator il, Type memberType, string format

if (memberType == typeof(string))
{
il.Emit(OpCodes.Newobj, strConst); // string *built
var substring = typeof(string).GetMethod("Substring", new[]{typeof(int), typeof(int)});

il.StoreScratchInt(); // start char[] *built
il.StoreScratchInt2(); // char[] *built
il.Emit(OpCodes.Pop); // *built
il.Emit(OpCodes.Ldarg_0); // string *built
il.LoadScratchInt2(); // start string *built
il.LoadScratchInt(); // length start string *built
il.Emit(OpCodes.Call, substring); // string *built

return;
}
Expand Down
10 changes: 10 additions & 0 deletions FluentStringParserTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ public void HaProxyLogs()
[TestMethod]
public void SpeedComparison()
{
// THIS TEST IS EXPECTED TO FAIL
// String performance in the hand written case is *very good*, if you
// can gin up better code for this case in the FSP, submit a patch.
// It's an interesting project.

string[] lines;
using (var input = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("FluentStringParserTests.TestData.txt")))
{
Expand Down Expand Up @@ -1098,6 +1103,11 @@ class StringPerfObject
[TestMethod]
public void SimpleStringPerf()
{
// THIS TEST IS EXPECTED TO FAIL
// String performance in the hand written case is *very good*, if you
// can gin up better code for this case in the FSP, submit a patch.
// It's an interesting project.

var regex = new Regex(@"([^,]*?),([^,]*?),([^,]*?),([^,]*)", RegexOptions.Compiled);
var parserTask =
FSBuilder
Expand Down

0 comments on commit c4af501

Please sign in to comment.