[Python-Dev] Nested scopes core dump
Tim Peters
tim.one@home.com
Tue, 20 Mar 2001 11:01:21 -0500
[Michael Hudson]
>>> Maybe you could do the check for resize *after* the call to
>>> insertdict? I think that would work, but I wouldn't like to go
>>> messing with such a performance critical bit of code without some
>>> careful thinking.
[Guido]
>> No, that could still decide to resize, couldn't it?
[Michael]
> Yes, but not when you're inserting on a key that is already in the
> dictionary - because the resize would have happened when the key was
> inserted into the dictionary, and thus the problem we're seeing here
> wouldn't happen.
Careful: this comment is only half the truth:
/* if fill >= 2/3 size, double in size */
The dictresize following is also how dicts *shrink*. That is, build up a
dict, delete a whole bunch of keys, and nothing at all happens to the size
until you call setitem again (actually, I think you need to call it more than
once -- the behavior is tricky). In any case, that a key is already in the
dict does not guarantee that a dict won't resize (via shrinking) when doing a
setitem.
We could bite the bullet and add a new PyDict_AdjustSize function, just
duplicating the resize logic. Then loops that know they won't be changing
the size can call that before starting. Delicate, though.