This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients Mark.Shannon, benjamin.peterson, methane, rhettinger, serhiy.storchaka, xiang.zhang
Date 2016-10-25.21:10:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1477429817.12.0.306284563361.issue28509@psf.upfronthosting.co.za>
In-reply-to
Content
LGTM.

Here is a script that produces more compact output. The first column is a size after which the dict is resized.

import sys
p = 10000
b = {}
for i in range(10000):
    a = {}
    b[i] = i
    a.update(b)
    s = sys.getsizeof(a)
    if s > p:
        print(i, p)
    p = s

Unpatched:
5 128
7 196
15 344
31 628
63 1208
127 2612
255 5176
511 10292
1023 20536
2047 41012
4095 81976
8191 163892

Patched:
5 128
10 196
21 344
42 628
85 1208
170 2612
341 5176
682 10292
1365 20536
2730 41012
5461 81976

But I suggest instead the condition

    mp->ma_keys->dk_usable < other->ma_used

use the condition

    mp->ma_used + mp->ma_keys->dk_usable < other->ma_used

If there are overlapping keys this can allow to avoid resizing. In worst keys one resizing will be happened in dictinsert().

Yes one estimation is:

    USABLE_FRACTION(2 * mp->ma_keys->dk_size) < mp->ma_used + other->ma_used

Dict size is at least doubled after resizing. No need to make preliminary resizing if the final result is the same. The benefit is that if there are many overlapping keys the final size can be ESTIMATE_SIZE(other->ma_used) instead of ESTIMATE_SIZE(mp->ma_used + other->ma_used).

All this conditions can be combined (because they have different computational cost):

    mp->ma_keys->dk_usable < other->ma_used &&
    mp->ma_used + mp->ma_keys->dk_usable < other->ma_used &&
    USABLE_FRACTION(2 * mp->ma_keys->dk_size) < mp->ma_used + other->ma_used
History
Date User Action Args
2016-10-25 21:10:17serhiy.storchakasetrecipients: + serhiy.storchaka, rhettinger, benjamin.peterson, methane, Mark.Shannon, xiang.zhang
2016-10-25 21:10:17serhiy.storchakasetmessageid: <1477429817.12.0.306284563361.issue28509@psf.upfronthosting.co.za>
2016-10-25 21:10:17serhiy.storchakalinkissue28509 messages
2016-10-25 21:10:16serhiy.storchakacreate