classification
Title: Create empty __annotations__ dictionaries lazily
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, serhiy.storchaka
Priority: low Keywords:

Created on 2019-08-26 06:39 by rhettinger, last changed 2019-08-27 05:12 by rhettinger. This issue is now closed.

Messages (2)
msg350490 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-26 06:39
Right now, every heap function is given a new annotations dictionary even if annotations aren't used.  How about we create these lazily in order to speed-up function creation time, improve start-up time, and save space.


>>> def f(): pass

>>> def g(): return 1

>>> f.__annotations__
{}
>>> g.__annotations__
{}
>>> hex(id(f.__annotations__))
'0x109207e40'
>>> hex(id(g.__annotations__))
'0x1092296c0'
msg350509 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-26 08:05
Guido's Time Machine strikes back.

>>> import gc
>>> def f(): pass
... 
>>> gc.get_referents(f)
[<code object f at 0x7f45f5d366c0, file "<stdin>", line 1>, {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'gc': <module 'gc' (built-in)>, 'f': <function f at 0x7f45f5dd3b90>}, '__main__', None, 'f', 'f']
>>> f.__annotations__
{}
>>> gc.get_referents(f)
[<code object f at 0x7f45f5d366c0, file "<stdin>", line 1>, {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'gc': <module 'gc' (built-in)>, 'f': <function f at 0x7f45f5dd3b90>}, '__main__', None, 'f', {}, 'f']
History
Date User Action Args
2019-08-27 05:12:13rhettingersetstatus: open -> closed
resolution: out of date
stage: resolved
2019-08-26 08:05:15serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg350509
2019-08-26 06:39:48rhettingercreate