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.

Title: Lazy creation of __dict__ in OrderedDict
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Winterflower, eric.snow, jcea, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-12-25 12:48 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
odict___dict__.patch serhiy.storchaka, 2015-12-25 12:48 review
Messages (4)
msg256988 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-12-25 12:48
For now OrderedDict always creates an empty dict for __dict__.

>>> from collections import OrderedDict
>>> import gc
>>> gc.get_referents(OrderedDict())
>>> class OD(OrderedDict): pass
>>> gc.get_referents(OD())
[<class '__main__.OD'>, {}]

But dict subclasses (as well as most other classes) create an empty dict for __dict__ only if needed.

>>> class D(dict): pass
>>> d = D()
>>> gc.get_referents(d)
[<class '__main__.D'>]
>>> d.__dict__
>>> gc.get_referents(d)
[{}, <class '__main__.D'>]

This allows to save CPU time for dictionary creation and a memory (144 bytes on 32-bit, twice as much on 64-bit).

Proposed patch makes __dict__ in OrderedDict to be created only if needed.
msg257044 - (view) Author: Camilla Montonen (Winterflower) Date: 2015-12-26 20:01
Hi Serhiy, 
I tried to see whether the patch's unit test in would fail when the changes to odictobject.c were not applied and it did not. 
The code change to does not appear to test the fact that a dict is not automatically created when an ordered dict is instantiated (?).
msg258593 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-19 13:56
The patch have no visible effect, except lesser memory consumption. The latter is hard to measure in tests. Additional tests just ensure that the patch doesn't break existing behavior.
msg259849 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-02-08 14:39
New changeset caab6b356a9e by Serhiy Storchaka in branch 'default':
Issue #25949: __dict__ for an OrderedDict instance is now created only when
Date User Action Args
2022-04-11 14:58:25adminsetgithub: 70137
2017-04-25 01:18:03jceasetnosy: + jcea
2016-02-08 14:40:06serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2016-02-08 14:39:36python-devsetnosy: + python-dev
messages: + msg259849
2016-01-19 13:56:45serhiy.storchakasetassignee: serhiy.storchaka
messages: + msg258593
2015-12-26 20:01:16Winterflowersetnosy: + Winterflower
messages: + msg257044
2015-12-25 12:48:08serhiy.storchakacreate