Skip to content

Commit 09a5678

Browse files
committed
gh-153568: Pause the GC while converting the AST to Python objects
Freshly converted AST nodes cannot be part of a reference cycle yet, so collections during the conversion only pay to traverse the half-built tree without ever freeing any of it.
1 parent e2118b0 commit 09a5678

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up :func:`ast.parse` by pausing the garbage collector while the AST is
2+
converted to Python objects.

Parser/asdl_c.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,15 @@ class PartingShots(StaticVisitor):
21172117
if (state == NULL) {
21182118
return NULL;
21192119
}
2120+
// Freshly converted AST nodes cannot be part of a reference cycle yet,
2121+
// so a garbage collection while building them cannot free anything of
2122+
// this tree and only pays to traverse its half-built nodes. Pause the
2123+
// collector while the conversion runs.
2124+
int gc_was_enabled = PyGC_Disable();
21202125
PyObject *result = ast2obj_mod(state, t);
2126+
if (gc_was_enabled) {
2127+
PyGC_Enable();
2128+
}
21212129
21222130
return result;
21232131
}

Python/Python-ast.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.

0 commit comments

Comments
 (0)