Skip to content

gh-153568: Use a managed dict for AST nodes - #160616

Merged
lysnikolaou merged 2 commits into
python:mainfrom
lysnikolaou:ast-node-inline-values
Oct 25, 2026
Merged

lysnikolaou merged 2 commits into
python:mainfrom
lysnikolaou:ast-node-inline-values

Conversation

@lysnikolaou

@lysnikolaou lysnikolaou commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

AST nodes had an explicit __dict__ slot, so every node built by ast.parse allocated its own dict. With Py_TPFLAGS_MANAGED_DICT, node attributes are stored as inline values with shared keys instead.

ast.parse is ~15.8% faster, walking trees is ~15.9% faster, and the memory used by the AST trees is reduced by ~51.6%.

AST nodes had an explicit `__dict__`` slot, so every node built by
`ast.parse` allocated its own dict. With Py_TPFLAGS_MANAGED_DICT,
node attributes are stored as inline values with shared keys instead.

`ast.parse` is ~15.8% faster, walking trees is ~15.9% faster, and
the memory used by the AST trees is reduced by ~51.6%.
@bedevere-app bedevere-app Bot added awaiting core review type-feature A feature request or enhancement labels Oct 24, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Oct 24, 2026

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fucking amazing!

@lysnikolaou
lysnikolaou merged commit aa0e410 into python:main Oct 25, 2026
59 of 60 checks passed
@lysnikolaou
lysnikolaou deleted the ast-node-inline-values branch October 25, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants