Skip to content

bpo-32623: Resize dict after del/pop - #5300

Closed
methane wants to merge 1 commit into
python:masterfrom
methane:dict-downsize
Closed

methane wants to merge 1 commit into
python:masterfrom
methane:dict-downsize

Conversation

@methane

@methane methane commented Jan 24, 2018 •

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of raising an exception if shrinking fails.

What do you think of being conservative: if shrinking fails, just ignore the error? The next del/pop will try again.

I'm not confortable with dict.pop() which is no longer atomic: an entry was removed and you cannot get it...

Comment thread Objects/dictobject.c
static int
after_removed(PyDictObject *mp)
{
if (mp->ma_keys->dk_size > PyDict_MINSIZE &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I hesitated to propose the same test in your previous PR #5297, but I wasn't sure if it was required in practice since MINSIZE=8.

I like the idea of being explicit here, check size > MINSIZE.

Comment thread Objects/dictobject.c

assert(_PyDict_CheckConsistency(mp));
if (after_removed(mp) < 0) {
Py_DECREF(old_value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that it's ok to drop the popped value here :-(

If shrinking the dict fails, I suggest to ignore the error and return the old value anyway. The next del/pop will try again to shrink the dict.

@bedevere-bot

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@1st1

1st1 commented Jan 24, 2018

Copy link
Copy Markdown
Member

This is a 3.8 thing, right?

@methane methane closed this Jan 29, 2018
@methane
methane deleted the dict-downsize branch April 28, 2025 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants