Skip to content

Commit

Permalink
Remove Lookup usage to improve performance (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Dec 2, 2024
1 parent c6d92c8 commit d262d07
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ csharp_style_prefer_range_operator = false
dotnet_diagnostic.IDE1006.severity = silent
csharp_prefer_system_threading_lock = true:suggestion

# IDE0078: Use pattern matching
dotnet_diagnostic.IDE0078.severity = silent

[*.{cs,vb}]
#### Naming styles ####

Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageVersion Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />

<!-- Common to all TFMs -->
<PackageVersion Include="Parlot" Version="1.2.1" />
<PackageVersion Include="Parlot" Version="1.2.2" />
<PackageVersion Include="TimeZoneConverter" Version="6.1.0" />

<!-- Benchmarks -->
Expand Down
17 changes: 15 additions & 2 deletions Fluid.Benchmarks/FluidBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes;

namespace Fluid.Benchmarks
{
[MemoryDiagnoser, ShortRunJob]
[MemoryDiagnoser]
public class FluidBenchmarks : BaseBenchmarks
{
private readonly TemplateOptions _options = new TemplateOptions();
private readonly FluidParser _parser = new FluidParser();
private readonly IFluidTemplate _fluidTemplate;
private readonly FluidParser _compiledParser = new FluidParser().Compile();

public FluidBenchmarks()
{
Expand All @@ -28,6 +29,18 @@ public override object ParseBig()
return _parser.Parse(BlogPostTemplate);
}

[Benchmark]
public object ParseCompiled()
{
return _compiledParser.Parse(ProductTemplate);
}

[Benchmark]
public object ParseBigCompiled()
{
return _compiledParser.Parse(BlogPostTemplate);
}

[Benchmark]
public override string Render()
{
Expand Down
9 changes: 2 additions & 7 deletions Fluid/FluidParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Fluid.Values;
using Parlot;
using Parlot.Fluent;
using Parlot.Rewriting;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web;
Expand Down Expand Up @@ -52,8 +51,7 @@ public class FluidParser
protected readonly Parser<IReadOnlyList<FunctionCallArgument>> FunctionCallArgumentsList;
protected readonly Parser<Expression> LogicalExpression;
protected readonly Parser<Expression> CombinatoryExpression; // and | or
protected readonly Parser<Expression> Primary;
protected readonly Deferred<Expression> PrimaryInternal = Deferred<Expression>();
protected readonly Deferred<Expression> Primary = Deferred<Expression>();
protected readonly Deferred<Expression> FilterExpression = Deferred<Expression>();
protected readonly Deferred<IReadOnlyList<Statement>> KnownTagsList = Deferred<IReadOnlyList<Statement>>();
protected readonly Deferred<IReadOnlyList<Statement>> AnyTagsList = Deferred<IReadOnlyList<Statement>>();
Expand All @@ -78,9 +76,6 @@ public FluidParser(FluidParserOptions parserOptions)
String.Name = "String";
Number.Name = "Number";

// Build lookup for String/Member/Number/Range
Primary = PrimaryInternal.Lookup((ISeekable)String, (ISeekable)new IdentifierParser(), (ISeekable)Number, (ISeekable)LParen);

var Integer = Terms.Integer().Then<Expression>(x => new LiteralExpression(NumberValue.Create(x)));
Integer.Name = "Integer";

Expand Down Expand Up @@ -128,7 +123,7 @@ public FluidParser(FluidParserOptions parserOptions)
Range.Name = "Range";

// primary => NUMBER | STRING | property
PrimaryInternal.Parser =
Primary.Parser =
String.Then<Expression>(x => new LiteralExpression(StringValue.Create(x)))
.Or(Member.Then<Expression>(static x =>
{
Expand Down

0 comments on commit d262d07

Please sign in to comment.