[New-bugs-announce] [issue44821] Instance dictionaries should be created eagerly

Mark Shannon report at bugs.python.org
Wed Aug 4 03:57:16 EDT 2021


New submission from Mark Shannon <mark at hotpy.org>:

Currently, instance dictionaries (__dict__ attribute) are created lazily when the first attribute is set.

This is bad for performance for a number of reasons:
1. It causes additional checks on every attribute access.
2. It causes allocation of the object and its dict to be temporarily separated, most likely leading to increased physical separation and worse cache behavior.
3. It has a large impact on specialization, as the first SET_ATTR for an object has to behave differently.


Creating a __dict__ lazily does not save a significant amount of memory.
If an object has a __dict__ slot, then it will end up with a dictionary before it dies in almost all cases.

Many objects, e.g. ints, floats, don't have a dictionary. They are unaffected.

Plain python objects that have no instance attributes are extremely rare, unless they have __slots__, in which case they don't have a dictionary anyway.

The remaining case is subclasses of builtin types that do not add extra attributes, but these are rare, and the overhead of an empty dictionary is only 64 bytes (on a 64 bit machine).

----------
assignee: Mark.Shannon
components: Interpreter Core
messages: 398864
nosy: Mark.Shannon
priority: normal
severity: normal
status: open
title: Instance dictionaries should be created eagerly
type: performance
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44821>
_______________________________________


More information about the New-bugs-announce mailing list