Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy creation of __dict__ in OrderedDict #70137

Closed
serhiy-storchaka opened this issue Dec 25, 2015 · 4 comments
Closed

Lazy creation of __dict__ in OrderedDict #70137

serhiy-storchaka opened this issue Dec 25, 2015 · 4 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 25949
Nosy @rhettinger, @jcea, @ericsnowcurrently, @serhiy-storchaka
Files
  • odict___dict__.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2016-02-08.14:40:06.098>
    created_at = <Date 2015-12-25.12:48:08.359>
    labels = ['extension-modules', 'type-feature']
    title = 'Lazy creation of __dict__ in OrderedDict'
    updated_at = <Date 2017-04-25.01:18:03.564>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2017-04-25.01:18:03.564>
    actor = 'jcea'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-02-08.14:40:06.098>
    closer = 'serhiy.storchaka'
    components = ['Extension Modules']
    creation = <Date 2015-12-25.12:48:08.359>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['41419']
    hgrepos = []
    issue_num = 25949
    keywords = ['patch']
    message_count = 4.0
    messages = ['256988', '257044', '258593', '259849']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'jcea', 'python-dev', 'eric.snow', 'serhiy.storchaka', 'Winterflower']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue25949'
    versions = ['Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Dec 25, 2015
    @Winterflower
    Copy link
    Mannequin

    Winterflower mannequin commented Dec 26, 2015

    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 (?).

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka self-assigned this Jan 19, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 8, 2016

    New changeset caab6b356a9e by Serhiy Storchaka in branch 'default':
    Issue bpo-25949: __dict__ for an OrderedDict instance is now created only when
    https://hg.python.org/cpython/rev/caab6b356a9e

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant