[New-bugs-announce] [issue38219] Optimize dict.__init__ and dict.update for dict argument

Serhiy Storchaka report at bugs.python.org
Wed Sep 18 16:46:08 EDT 2019


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

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.

----------
components: Interpreter Core
messages: 352753
nosy: rhettinger, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Optimize dict.__init__ and dict.update for dict argument
type: performance
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38219>
_______________________________________


More information about the New-bugs-announce mailing list