Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1928314

Browse files
committed
Speed dictresize by collapsing its two passes into one; the reason given
in the comments for using two passes was bogus, as the only object that can get decref'ed due to the copy is the dummy key, and decref'ing dummy can't have side effects (for one thing, dummy is immortal! for another, it's a string object, not a potentially dangerous user-defined object).
1 parent b686791 commit 1928314

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

Objects/dictobject.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,17 @@ dictresize(dictobject *mp, int minused)
396396
mp->ma_fill = 0;
397397
mp->ma_used = 0;
398398

399-
/* Make two passes, so we can avoid decrefs
400-
(and possible side effects) till the table is copied */
399+
/* Copy the data over; this is refcount-neutral for active entries;
400+
dummy entries aren't copied over, of course */
401401
for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
402-
if (ep->me_value != NULL)
403-
insertdict(mp,ep->me_key,ep->me_hash,ep->me_value);
404-
}
405-
for (i = 0, ep = oldtable; i < oldsize; i++, ep++) {
406-
if (ep->me_value == NULL) {
407-
Py_XDECREF(ep->me_key);
402+
if (ep->me_value != NULL) /* active entry */
403+
insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
404+
405+
else if (ep->me_key != NULL) { /* dummy entry */
406+
assert(ep->me_key == dummy);
407+
Py_DECREF(ep->me_key);
408408
}
409+
/* else key == value == NULL: nothing to do */
409410
}
410411

411412
if (oldtable != NULL)

0 commit comments

Comments
 (0)