New submission from Mark Shannon <m...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue44821>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to