On Tue, 15 Sep 2020 at 09:22, Inada Naoki firstname.lastname@example.org wrote:
On Tue, Sep 15, 2020 at 5:08 AM Marco Sulla Marco.Sulla.Python@gmail.com wrote:
- How can we check the size of an object only if it's an iterable
using the Python C API?
There is no good way. Additionally, we need to know distinct count if we want to preallocate hash table. For example, `len(dict(["foo"]*1000))` is 1, not 1000. [...] We have "one big resize" logic in dict_merge already. And I use dummy empty dictkeys for new empty dict. So we don't allocate any temporary, intermediate dictkey object.
Well, yes, but only for the first positional argument and if it's a map. I would be able to resize to the maximum possible size, that is len(arg) + len(kwarg). Of course the size can be overestimated, but I suppose the overlaps are very rare and small.
The problem is that if I do this resize in dict_new, when the compilation does
python -E -S -m sysconfig --generate-posix-vars
a segfault happens. If I reintroduce the temporary dummy keys, it works.