Skip to content

gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT - #142712

Merged
Fidget-Spinner merged 8 commits into
python:mainfrom
corona10:gh-134584-_STORE_SUBSCR_DICT
Dec 15, 2025
Merged

Fidget-Spinner merged 8 commits into
python:mainfrom
corona10:gh-134584-_STORE_SUBSCR_DICT

Conversation

@corona10

@corona10 corona10 commented Dec 14, 2025 •

Copy link
Copy Markdown
Member
@bedevere-app bedevere-app Bot mentioned this pull request Dec 14, 2025
35 tasks done
@corona10
corona10 marked this pull request as draft December 14, 2025 14:27
@corona10 corona10 changed the title gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT Dec 14, 2025
Comment thread Python/bytecodes.c Outdated
}
INPUTS_DEAD();
st = dict_st;
sb = sub;

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.

Reference to sub is already stolen by _PyDict_SetItem_Take2. So you can't pass this down.

Comment thread Python/bytecodes.c Outdated
PyStackRef_CLOSE(dict_st);
ERROR_IF(err);
if (err) {
ERROR_NO_POP();

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.

We can't do this here. The problem is that _PyDict_SetItem_Take2 has already stolen the references to sub and value. This means when we jump to error, we decref sub and value again, causing a double decref.

The fix might be to do

if (err) {
    PyStackRef_CLOSE(dict_st);
    ERROR_IF(1);
}
@corona10 corona10 changed the title [wip] gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT Dec 15, 2025
@corona10
corona10 marked this pull request as ready for review December 15, 2025 05:13
@corona10

Copy link
Copy Markdown
Member Author

@Fidget-Spinner Done :) Thanks for the guide.

Comment thread Python/optimizer_bytecodes.c Resolved

@Fidget-Spinner Fidget-Spinner 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.

LGTM. Thanks!

@Fidget-Spinner
Fidget-Spinner enabled auto-merge (squash) December 15, 2025 15:56
@Fidget-Spinner
Fidget-Spinner enabled auto-merge (squash) December 15, 2025 16:10
@Fidget-Spinner
Fidget-Spinner merged commit abaaeee into python:main Dec 15, 2025
fatelei pushed a commit to fatelei/cpython that referenced this pull request Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants