Issue 25949: Lazy creation of dict in OrderedDict (original) (raw)

Issue25949

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.

This issue has been migrated to GitHub: https://github.com/python/cpython/issues/70137

classification

Title: Lazy creation of __dict__ in OrderedDict
Type: enhancement Stage: resolved
Components: Extension Modules Versions: Python 3.6

process

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.

Files
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 test_ordered_dict.py would fail when the changes to odictobject.c were not applied and it did not. The code change to test_ordered_dict.py 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 https://hg.python.org/cpython/rev/caab6b356a9e
History
Date User Action Args
2022-04-11 14:58:25 admin set github: 70137
2017-04-25 01🔞03 jcea set nosy: + jcea
2016-02-08 14:40:06 serhiy.storchaka set status: open -> closedresolution: fixedstage: patch review -> resolved
2016-02-08 14:39:36 python-dev set nosy: + python-devmessages: +
2016-01-19 13:56:45 serhiy.storchaka set assignee: serhiy.storchakamessages: +
2015-12-26 20:01:16 Winterflower set nosy: + Winterflowermessages: +
2015-12-25 12:48:08 serhiy.storchaka create