Skip to content

Specialising LOAD_ATTR for mutable class attributes #93657

Closed as not planned
Closed as not planned

Description

@Fidget-Spinner

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.

Activity

  1. changed the title [-]Optimizing LOAD_ATTR for Enums/mutable class attributes[/-] [+]Specialising LOAD_ATTR for Enums/mutable class attributes[/+] on Jun 9, 2022
  2. rhettinger commented on Jun 9, 2022

    @rhettinger
    Contributor

    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.

  3. Fidget-Spinner commented on Jun 10, 2022

    @Fidget-Spinner
    MemberAuthor

    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).

  4. changed the title [-]Specialising LOAD_ATTR for Enums/mutable class attributes[/-] [+]Specialising LOAD_ATTR for mutable class attributes[/+] on Jun 10, 2022
  5. Fidget-Spinner commented on Jun 10, 2022

    @Fidget-Spinner
    MemberAuthor

    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).

  6. Fidget-Spinner commented on Jul 8, 2022

    @Fidget-Spinner
    MemberAuthor

    Thanks to #94614 and #93910 we don't need this anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions