PEP 649: Avoid creation of function objects for __annotate__
#124157
Labels
3.14
new features, bugs and security fixes
interpreter-core
(Objects, Python, Grammar, and Parser dirs)
topic-typing
Currently, when a class, function, or module has any annotations, we always generate an
__annotate__
function object at import time. A function object takes 168 bytes. But in most cases, all of the relevant fields on an__annotate__
function are predictable (there's no docstring and no defaults or kwdefaults, the name is__annotate__
, etc.). So we could save significant memory by constructing only a smaller object and constructing the function on demand when somebody asks for it (by accessing__annotate__
).We need the following to create an
__annotate__
function object:__annotate__
descriptor can't easily get to the globals dict. To do this, we may need a new bytecode that just loads the current globals.I am thinking of a format where
__annotate__
can be any of the following:__annotate__
getters would have to recognize the second and third cases and translate them into function objects on the fly. As a result, users accessing.__annotate__
would never see the tuple, though those who peek directly into a module or class's__dict__
might.Other related opportunities for optimization:
functools.wraps
would unnecessarily force materialization of the__annotate__
function. Not sure there's an elegant solution for this.The text was updated successfully, but these errors were encountered: