]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-153568: Skip parser memo lookups that cannot match (#153571)
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Sat, 18 Jul 2026 16:12:38 +0000 (18:12 +0200)
committerGitHub <noreply@github.com>
Sat, 18 Jul 2026 16:12:38 +0000 (18:12 +0200)
Each token now carries a small filter of the rule types present in its
memo list, so the common case of a miss no longer walks the list.

Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-14-57-19.gh-issue-153568.memoflt.rst [new file with mode: 0644]
Parser/pegen.c
Parser/pegen.h

diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-14-57-19.gh-issue-153568.memoflt.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-11-14-57-19.gh-issue-153568.memoflt.rst
new file mode 100644 (file)
index 0000000..9e78e5a
--- /dev/null
@@ -0,0 +1,2 @@
+Speed up the parser by letting memoization lookups that cannot match return
+immediately.
index 447124cbe00b66a313b39193a114296aa858ec4b..fcec810037e98d44bcf1c9b7f190d878d926215d 100644 (file)
@@ -100,6 +100,7 @@ _PyPegen_insert_memo(Parser *p, int mark, int type, void *node)
     m->mark = p->mark;
     m->next = p->tokens[mark]->memo;
     p->tokens[mark]->memo = m;
+    p->tokens[mark]->memo_mask |= 1ULL << (type & 63);
     return 0;
 }
 
@@ -360,6 +361,10 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
 
     Token *t = p->tokens[p->mark];
 
+    if (!(t->memo_mask & (1ULL << (type & 63)))) {
+        return 0;
+    }
+
     for (Memo *m = t->memo; m != NULL; m = m->next) {
         if (m->type == type) {
 #if defined(Py_DEBUG)
index 884b3ff62d7d99b5d6d41e549c943d68e03dc7cd..3cca698692bf6bf2c1bdd8fef78bc84c1485f0a9 100644 (file)
@@ -42,6 +42,10 @@ typedef struct {
     int level;
     int lineno, col_offset, end_lineno, end_col_offset;
     Memo *memo;
+    // Filter over the rule types present in `memo` (bit `type & 63` is set
+    // for every entry): lets lookups skip walking the list on definite
+    // misses, which are the common case.
+    uint64_t memo_mask;
     PyObject *metadata;
 } Token;