You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems if given iterable is exact set type or exact dict type(it means, the class that inherits dict or set is not the case), preallocate the memory for dict with the given length of iterable
But I cant find the case when that (PyDictObject *)d)->ma_used does not have value 0. d is result of _PyObject_CallNoArg of given type thus there may not exist the case that the given type is exact dict type and not empty 😢
@dieterplex Oh yes that is exactly the case that result of _PyObject_CallNoArg of given type is exact dict type and not empty, as __new__ of x return the non-empty exact dict. I checked it with debugger in cpython 😊
Implementing CPython like dictresize seems need put some effort to it. Alternatively, just route tomerge_object for specific conditions and add a TODO to follow up with a separate issue? @youknowone
And where should I place the unit test file?
@dieterplex that sounds good. I don't feel like we necessarily need tests for the case, but you can put any original tests in extra_tests/snippets directory. Then CI will run every tests inside the directory.
@dieterplex Hi. Are you currently working on this issue? If not, I would like to use this issue for the next sprint this weekend. Please tell me freely.
dict.fromkeys now has the CPython _PyDict_FromKeys fast path: exact dict/set keys reuse stored hashes (fromkeys_known_hashes) and an empty exact dict preallocates with reserve_for_empty (#8503, #8874).
Activity
dieterplex commentedon Mar 30, 2022
Do you mean the cases that
dictresizeoccurs when the keys are dict/set ?Snowapril commentedon Mar 30, 2022
It seems if given
iterableis exactsettype or exactdicttype(it means, the class that inherits dict or set is not the case), preallocate the memory fordictwith the given length ofiterableBut I cant find the case when that
(PyDictObject *)d)->ma_useddoes not have value 0.dis result of_PyObject_CallNoArgof given type thus there may not exist the case that the given type is exactdicttype and not empty 😢dieterplex commentedon Mar 30, 2022
Would this be the a valid case?
dieterplex commentedon Mar 30, 2022
Ah, that must be exact
dict.Snowapril commentedon Mar 30, 2022
@dieterplex Oh yes that is exactly the case that result of
_PyObject_CallNoArgof given type is exactdicttype and not empty, as__new__ofxreturn the non-empty exactdict. I checked it with debugger in cpython 😊dieterplex commentedon Mar 30, 2022
In other words, it should be preallocate when meet conditions. But is there such a concept it could be preallocate for entries, a.k.a.
DictContent?edit: Found
DictInner::resize()insidedictdatatype::Dict.dieterplex commentedon Mar 31, 2022
Implementing CPython like
dictresizeseems need put some effort to it. Alternatively, just route tomerge_objectfor specific conditions and add a TODO to follow up with a separate issue? @youknowoneAnd where should I place the unit test file?
youknowone commentedon Mar 31, 2022
@dieterplex that sounds good. I don't feel like we necessarily need tests for the case, but you can put any original tests in
extra_tests/snippetsdirectory. Then CI will run every tests inside the directory.Snowapril commentedon Apr 7, 2022
@dieterplex Hi. Are you currently working on this issue? If not, I would like to use this issue for the next sprint this weekend. Please tell me freely.
dieterplex commentedon Apr 7, 2022
Not yet, just go ahead no problem.
I look into it and think about is there a way to replace
entrieswithout bulk insert?Snowapril commentedon Apr 7, 2022
Thank 😊. If not used, I'll tell you again in this thread.
youknowone commentedon Oct 6, 2026
dict.fromkeysnow has the CPython_PyDict_FromKeysfast path: exact dict/set keys reuse stored hashes (fromkeys_known_hashes) and an empty exact dict preallocates withreserve_for_empty(#8503, #8874).Closing.
— commented by Grok 4.6