Iteratively parse nested parentheses in PrattParserWorker - #2278
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
Replace the lookahead-based grouping parentheses counter (`CountGroupingParentheses`) with a bottom-up iterative approach in `PrattParserWorker::ParsePrimary()` to eliminate quadratic $O(N^2)$ lookahead scans while keeping $O(1)$ call-stack frames on deeply nested parenthesized expressions. ### Benchmarks (`pratt_parser_benchmark`, `-c opt --dynamic_mode=off`) | Benchmark Case | Before (Mean) | After (Mean) | Change / Speedup | | :--- | ---: | ---: | ---: | | `BM_Pratt_ParseNestedParentheses/10` | 724 ns | 535 ns | -26.1% (1.35x faster) | | `BM_Pratt_ParseNestedParentheses/50` | 2,484 ns | 1,625 ns | -34.6% (1.53x faster) | | `BM_Pratt_ParseNestedParentheses/200` | 8,859 ns | 5,427 ns | -38.7% (1.63x faster) | | `BM_Pratt_ParseNestedLeftParenthesesCalc/10` | 7,876 ns | 3,397 ns | -56.9% (2.32x faster) | | `BM_Pratt_ParseNestedLeftParenthesesCalc/50` | 112,539 ns | 14,518 ns | -87.1% (7.75x faster) | | `BM_Pratt_ParseNestedLeftParenthesesCalc/200` | 1,583,442 ns | 57,375 ns | -96.4% (27.6x faster) | PiperOrigin-RevId: 984557444
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Iteratively parse nested parentheses in PrattParserWorker
Replace the lookahead-based grouping parentheses counter ($O(N^2)$ lookahead scans while keeping $O(1)$ call-stack frames on deeply nested parenthesized expressions.
CountGroupingParentheses) with a bottom-up iterative approach inPrattParserWorker::ParsePrimary()to eliminate quadraticBenchmarks (
pratt_parser_benchmark,-c opt --dynamic_mode=off)BM_Pratt_ParseNestedParentheses/10BM_Pratt_ParseNestedParentheses/50BM_Pratt_ParseNestedParentheses/200BM_Pratt_ParseNestedLeftParenthesesCalc/10BM_Pratt_ParseNestedLeftParenthesesCalc/50BM_Pratt_ParseNestedLeftParenthesesCalc/200