Skip to content

Commit 8a40a92

Browse files
shugoclaude
authored andcommitted
Avoid a quadratic walk when parsing long call chains
parse_expression_terminator runs after every infix operator and called pm_block_call_p, which walks the whole receiver chain, even when the binding power alone already decided the result. Check the binding power first; pm_block_call_p is pure, so the condition is unchanged. Parsing "a" followed by 8000 ".b" links drops from 44.8ms to 0.46ms. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent a9b755a commit 8a40a92

1 file changed

Lines changed: 2 additions & 9 deletions

File tree

src/prism.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22403,11 +22403,7 @@ parse_expression_terminator(pm_parser_t *parser, pm_node_t *node) {
2240322403
// A block call (command with do-block, or any call chained
2240422404
// from one) can only be followed by call chaining (., ::,
2240522405
// &.), composition (and/or), and modifier operators.
22406-
if (pm_block_call_p(node)) {
22407-
return left > PM_BINDING_POWER_COMPOSITION && left < PM_BINDING_POWER_CALL;
22408-
}
22409-
22410-
return false;
22406+
return left > PM_BINDING_POWER_COMPOSITION && left < PM_BINDING_POWER_CALL && pm_block_call_p(node);
2241122407
}
2241222408
case PM_SUPER_NODE:
2241322409
case PM_YIELD_NODE:
@@ -22419,10 +22415,7 @@ parse_expression_terminator(pm_parser_t *parser, pm_node_t *node) {
2241922415

2242022416
/* A super carrying a do-block is a block call, so it may also be
2242122417
* followed by call chaining (`.`, `::`, `&.`). */
22422-
if (pm_block_call_p(node)) {
22423-
return left > PM_BINDING_POWER_COMPOSITION && left < PM_BINDING_POWER_CALL;
22424-
}
22425-
return false;
22418+
return left > PM_BINDING_POWER_COMPOSITION && left < PM_BINDING_POWER_CALL && pm_block_call_p(node);
2242622419
case PM_DEF_NODE:
2242722420
// An endless method whose body is a command-style call (e.g.,
2242822421
// `def f = foo bar`) is a command assignment and can only be

0 commit comments

Comments
 (0)