
On Sun, Jul 26, 2020 at 4:44 AM Marco Sulla Marco.Sulla.Python@gmail.com wrote:
I also remembered another possible use-case: kwargs in CPython. In C code, kwargs are PyDictObjects. I suppose they are usually not modified; if so, fdict could be used, since it seems to be faster at creation.
I have not confirmed why frozendict is faster than dict for creation. But note that kwargs is not created by `dict(d)`. It is created by PyDict_New() and PyDict_SetItem(). Please benchmark these C APIs if you want to propose the idea.
https://github.com/python/cpython/blob/a74eea238f5baba15797e2e8b570d153bc869... https://github.com/python/cpython/blob/a74eea238f5baba15797e2e8b570d153bc869...
FWIW, I optimized dict(d) in https://bugs.python.org/issue41431 (https://github.com/python/cpython/pull/21674 )
$ ./python -m pyperf timeit --compare-to ./python-master -s 'd=dict.fromkeys(range(1000))' -- 'dict(d)' python-master: ..................... 21.5 us +- 0.2 us python: ..................... 4.52 us +- 0.16 us Mean +- std dev: [python-master] 21.5 us +- 0.2 us -> [python] 4.52 us +- 0.16 us: 4.76x faster (-79%)
Regards,
-- Inada Naoki songofacandy@gmail.com