Skip to content

Reduce the number of materialized instance __dict__s #106485

Closed

Description

@markshannon

Accessing the __dict__ attribute of an object causes the dictionary to be materialized.
This has a negative impact on performance.
We should provide means to access the __dict__ without forcing materialization, which can be used in copy, pickle and any third party code that chooses to.

I propose two new methods on object:

  • __dict_copy__(self), which will be equivalent to self.__dict__.copy(), but without materializing the dictionary of objects with virtual dictionaries.
  • __dict_update__(self, arg), which will be equivalent to self.__dict__.update(arg), but without materializing the dictionary of objects with virtual dictionaries.

Linked PRs

Activity

  1. brandtbucher commented on Jul 6, 2023

    @brandtbucher
    Member

    I opened a draft PR for an alternate approach, which aggressively "un-materializes" __dict__s in LOAD_ATTR_WITH_HINT.

  2. changed the title [-]Avoid using `__dict__` in pickle and copy.[/-] [+]Reduce the number of materialized instance `__dict__`s[/+] on Jul 6, 2023
  3. added a commit that references this issue on Aug 9, 2023
  4. kumaraditya303 commented on Aug 10, 2023

    @kumaraditya303
    Contributor

    This change seems to be causing crashes on buildbots. See https://buildbot.python.org/all/#/builders/123/builds/813 as example.

  5. markshannon commented on Aug 10, 2023

    @markshannon
    MemberAuthor

    Two more things to fix:

    • Instances created by copy.deepcopy do not get their __dict__s dematerialized.
    • We shouldn't be creating dictionaries in _PyObjectDict_SetItem for objects with managed __dict__s if the key is in the shared keys.
  6. added 2 commits that reference this issue on Aug 10, 2023
  7. hugovk commented on Nov 10, 2023

    @hugovk
    Member

    Is there more to do here, or can we close this issue?

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

    performancePerformance or resource usage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions