You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Specialising LOAD_ATTR for mutable class attributes #93657
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
def f():
for _ in range(10):
Color.RED
f()
This use pattern is very common in enums and also the recommended way to check constants in pattern matching. Specialising for this all mutable class attributes should speed up those use cases.
changed the title [-]Optimizing LOAD_ATTR for Enums/mutable class attributes[/-][+]Specialising LOAD_ATTR for Enums/mutable class attributes[/+]on Jun 9, 2022
I don't see any reason to preference one namespace over another. I expect that instance namespaces will be dominant in practice, followed by some mix of class namespaces, module namespaces, SimpleNamespaces, and descriptors. In practice, non-enum attribute lookups dominate the Python world.
Also the mention on Enums in docs was a bit gratuitous. The primary advantages of an Enum over a regular class namespace is in how the object print and that they can be iterated. But when used in a case statement, those features aren't used by the implementation and are not accessible by the user.
I misphrased. When specializing for enums it would specialize for all mutable class attributes, not just enums. I just pointed out the enum pattern to justify spending the time and code. I don't plan to really specialise just for the enum type (I'm not sure how to even do that to begin with).
changed the title [-]Specialising LOAD_ATTR for Enums/mutable class attributes[/-][+]Specialising LOAD_ATTR for mutable class attributes[/+]on Jun 10, 2022
Activity
[-]Optimizing LOAD_ATTR for Enums/mutable class attributes[/-][+]Specialising LOAD_ATTR for Enums/mutable class attributes[/+]rhettinger commentedon Jun 9, 2022
I don't see any reason to preference one namespace over another. I expect that instance namespaces will be dominant in practice, followed by some mix of class namespaces, module namespaces, SimpleNamespaces, and descriptors. In practice, non-enum attribute lookups dominate the Python world.
Also the mention on Enums in docs was a bit gratuitous. The primary advantages of an Enum over a regular class namespace is in how the object print and that they can be iterated. But when used in a case statement, those features aren't used by the implementation and are not accessible by the user.
Fidget-Spinner commentedon Jun 10, 2022
I misphrased. When specializing for enums it would specialize for all mutable class attributes, not just enums. I just pointed out the enum pattern to justify spending the time and code. I don't plan to really specialise just for the enum type (I'm not sure how to even do that to begin with).
[-]Specialising LOAD_ATTR for Enums/mutable class attributes[/-][+]Specialising LOAD_ATTR for mutable class attributes[/+]Fidget-Spinner commentedon Jun 10, 2022
I'm going to wait until #93429 is merged because that makes
LOAD_ATTRmore generic (and also gives me more cache to work with).Fidget-Spinner commentedon Jul 8, 2022
Thanks to #94614 and #93910 we don't need this anymore.