-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache types.GenericAlias in getitems #101859
Comments
Oh yeah. I think @gvanrossum envisioned that at some point https://github.com/python/cpython/blob/main/Objects/genericaliasobject.c#L917 We should definitely implement some type of cache. Even if it doesn't speed things up much, it should save space. |
Agreed. |
@Fidget-Spinner I am working on this right now, but I have several technical questions. Usually, we place cache in a module state. But, since Another option is to have a
Any ideas? :) |
Builtin object caches tend to go into interpreter state. See for example the slice cache cpython/Include/internal/pycore_interp.h Line 137 in 03089fd
There's the dict, tuple and other cache also in there. You would need to setup and clear the state in the interpreter state initializer and destructor (sorry I forgot where those are). |
Thanks, I was not aware of that! 👍 |
Cc. @samuelcolvin, who just mentioned at the typing summit that the current caching for |
@AlexWaygood Are there any details re what kind of issues? |
The issue is that the order of union members can be cached after the first union is created. Causing problems for runtime type checking where order can matter. |
Yes, please see:
|
The "old-style" generic classes from the typing module (List, Dict, etc) cached typing._GenericAlias instances retuned from
__getitem__
:But when this functionality was ported to built-in types, caching was not implemented:
Since it is very common to subscript the same types with the same arguments, caching will be very beneficial here and will reduce memory footprint of heavily type-annotated modules.
Linked PRs
types.GenericAlias
objects #103541The text was updated successfully, but these errors were encountered: