classification
Title: Optimize dict.__init__ and dict.update for dict argument
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: methane, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-09-18 20:46 by serhiy.storchaka, last changed 2019-09-25 06:49 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16268 merged serhiy.storchaka, 2019-09-18 20:47
Messages (2)
msg352753 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-18 20:46
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.
msg353148 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-25 06:47
New changeset f163aeaa8c15b806b622c9cb10bc1d2a6e034e24 by Serhiy Storchaka in branch 'master':
bpo-38219: Optimize dict creating and updating by a dict. (GH-16268)
https://github.com/python/cpython/commit/f163aeaa8c15b806b622c9cb10bc1d2a6e034e24
History
Date User Action Args
2019-09-25 06:49:29serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-25 06:47:03serhiy.storchakasetmessages: + msg353148
2019-09-19 07:39:57xtreaksetnosy: + methane
2019-09-18 20:47:30serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request15859
2019-09-18 20:46:08serhiy.storchakacreate