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

Optimize dict.__init__ and dict.update for dict argument #82400

Closed
serhiy-storchaka opened this issue Sep 18, 2019 · 2 comments
Closed

Optimize dict.__init__ and dict.update for dict argument #82400

serhiy-storchaka opened this issue Sep 18, 2019 · 2 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@serhiy-storchaka
Copy link
Member

BPO 38219
Nosy @rhettinger, @methane, @serhiy-storchaka
PRs
  • bpo-38219: Optimize dict creating and updating by a dict. #16268
  • 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 = None
    closed_at = <Date 2019-09-25.06:49:29.279>
    created_at = <Date 2019-09-18.20:46:08.582>
    labels = ['interpreter-core', '3.9', 'performance']
    title = 'Optimize dict.__init__ and dict.update for dict argument'
    updated_at = <Date 2019-09-25.06:49:29.278>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-09-25.06:49:29.278>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-09-25.06:49:29.279>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2019-09-18.20:46:08.582>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38219
    keywords = ['patch']
    message_count = 2.0
    messages = ['352753', '353148']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'methane', 'serhiy.storchaka']
    pr_nums = ['16268']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue38219'
    versions = ['Python 3.9']

    @serhiy-storchaka
    Copy link
    Member Author

    dict.__init__() and dict.update() with a positional argument look up the "keys" attribute of the argument to distinguish a mapping from a sequence of item-value pairs. It has a non-trivial cost. Merging dicts is optimized for exact dicts in PyDict_Merge(), so it would be worth to optimize also this check.

    $ ./python -m pyperf timeit -s "d = {}; D = dict" -- "D(d)"
    Unpatched:  Mean +- std dev: 154 ns +- 4 ns
    Patched:    Mean +- std dev: 110 ns +- 4 ns
    
    $ ./python -m pyperf timeit -s "d = {}" -- "d.update(d)"
    Unpatched:  Mean +- std dev: 112 ns +- 3 ns
    Patched:    Mean +- std dev: 70.4 ns +- 1.5 ns

    The cost of the check is about 40 ns, and it is a significant part of the total time of creating/updating a small dict.

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Sep 18, 2019
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset f163aea by Serhiy Storchaka in branch 'master':
    bpo-38219: Optimize dict creating and updating by a dict. (GH-16268)
    f163aea

    @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
    3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant