Skip to content

Commit e6bd93f

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 e5fbabb commit e6bd93f

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
@@ -2116,7 +2116,15 @@ class PartingShots(StaticVisitor):
21162116
if (state == NULL) {
21172117
return NULL;
21182118
}
2119+
// Freshly converted AST nodes cannot be part of a reference cycle yet,
2120+
// so a garbage collection while building them cannot free anything of
2121+
// this tree and only pays to traverse its half-built nodes. Pause the
2122+
// collector while the conversion runs.
2123+
int gc_was_enabled = PyGC_Disable();
21192124
PyObject *result = ast2obj_mod(state, t);
2125+
if (gc_was_enabled) {
2126+
PyGC_Enable();
2127+
}
21202128
21212129
return result;
21222130
}

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)