Skip to content

Commit eb90043

Browse files
committed
bpo-33205: dict: Change GROWTH_RATE from (used*2 + capacity/2) to (used*3)
1 parent 233de02 commit eb90043

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change dict growth function from ``round_up_to_power_2(used*2+capacity/2)`` to ``round_up_to_power_2(used*3)``.
2+
Previously, dict is shrinked only when ``used == 0``. Now dict has more chance to be shrinked.

Objects/dictobject.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,19 @@ dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
396396
*/
397397

398398
/* GROWTH_RATE. Growth rate upon hitting maximum load.
399-
* Currently set to used*2 + capacity/2.
399+
* Currently set to used*3.
400400
* This means that dicts double in size when growing without deletions,
401401
* but have more head room when the number of deletions is on a par with the
402-
* number of insertions.
402+
* number of insertions. See also bpo-17563 and bpo-33205.
403403
* Raising this to used*4 doubles memory consumption depending on the size of
404404
* the dictionary, but results in half the number of resizes, less effort to
405405
* resize.
406+
*
406407
* GROWTH_RATE was set to used*4 up to version 3.2.
407408
* GROWTH_RATE was set to used*2 in version 3.3.0
409+
* GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
408410
*/
409-
#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
411+
#define GROWTH_RATE(d) ((d)->ma_used*3)
410412

411413
#define ENSURE_ALLOWS_DELETIONS(d) \
412414
if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \

0 commit comments

Comments
 (0)