Skip to content

Commit 54e436f

Browse files
The parser predicts in SLL first (#1528)
ParseHard allocated 3.8 MB to read a twenty-token expression, and every literal token added to the grammar added 12,136 B/op to it. The bytes were ANTLR's full-context LL prediction, which the default prediction mode falls back to wherever its context-free SLL prediction sees a conflict. The parser now predicts in SLL, with the bail error strategy, and parses again in LL only where that reports an error or an action throws: SLL either returns the tree LL would or reports a syntax error, so the tree is LL's either way. ParseHard allocates 59,871 B/op, 98.4% less, and the baseline moves to it. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 694abc3 commit 54e436f

3 files changed

Lines changed: 99 additions & 46 deletions

File tree

‎Sources/AngouriMath/Core/Parser.cs‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Any modifications to other source files will be overwritten when the parser is r
1414
*/
1515

1616
using Antlr4.Runtime;
17+
using Antlr4.Runtime.Atn;
1718
using System;
1819
using System.IO;
1920
using System.Text;
@@ -159,6 +160,32 @@ internal static Either<Entity, Failure<ReasonWhyParsingFailed>> ParseSilent(stri
159160

160161
var parser = new AngouriMathParser(tokenStream, null, writer);
161162

163+
// First in SLL prediction, which never looks past the current rule for context:
164+
// it returns the tree LL would, or reports an error where the input needs LL's
165+
// context or is invalid, and it bails there rather than recovering, so either is
166+
// retried in LL below. LL's full-context prediction allocated 3.8 MB for one
167+
// twenty-token expression, and grew with every alternative `atom` has, so every
168+
// function added to the grammar cost every parse.
169+
parser.Interpreter.PredictionMode = PredictionMode.SLL;
170+
parser.ErrorHandler = new BailErrorStrategy();
171+
parser.RemoveErrorListeners();
172+
var read = false;
173+
try
174+
{
175+
parser.Parse();
176+
read = true;
177+
}
178+
// A syntax error, or an action that threw on an alternative SLL took and LL would
179+
// not have: neither is an answer, and LL gives the one there is.
180+
catch (Exception)
181+
{
182+
tokenStream.Seek(0);
183+
parser.Reset();
184+
parser.Interpreter.PredictionMode = PredictionMode.LL;
185+
parser.ErrorHandler = new DefaultErrorStrategy();
186+
parser.AddErrorListener(ConsoleErrorListener<IToken>.Instance);
187+
}
188+
162189
// ANTLR reports a syntax error to the listener and then *recovers*, carrying on
163190
// with a rule context whose value was never assigned. The grammar's actions run
164191
// anyway and dereference it, so an invalid input can bring the parse down before
@@ -171,14 +198,15 @@ internal static Either<Entity, Failure<ReasonWhyParsingFailed>> ParseSilent(stri
171198
// where the parser throws with nothing recorded against the input, the exception
172199
// is ours and it keeps propagating.
173200
// https://github.com/asc-community/AngouriMath/issues/813
174-
try
175-
{
176-
parser.Parse();
177-
}
178-
catch (Exception) when (writer.errors.Count > 0)
179-
{
180-
return new Failure<ReasonWhyParsingFailed>(writer.errors[0]);
181-
}
201+
if (!read)
202+
try
203+
{
204+
parser.Parse();
205+
}
206+
catch (Exception) when (writer.errors.Count > 0)
207+
{
208+
return new Failure<ReasonWhyParsingFailed>(writer.errors[0]);
209+
}
182210

183211
if (writer.errors.Count > 0)
184212
return new Failure<ReasonWhyParsingFailed>(writer.errors[0]);

‎Sources/AngouriMath/Docs/WhatsNew/version_performance_control.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,31 @@ cent — its measured run-to-run spread on mean time is up to 51.8%. A move of 8
256256
outside that band by a wide margin and agrees in sign and rough size with the allocation column
257257
beside it. The small rows from the same run are still not worth reading, and are not quoted.
258258

259+
## The 2203rd: the parser predicts in SLL first
260+
261+
`ParseHard` allocated 3.8 MB to read a twenty-token expression, and every function added to the
262+
grammar made it more: `Ei` and `li` cost it 24,272 B/op, and `Si`, `Ci`, `Shi` and `Chi` would
263+
have cost another 48,544 -- 12,136 B for each literal token, exactly, which put the branch adding
264+
the four trigonometric integrals over the gate's 3%. The bytes were ANTLR's full-context LL
265+
prediction, which the default prediction mode falls back to wherever its context-free SLL
266+
prediction sees a conflict, on every parse that reaches one. The parser now predicts in SLL, with
267+
the bail error strategy, and parses again in LL only where that reports an error or an action
268+
throws. ANTLR documents that SLL "will either return a correct parse tree (i.e. the same parse
269+
tree that would be returned with the LL prediction mode), or it will report a syntax error", so
270+
the tree is LL's either way, and only an input SLL cannot read pays for both. Of the 117,358
271+
distinct expressions in Rubi's suite that the library can express, the only ones SLL could not read
272+
were the 92 LL cannot either: it read every one LL read, to an equal entity.
273+
274+
| benchmark | 2202nd | 2203rd | allocation | time |
275+
|---|--:|--:|--:|--:|
276+
| `ParseHard` | 3,799,418 | 59,871 | **−98.4%** | 1,525 → 25.6 µs |
277+
| `ParseEasy` | 18,301 | 18,373 | +0.4% | 6.27 → 5.58 µs |
278+
| every other gated entry | | | within 64 bytes | within the noise |
279+
280+
Bytes allocated per call, same machine, both columns by the gate in one session. The 72 bytes on
281+
`ParseEasy` are what the two stages cost a parse that never falls back. The baseline moves to this
282+
run.
283+
259284
## The 2077th: the root of a negative real is exactly imaginary, and cheaper for it
260285

261286
A correctness fix ([#1378](https://github.com/asc-community/AngouriMath/issues/1378)) that

‎Sources/Tests/DotnetBenchmark/performance-baseline.json‎

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
11
{
22
"comment": "Allocated bytes and mean nanoseconds per operation for the popular use cases of https://github.com/asc-community/AngouriMath/issues/746. Checked by PerformanceGate.cs; see Sources/AngouriMath/Docs/WhatsNew/version_performance_control.md for when updating it is legitimate.",
33
"benchmark": "CommonFunctionsInterVersion",
4-
"commit": "84f83fd7",
5-
"measuredOn": "2026-09-21",
4+
"commit": "7c0dce12e6e60cb8ce33a7aae661805a9e7cab99",
5+
"measuredOn": "2026-09-28",
66
"runtime": ".NET 10.0.10",
77
"machine": "Ubuntu 26.04 LTS, X64, 8 logical cores",
88
"cases": {
99
"CompileEasy": {
1010
"allocatedBytes": 11031,
11-
"meanNanoseconds": 186349.36119666466
11+
"meanNanoseconds": 187148.49052734376
1212
},
1313
"CompileHard": {
1414
"allocatedBytes": 20434,
15-
"meanNanoseconds": 310566.03922526044
15+
"meanNanoseconds": 312400.38727678574
1616
},
1717
"Derivate": {
18-
"allocatedBytes": 55095,
19-
"meanNanoseconds": 13865.370408121746
18+
"allocatedBytes": 55783,
19+
"meanNanoseconds": 14270.340443929037
2020
},
2121
"EvalEasy": {
2222
"allocatedBytes": 0,
23-
"meanNanoseconds": 1.8814736750836556
23+
"meanNanoseconds": 2.082944576193889
2424
},
2525
"EvalPolynomialFresh": {
26-
"allocatedBytes": 12488,
27-
"meanNanoseconds": 4092.535597229004
26+
"allocatedBytes": 12536,
27+
"meanNanoseconds": 4116.869101206462
2828
},
2929
"EvalPolynomialFresh15Digits": {
30-
"allocatedBytes": 11816,
31-
"meanNanoseconds": 4219.261494954427
30+
"allocatedBytes": 12064,
31+
"meanNanoseconds": 4281.885651179722
3232
},
3333
"EvalTranscendentalFresh": {
34-
"allocatedBytes": 34720,
35-
"meanNanoseconds": 16126.92978140024
34+
"allocatedBytes": 34760,
35+
"meanNanoseconds": 16291.656792776925
3636
},
3737
"EvalTrig": {
38-
"allocatedBytes": 69904,
39-
"meanNanoseconds": 32277.99326578776
38+
"allocatedBytes": 69944,
39+
"meanNanoseconds": 32223.94764811198
4040
},
4141
"EvalTrigPrecise": {
42-
"allocatedBytes": 336472,
43-
"meanNanoseconds": 404842.2501502404
42+
"allocatedBytes": 336552,
43+
"meanNanoseconds": 410607.1845703125
4444
},
4545
"ParseEasy": {
46-
"allocatedBytes": 18461,
47-
"meanNanoseconds": 6216.401372909546
46+
"allocatedBytes": 18373,
47+
"meanNanoseconds": 5579.264629109701
4848
},
4949
"ParseHard": {
50-
"allocatedBytes": 3727130,
51-
"meanNanoseconds": 1492410.295200893
50+
"allocatedBytes": 59871,
51+
"meanNanoseconds": 25569.49151611328
5252
},
5353
"RunEasy": {
5454
"allocatedBytes": 0,
55-
"meanNanoseconds": 20.080275533291008
55+
"meanNanoseconds": 20.103014286075318
5656
},
5757
"RunHard": {
5858
"allocatedBytes": 0,
59-
"meanNanoseconds": 293.90326486315047
59+
"meanNanoseconds": 293.32832632746016
6060
},
6161
"RunMedium": {
6262
"allocatedBytes": 0,
63-
"meanNanoseconds": 167.367431810924
63+
"meanNanoseconds": 164.38394159930093
6464
},
6565
"SimplifyEasy": {
66-
"allocatedBytes": 79026,
67-
"meanNanoseconds": 44460.77027587891
66+
"allocatedBytes": 79010,
67+
"meanNanoseconds": 43888.25782877604
6868
},
6969
"SimplifyHard": {
70-
"allocatedBytes": 173260088,
71-
"meanNanoseconds": 102448230.35714285
70+
"allocatedBytes": 175442312,
71+
"meanNanoseconds": 110382788.55
7272
},
7373
"SolveEasy": {
74-
"allocatedBytes": 453181,
75-
"meanNanoseconds": 204341.27948869977
74+
"allocatedBytes": 453621,
75+
"meanNanoseconds": 205587.90686035156
7676
},
7777
"SolveEasyMedium": {
78-
"allocatedBytes": 65768,
79-
"meanNanoseconds": 18431.602217610678
78+
"allocatedBytes": 66576,
79+
"meanNanoseconds": 18447.484899902345
8080
},
8181
"SolveHard": {
82-
"allocatedBytes": 10155896,
83-
"meanNanoseconds": 68013707.275
82+
"allocatedBytes": 10148544,
83+
"meanNanoseconds": 78453427.95959596
8484
},
8585
"SolveMedium": {
86-
"allocatedBytes": 451845,
87-
"meanNanoseconds": 257009.43385416668
86+
"allocatedBytes": 457918,
87+
"meanNanoseconds": 272234.59720284597
8888
},
8989
"SolveMediumHard": {
90-
"allocatedBytes": 1393748,
91-
"meanNanoseconds": 8489135.7375
90+
"allocatedBytes": 1407676,
91+
"meanNanoseconds": 8650714.277083334
9292
}
9393
},
9494
"ungated": {

0 commit comments

Comments
 (0)