Skip to content

Mitigating performance regressions in code that explicitly use __dict__ #93350

Closed as not planned
@markshannon

Description

@markshannon

Python 3.11 creates dictionaries lazily.
In general this is a big win. The rare code that explicitly uses __dict__ slows down, but the speedups and memory saving for the vast majority of code make that worthwhile.

The problem is code that is written to use __dict__ to improve performance. For example, functools.cached_property lazily fills in the computed attribute. To do so, it needs to look into the instance dictionary, bypassing usual attribute lookup.

In order to make this perform well for 3.11 we need a way to look at the instance attributes, without creating the dict.
In other words, we need something equivalent to getattr(obj.__dict__, name, default) that does not create the dict.

If we were to add sys._get_instance_attr(obj, name, default) that is defined as being equivalent to

def _get_instance_attr(obj, name, default):
      return getattr(obj.__dict__, name, default)

then we could use that in functools.cached_property (and maybe other places) to prevent the performance regression in 3.11

This is arguably a new feature, and I want it in 3.11, so this might not be an acceptable change. @pablogsal?

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesperformancePerformance or resource usage

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions