Skip to content

Update the expression parser to Parlot 2.3.0 - #408

Open
sebastienros wants to merge 3 commits into
spring-projects:mainfrom
sebastienros:perf/parlot-2.3.0
Open

sebastienros wants to merge 3 commits into
spring-projects:mainfrom
sebastienros:perf/parlot-2.3.0

Conversation

@sebastienros

Copy link
Copy Markdown
Contributor

A follow-up to #375. Parlot 2.3.0 has new combinators and Operators() features: this updates the source generator and
uses the one that helps this grammar. The language doesn't change, nor do the trees or the syntax errors.

Changes

  1. Update Parlot.SourceGenerator to 2.3.0. Since 2.3.0, a When that rejects a value reports the failure at the start
    of the value (Report the start of what a generated parser rejected as the failure sebastienros/parlot#497). ParsePrimary then reported its errors at the start of the expression instead
    of at the token that can't follow it, where the ANTLR parser reported them: a|b at column 1 instead of 2. Three
    entries of the golden file caught it, and 15.5% of the errors of ParsePrimary in the differential test below moved.
    When the follow check fails, the primary expression is now read again and fails after its end, which reports that token
    again. This runs on the error path only.
  2. Read string literals with String() and pool them. Terms.String(StringLiteralQuotes.Single, StringEscapes.Sql)
    reads the strings of the language: a doubled quote is a quote and a backslash is an ordinary char
    (Support the escape sequences of other languages in String() sebastienros/parlot#535). It returns the text between the quotes, so ToPooledString() gets the value without copying
    the token and then its content: two strings less per literal, and none when the literal was already read, as when
    ExpressionEvaluator.GetValue parses the same expression again. The pool is bounded, takes no lock to find a value,
    and turns itself off when values rarely repeat.
  3. Give the reason =, ?? and ?: are not levels of the operator table. Comment and README only: they apply at
    most once to a logical expression. Both said that the last operand of ?: being a whole expression kept it out of
    the table, which 2.3.0 can now parse (see below).

Same trees and errors

  • The differential test of Replace ANTLR with a generated Parlot parser in the expression language #375, as a console comparing two builds of Spring.Core: the corpus of the port, generated
    expressions and mutations of both, 104,204 inputs through Parse, ParsePrimary, ParseProperty and ParseLambda.
    This branch returns the same trees and the same errors, message and column, as 2.2.1 for all 416,816 parses, on .NET 10
    (arm64) and on .NET Framework 4.8 (x64), where the generated parser takes its downlevel paths.
  • ExpressionGrammarTests, with the golden trees and errors of the ANTLR parser, and the other expression tests pass.
  • Spring.Core.Tests: net472 on Windows, 2,813 passed and none failed. net10.0 on macOS: all pass except 5 tests of
    Spring.Globalization.Formatters, which depend on the culture data of the OS and pass on Windows.
  • The deepest nesting that Expression.Parse reads on a small stack is the same with 2.2.1 and this branch: 47
    parentheses or calls on a 256 KB thread, 271 parentheses and 255 calls on 1 MB (.NET 10, arm64, Release), so the
    nesting limits of the README don't move.

Benchmarks

ExpressionBenchmark and ObjectWrapperBenchmark from #375. BenchmarkDotNet in process, 3 warmup and 10 iterations of
150 ms, .NET 10, builds alternated.

Parsing, Apple M4 Pro (arm64), mean of 2 to 4 runs per build:

2.2.1 2.3.0 + ParsePrimary fix (1) + pooled strings (2)
Parse Small 220 ns, 536 B 221 ns, 536 B 218 ns, 536 B 228 ns, 536 B
Parse Medium 1,214 ns, 2,040 B 1,151 ns, 2,040 B 1,119 ns, 2,040 B 1,162 ns, 1,840 B
Parse Large 7,592 ns, 12,024 B 7,211 ns, 12,024 B 7,279 ns, 12,024 B 7,380 ns, 11,440 B
  • 2.3.0 parses the medium and large expressions about 5% faster than 2.2.1, in each of the three rounds. The differences
    between the three 2.3.0 columns are within the spread of the runs of a build (±4%): the small expression has no string
    literal, and its 228 ns in (2) comes from one run at 247 ns, the others took 215 and 221 ns.
  • Pooling the strings saves 200 B per parse of the medium expression (3 literals) and 584 B on the large one (9 literals).
  • ObjectWrapper.PropertyPath, which goes through ParsePrimary, took 527 to 628 ns and allocated 1.42 KB with every
    build. Alternated four times, it took 541 ns with 2.3.0 alone and 540 ns with the fix of (1), which only adds a check of
    the next char on a successful parse.

From parsing to evaluation, AMD EPYC 7763 (x64, Windows), 2.2.1 against this branch, mean of 2 runs each:

2.2.1 this branch
Parse Small 514 ns, 536 B 497 ns, 536 B
Parse Medium 2,522 ns, 2,040 B 2,480 ns, 1,840 B
GetValueUncached Small 1,002 ns, 944 B 951 ns, 944 B
GetValueUncached Medium 12,832 ns, 8,800 B 13,011 ns, 8,600 B
ParseThenGetValue100 Small 10,230 ns, 12,824 B 11,003 ns, 12,824 B
ParseThenGetValue100 Medium 196,162 ns, 289,177 B 200,009 ns, 288,977 B
GetValuePrepared Small (evaluation only) 95 ns, 120 B 102 ns, 120 B
GetValuePrepared Medium (evaluation only) 1,799 ns, 2,832 B 1,917 ns, 2,832 B
  • Above parsing, the times move by -5% to +8%, as much as GetValuePrepared, whose code this branch doesn't change: it
    evaluates a tree parsed once. These differences come from where the code of the evaluation lands, not from the
    parser. On the Mac too, evaluating alone took the same time with and without these changes (51 ns and 50 ns).
  • The large expression is left out: it is the first case of each process and was still being compiled by the tiered JIT
    (2.2.1 took 56 and 70 µs to parse it, this branch 17 and 16 µs, while the medium one takes 2.5 µs).

What 2.3.0 has that this grammar doesn't use

Since 2.3.0, a When that rejects a value reports the failure at the start of the value.
ParsePrimary's syntax errors then pointed at the start of the expression instead of the token
that can't follow it, where the former parser reported them: "a|b" at column 1 instead of 2.
When the condition fails, the primary expression is now read again and fails after its end,
which reports that token again, on the error path only.

Signed-off-by: Sébastien Ros <1165805+sebastienros@users.noreply.github.com>
The escape sequences of the language, a doubled quote and no backslash escape, are those of
StringEscapes.Sql. String() returns the text between the quotes, decoded only when it has a
doubled quote, so ToPooledString() gets the value without first copying the token and then its
content: two strings less per literal, and none when the literal was already read, as when an
uncached expression is parsed again.

The lexer's ScanString still finds the strings of type names and of syntax errors.

Signed-off-by: Sébastien Ros <1165805+sebastienros@users.noreply.github.com>
They apply at most once to a logical expression. The comment and the README said the last
operand of ?: being a whole expression kept it out of the table, which an operator table can
now parse.

Signed-off-by: Sébastien Ros <1165805+sebastienros@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant