[New-bugs-announce] [issue25949] Lazy creation of __dict__ in OrderedDict

Serhiy Storchaka report at bugs.python.org
Fri Dec 25 07:48:08 EST 2015


New submission from Serhiy Storchaka:

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.

----------
components: Extension Modules
files: odict___dict__.patch
keywords: patch
messages: 256988
nosy: eric.snow, rhettinger, serhiy.storchaka
priority: normal
severity: normal
stage: patch review
status: open
title: Lazy creation of __dict__ in OrderedDict
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file41419/odict___dict__.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25949>
_______________________________________


More information about the New-bugs-announce mailing list