Skip to content

Commit 86b00a5

Browse files
committed
gh-153568: Skip the expression precedence chain for single-token atoms
A bare name or number followed by a token that cannot extend an expression is now built directly, avoiding a dozen rule invocations per atom. Rules declare the hook with the new (fastpath=function) flag next to (memo); the generator only emits the hook call and knows nothing about tokens or expressions.
1 parent 7715c92 commit 86b00a5

11 files changed

Lines changed: 133 additions & 3 deletions

File tree

‎Grammar/python.gram‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ named_expression[expr_ty]:
747747
| invalid_named_expression
748748
| expression !':='
749749

750-
disjunction[expr_ty] (memo):
750+
disjunction[expr_ty] (memo, fastpath=_PyPegen_atom_fast_path):
751751
| a=conjunction b=('or' c=conjunction { c })+ { _PyAST_BoolOp(
752752
Or,
753753
CHECK(asdl_expr_seq*, _PyPegen_seq_insert_in_front(p, a, b)),
@@ -761,7 +761,7 @@ conjunction[expr_ty] (memo):
761761
EXTRA) }
762762
| inversion
763763

764-
inversion[expr_ty] (memo):
764+
inversion[expr_ty] (memo, fastpath=_PyPegen_atom_fast_path):
765765
| 'not' a=inversion { _PyAST_UnaryOp(Not, a, EXTRA) }
766766
| comparison
767767

‎InternalDocs/parser.md‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,51 @@ actions. As with memoization, actions must not invalidate cached results. In
616616
particular, suffix actions must not move the parser before their starting mark,
617617
rewrite buffered input, or replace memo entries for earlier positions.
618618

619+
Fast-path hooks
620+
---------------
621+
622+
Some rules are entered so often that even the bookkeeping of trying their
623+
alternatives is expensive. Consider parsing the argument ``a`` in a call
624+
like ``f(a, b)``: the argument is a full expression, so the parser descends
625+
the whole operator precedence chain
626+
627+
```
628+
expression -> disjunction -> conjunction -> inversion -> comparison
629+
-> bitwise_or -> bitwise_xor -> bitwise_and -> shift_expr -> sum
630+
-> term -> factor -> power -> await_primary -> primary -> atom
631+
```
632+
633+
before it can produce the ``Name`` node for ``a``: around fourteen rule
634+
invocations, each paying its own C-stack check and memoization lookups, to
635+
consume a single token. But since the following token is ``,``, which
636+
cannot continue any binary or postfix expression, that outcome is already
637+
known after peeking at two tokens.
638+
639+
For cases like this a rule can declare a hand-written C hook with the
640+
``fastpath`` flag, next to where ``memo`` goes:
641+
642+
```
643+
disjunction[expr_ty] (memo, fastpath=_PyPegen_atom_fast_path):
644+
```
645+
646+
The generator calls the hook on rule entry, before any alternative is tried:
647+
648+
```c
649+
if (_PyPegen_atom_fast_path(p, &_res)) {
650+
p->level--;
651+
return _res;
652+
}
653+
```
654+
655+
The hook receives the parser and a pointer to the rule's result variable and
656+
returns 1 if it handled the parse (storing its result, which can be ``NULL``
657+
to signal failure with ``p->error_indicator`` set) or 0 to fall through to
658+
the rule's normal alternatives. The generator knows nothing about what the
659+
hook does: all parsing logic lives in the hook itself, next to the other
660+
token helpers in [`Parser/pegen.c`](../Parser/pegen.c). A hook must behave
661+
exactly like the rule it accelerates, including which tokens it fills,
662+
because error reporting depends on the number of tokens read (``p->fill``).
663+
619664
Automatic variables
620665
-------------------
621666

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up parsing of simple expressions by recognizing single-token atoms
2+
without descending the full precedence rule chain.

‎Parser/parser.c‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Parser/pegen.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,52 @@ _resize_tokens_array(Parser *p) {
327327
return 0;
328328
}
329329

330+
// Fast path attached to the expression precedence chain's entry rules via
331+
// the (fastpath=function) rule flag in the grammar. A bare NAME/NUMBER
332+
// atom followed by a token that cannot start or continue a binary/postfix
333+
// expression is a complete expression, so it is built directly instead of
334+
// descending the whole chain. Returns 1 if it produced a result (stored
335+
// in *result), 0 to fall through to the rule's alternatives. The second
336+
// token is only examined when the first is NAME/NUMBER: the chain fills
337+
// it too in that case, so error reporting (which keys off p->fill)
338+
// observes an identical token fill state.
339+
int
340+
_PyPegen_atom_fast_path(Parser *p, void *result)
341+
{
342+
if (p->mark == p->fill && _PyPegen_fill_token(p) < 0) {
343+
p->error_indicator = 1;
344+
*(void **)result = NULL;
345+
return 1;
346+
}
347+
Token *t = p->tokens[p->mark];
348+
if (t->type != NAME && t->type != NUMBER) {
349+
return 0;
350+
}
351+
if (p->mark + 1 == p->fill && _PyPegen_fill_token(p) < 0) {
352+
p->error_indicator = 1;
353+
*(void **)result = NULL;
354+
return 1;
355+
}
356+
switch (p->tokens[p->mark + 1]->type) {
357+
case COMMA:
358+
case RPAR:
359+
case RSQB:
360+
case RBRACE:
361+
case COLON:
362+
case NEWLINE:
363+
case SEMI:
364+
case EQUAL:
365+
case ENDMARKER:
366+
break;
367+
default:
368+
return 0;
369+
}
370+
expr_ty res = (t->type == NAME)
371+
? _PyPegen_name_token(p) : _PyPegen_number_token(p);
372+
*(void **)result = res;
373+
return 1;
374+
}
375+
330376
int
331377
_PyPegen_fill_token(Parser *p)
332378
{

‎Parser/pegen.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Token *_PyPegen_get_last_nonnwhitespace_token(Parser *);
195195
int _PyPegen_fill_token(Parser *p);
196196
// Return -1 on failure, leaving the buffer and capacity unchanged.
197197
int _PyPegen_grow_loop_buffer(void ***buffer, Py_ssize_t *capacity);
198+
int _PyPegen_atom_fast_path(Parser *p, void *result);
198199
expr_ty _PyPegen_name_token(Parser *p);
199200
expr_ty _PyPegen_number_token(Parser *p);
200201
void *_PyPegen_string_token(Parser *p);

‎Tools/peg_generator/pegen/c_generator_calls.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,17 @@ def candidate(alt: Alt) -> Rule | None:
409409
i = j
410410
self._prefixes[rule.name] = tuple(prefixes)
411411

412+
def _rule_fast_path(self, rule: Rule) -> str | None:
413+
for flag in rule.flags:
414+
name, _, func = flag.partition("=")
415+
if name == "fastpath":
416+
if not func:
417+
raise ValueError(
418+
f"rule {rule.name!r}: fastpath flag needs a function"
419+
)
420+
return func
421+
return None
422+
412423
def prepare_rule(
413424
self, rule: Rule, *, skip_actions: bool = False, shared: bool = False
414425
) -> CRule:
@@ -427,6 +438,7 @@ def prepare_rule(
427438
left_recursive=rule.left_recursive,
428439
leader=rule.leader,
429440
memoize="memo" in rule.flags and not rule.left_recursive,
441+
fastpath=self._rule_fast_path(rule),
430442
disable_invalid_rules=rule.name.endswith("without_invalid"),
431443
prefixes=self._prefixes.get(rule.name, ()),
432444
source=rule.source,

‎Tools/peg_generator/pegen/c_generator_model.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class CRule:
123123
left_recursive: bool
124124
leader: bool
125125
memoize: bool
126+
fastpath: str | None
126127
disable_invalid_rules: bool
127128
prefixes: tuple[CPrefix, ...] = ()
128129
source: pathlib.Path | None = None

‎Tools/peg_generator/pegen/c_generator_rules.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ def _emit_rule_body(self) -> None:
176176
self._emit_recursion_check()
177177
self._emit_error_check()
178178
self._print(f"{result_type} _res = NULL;")
179+
if fastpath := self._rule.fastpath:
180+
self._print(f"if ({fastpath}(p, &_res)) {{")
181+
with self._indent():
182+
self._returns.emit("_res")
183+
self._print("}")
179184
if memoize:
180185
self._print(f"if (_PyPegen_is_memoized(p, {signature.name}_type, &_res)) {{")
181186
with self._indent():

‎Tools/peg_generator/pegen/grammar_parser.py‎

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)