Skip to content

Commit

Permalink
Add Parlot to JsonBench
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jan 19, 2021
1 parent 0ef39a9 commit 32ad354
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
29 changes: 25 additions & 4 deletions Pidgin.Bench/JsonBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using Pidgin.Bench.ParlotParsers;
using Pidgin.Bench.SpracheParsers;
using Pidgin.Bench.SuperpowerParsers;
using Pidgin.Examples.Json;
Expand Down Expand Up @@ -47,7 +48,12 @@ public void BigJson_Superpower()
[Benchmark, BenchmarkCategory("Big")]
public void BigJson_FParsec()
{
Pidgin.Bench.FParsec.JsonParser.parse(_bigJson);
FParsec.JsonParser.parse(_bigJson);
}
[Benchmark, BenchmarkCategory("Big")]
public void BigJson_Parlot()
{
ParlotJsonParser.Parse(_bigJson);
}

[Benchmark(Baseline = true), BenchmarkCategory("Long")]
Expand All @@ -68,7 +74,12 @@ public void LongJson_Superpower()
[Benchmark, BenchmarkCategory("Long")]
public void LongJson_FParsec()
{
Pidgin.Bench.FParsec.JsonParser.parse(_longJson);
FParsec.JsonParser.parse(_longJson);
}
[Benchmark, BenchmarkCategory("Long")]
public void LongJson_Parlot()
{
ParlotJsonParser.Parse(_longJson);
}

[Benchmark(Baseline = true), BenchmarkCategory("Deep")]
Expand All @@ -90,7 +101,12 @@ public void DeepJson_Sprache()
[Benchmark, BenchmarkCategory("Deep")]
public void DeepJson_FParsec()
{
Pidgin.Bench.FParsec.JsonParser.parse(_deepJson);
FParsec.JsonParser.parse(_deepJson);
}
[Benchmark, BenchmarkCategory("Deep")]
public void DeepJson_Parlot()
{
ParlotJsonParser.Parse(_deepJson);
}

[Benchmark(Baseline = true), BenchmarkCategory("Wide")]
Expand All @@ -111,7 +127,12 @@ public void WideJson_Superpower()
[Benchmark, BenchmarkCategory("Wide")]
public void WideJson_FParsec()
{
Pidgin.Bench.FParsec.JsonParser.parse(_wideJson);
FParsec.JsonParser.parse(_wideJson);
}
[Benchmark, BenchmarkCategory("Wide")]
public void WideJson_Parlot()
{
ParlotJsonParser.Parse(_wideJson);
}

private static IJson BuildJson(int length, int depth, int width)
Expand Down
56 changes: 56 additions & 0 deletions Pidgin.Bench/ParlotParsers/ParlotJsonParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Parlot.Fluent;
using Pidgin.Examples.Json;

using static Parlot.Fluent.Parsers;

namespace Pidgin.Bench.ParlotParsers
{
public class ParlotJsonParser
{
private static readonly Parlot.Fluent.Parser<IJson> Json;

static ParlotJsonParser()
{
var LBrace = Terms.Char('{');
var RBrace = Terms.Char('}');
var LBracket = Terms.Char('[');
var RBracket = Terms.Char(']');
var Colon = Terms.Char(':');
var Comma = Terms.Char(',');

var String = Terms.String(StringLiteralQuotes.Double);

var jsonString =
String
.Then<IJson>(static s => new JsonString(s.ToString()));

var json = Deferred<IJson>();

var jsonArray =
Between(LBracket, Separated(Comma, json), RBracket)
.Then<IJson>(static els => new JsonArray(els.ToImmutableArray()));

var jsonMember =
String.And(Colon).And(json)
.Then(static member => new KeyValuePair<string, IJson>(member.Item1.ToString(), member.Item3));

var jsonObject =
Between(LBrace, Separated(Comma, jsonMember), RBrace)
.Then<IJson>(static kvps => new JsonObject(kvps.ToImmutableDictionary()));

Json = json.Parser = jsonString.Or(jsonArray).Or(jsonObject);
}

public static IJson Parse(string input)
{
if (Json.TryParse(input, out var result))
{
return result;
}

return null;
}
}
}
7 changes: 4 additions & 3 deletions Pidgin.Bench/Pidgin.Bench.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand All @@ -15,7 +15,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Sprache" Version="2.3.0" />
<PackageReference Include="Parlot" Version="0.0.7" />
<PackageReference Include="Sprache" Version="2.3.1" />
<PackageReference Include="Superpower" Version="2.3.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>
Expand Down

0 comments on commit 32ad354

Please sign in to comment.