Skip to content

Changes in the compiler and dictionary to make key sharing in classes more effective. #77

Closed

Description

@markshannon

Effective key-sharing is important for memory use and performance of class instances. With #72 it will become even more important.
Unfortunately, there are a few patterns that interfere with key sharing, when we generate the set of keys dynamically.
Anecdotally, in real-world applications there are many classes where keys are not shared.

E.g.

class C:
    foo = None #default value
    def __init__(self):
         self.bar = 0
    def meth(self):
        self.foo = 1

Building key sets dynamically will result in the set {'bar'}. Any objects that lazily update foo will be unable to share keys. By statically analyzing the class in the compiler, we can see that the set {'bar','foo'} would be a better choice.
Instances that do not define foo will waste a small amount of memory, but the overall saving from improved sharing would more than compensate.

To do:

  • Gather data on the number and kinds of classes where key sharing does not currently work well.
  • Discuss of Python Dev exactly when dictionary ordering should apply. Possibly write a PEP
  • Implement generating key hints in compiler and using them in class creation.

Activity

  1. markshannon commented on Sep 22, 2021

    @markshannon
    MemberAuthor

    Closing this in favor of the more dynamic approach of #30 and #72

  2. moved this from Todo to Other in Fancy CPython Boardon Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions