Skip to content

[Pratt parser] Simplify helper methods and inlining - #1442

Merged
dmitriplotnikov merged 1 commit into
parser-recursion-depth-testfrom
pratt-parser-simplify
Aug 28, 2026
Merged

[Pratt parser] Simplify helper methods and inlining#1442
dmitriplotnikov merged 1 commit into
parser-recursion-depth-testfrom
pratt-parser-simplify

Conversation

@dmitriplotnikov

Copy link
Copy Markdown
Collaborator

Summary

Simplifies pratt_parser.go by inlining single-callsite helper functions (initTokenStream, parse, newLogicManager, receiverCallOrMacro, globalCallOrMacro), while maintaining distinct branches for - and ! and preserving recursive descent stack frame sizes.

Recursion Depth Test (Stack: 64 KB)

Category ANTLR (Max Depth OK) Pratt (Max Depth OK) Pratt Advantage
Parens ((((...42...)))) 20 unlimited unlimited
Unary (! ! ! ... true) unlimited unlimited 1.0x
Binary (1 + (1 + (...42...))) 21 93 ~4.4x
List ([[[...42...]]]) 18 85 ~4.7x

Benchmark Results (Sequential & Parallel)

Benchmark Category ns/op B/op allocs/op
Simple 27,429 ns/op 13,535 B/op 404 allocs/op
Complex 107,297 ns/op 51,864 B/op 1,397 allocs/op
Macros 50,519 ns/op 23,883 B/op 641 allocs/op
Errors 83,839 ns/op 37,764 B/op 1,176 allocs/op
Parallel/Simple 6,732 ns/op 13,494 B/op 404 allocs/op
Parallel/Complex 26,721 ns/op 51,865 B/op 1,397 allocs/op
Parallel/Macros 11,902 ns/op 23,732 B/op 641 allocs/op
Parallel/Errors 21,489 ns/op 37,659 B/op 1,176 allocs/op

Inline single-callsite helper functions (initTokenStream, parse,
newLogicManager, receiverCallOrMacro, globalCallOrMacro) to simplify
pratt_parser.go while preserving stack frame sizes and recursive descent
performance boundaries.

| Category | ANTLR (Max Depth OK) | Pratt (Max Depth OK) | Pratt Advantage |
| :--- | :--- | :--- | :--- |
| **Parens** (`(((...42...)))`) | 20 | unlimited | **unlimited** |
| **Unary** (`! ! ! ... true`) | unlimited | unlimited | **1.0x** |
| **Binary** (`1 + (1 + (...42...))`) | 21 | 93 | **~4.4x** |
| **List** (`[[[...42...]]]`) | 18 | 85 | **~4.7x** |

| Benchmark Category | ns/op | B/op | allocs/op |
| :--- | :--- | :--- | :--- |
| **Simple** | 27,429 ns/op | 13,535 B/op | 404 allocs/op |
| **Complex** | 107,297 ns/op | 51,864 B/op | 1,397 allocs/op |
| **Macros** | 50,519 ns/op | 23,883 B/op | 641 allocs/op |
| **Errors** | 83,839 ns/op | 37,764 B/op | 1,176 allocs/op |
| **Parallel/Simple** | 6,732 ns/op | 13,494 B/op | 404 allocs/op |
| **Parallel/Complex** | 26,721 ns/op | 51,865 B/op | 1,397 allocs/op |
| **Parallel/Macros** | 11,902 ns/op | 23,732 B/op | 641 allocs/op |
| **Parallel/Errors** | 21,489 ns/op | 37,659 B/op | 1,176 allocs/op |
@dmitriplotnikov
dmitriplotnikov merged commit be9d2e5 into parser-recursion-depth-test Aug 28, 2026
6 checks passed
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.

2 participants