Skip to content
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

Specialising LOAD_ATTR for mutable class attributes #93657

Open
Fidget-Spinner opened this issue Jun 9, 2022 · 3 comments
Open

Specialising LOAD_ATTR for mutable class attributes #93657

Fidget-Spinner opened this issue Jun 9, 2022 · 3 comments
Assignees
Labels
interpreter-core performance type-feature

Comments

@Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented Jun 9, 2022

Currently the following code doesn't specialise:

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.

@Fidget-Spinner Fidget-Spinner added type-feature performance interpreter-core labels Jun 9, 2022
@Fidget-Spinner Fidget-Spinner changed the title Optimizing LOAD_ATTR for Enums/mutable class attributes Specialising LOAD_ATTR for Enums/mutable class attributes Jun 9, 2022
@Fidget-Spinner Fidget-Spinner self-assigned this Jun 9, 2022
@rhettinger
Copy link
Contributor

@rhettinger rhettinger commented 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
Copy link
Member Author

@Fidget-Spinner Fidget-Spinner commented 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).

@Fidget-Spinner Fidget-Spinner changed the title Specialising LOAD_ATTR for Enums/mutable class attributes Specialising LOAD_ATTR for mutable class attributes Jun 10, 2022
@Fidget-Spinner
Copy link
Member Author

@Fidget-Spinner Fidget-Spinner commented Jun 10, 2022

I'm going to wait until #93429 is merged because that makes LOAD_ATTR more generic (and also gives me more cache to work with).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core performance type-feature
Projects
None yet
Development

No branches or pull requests

2 participants