New submission from Serhiy Storchaka <storchaka+cpyt...@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 <rep...@bugs.python.org> <https://bugs.python.org/issue38219> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com