This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Instance dictionaries should be created eagerly
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Mark.Shannon Nosy List: Mark.Shannon, terry.reedy
Priority: normal Keywords: patch

Created on 2021-08-04 07:57 by Mark.Shannon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27589 merged Mark.Shannon, 2021-08-04 10:38
Messages (3)
msg398864 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-08-04 07:57
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).
msg398908 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-08-04 15:41
New changeset cee67fa66129b5d1db5c8aa3884338f82f0da3de by Mark Shannon in branch 'main':
bpo-44821: Eagerly assign __dict__ for new objects. (GH-27589)
https://github.com/python/cpython/commit/cee67fa66129b5d1db5c8aa3884338f82f0da3de
msg399148 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-08-06 21:03
Close this?
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 88984
2021-08-09 14:57:19Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-08-06 21:03:57terry.reedysetnosy: + terry.reedy
messages: + msg399148
2021-08-04 15:41:22Mark.Shannonsetmessages: + msg398908
2021-08-04 10:38:08Mark.Shannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26091
2021-08-04 07:57:16Mark.Shannoncreate