From 32ad3547ffd73128201999228811dde51eb42064 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Tue, 19 Jan 2021 17:19:44 +0200 Subject: [PATCH] Add Parlot to JsonBench --- Pidgin.Bench/JsonBench.cs | 29 ++++++++-- .../ParlotParsers/ParlotJsonParser.cs | 56 +++++++++++++++++++ Pidgin.Bench/Pidgin.Bench.csproj | 7 ++- 3 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 Pidgin.Bench/ParlotParsers/ParlotJsonParser.cs diff --git a/Pidgin.Bench/JsonBench.cs b/Pidgin.Bench/JsonBench.cs index ea065744..7f78a7ba 100644 --- a/Pidgin.Bench/JsonBench.cs +++ b/Pidgin.Bench/JsonBench.cs @@ -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; @@ -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")] @@ -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")] @@ -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")] @@ -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) diff --git a/Pidgin.Bench/ParlotParsers/ParlotJsonParser.cs b/Pidgin.Bench/ParlotParsers/ParlotJsonParser.cs new file mode 100644 index 00000000..bda5ac9b --- /dev/null +++ b/Pidgin.Bench/ParlotParsers/ParlotJsonParser.cs @@ -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 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(static s => new JsonString(s.ToString())); + + var json = Deferred(); + + var jsonArray = + Between(LBracket, Separated(Comma, json), RBracket) + .Then(static els => new JsonArray(els.ToImmutableArray())); + + var jsonMember = + String.And(Colon).And(json) + .Then(static member => new KeyValuePair(member.Item1.ToString(), member.Item3)); + + var jsonObject = + Between(LBrace, Separated(Comma, jsonMember), RBrace) + .Then(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; + } + } +} \ No newline at end of file diff --git a/Pidgin.Bench/Pidgin.Bench.csproj b/Pidgin.Bench/Pidgin.Bench.csproj index 76b9e74c..0496757d 100755 --- a/Pidgin.Bench/Pidgin.Bench.csproj +++ b/Pidgin.Bench/Pidgin.Bench.csproj @@ -2,9 +2,9 @@ Exe - netcoreapp3.0 + net5.0 false - 8.0 + 9.0 enable @@ -15,7 +15,8 @@ - + +