Skip to content

gh-145214: Narrow _GUARD_TOS_ANY_{SET,DICT} by using probable type - #145215

Merged
corona10 merged 4 commits into
python:mainfrom
corona10:gh-145214
Mar 3, 2026
Merged

corona10 merged 4 commits into
python:mainfrom
corona10:gh-145214

Conversation

@corona10

@corona10 corona10 commented Feb 25, 2026 •

Copy link
Copy Markdown
Member

Comment thread Python/optimizer_bytecodes.c Outdated
PyTypeObject *tp = sym_get_type(tos);
if (tp == &PySet_Type || tp == &PyFrozenSet_Type) {
ADD_OP(_NOP, 0, 0);
sym_set_type(tos, tp);

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.

Why set the type when it is already known?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay, now I get it means that setting the type at this moment is meaningless.

@markshannon markshannon 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.

What we could do here is narrow the guard, if the probable type is either a set or frozen set.

So, leaving the existing check alone, add the following:

tp = sym_get_probable_type(tos);
if (tp == &PySet_Type) {
    ADD_OP(_GUARD_TOS_SET, 0, 0);
}

Likewise for frozen sets, and we could also do the same for dict guards.

@corona10

Copy link
Copy Markdown
Member Author

I just added a new commit:
b28292e
IIUC, we’re trying to narrow:

  • _GUARD_TOS_ANY_SET → _GUARD_TOS_SET or _GUARD_TOS_FROZEN_SET → nop
  • _GUARD_TOS_ANY_DICT → _GUARD_TOS_DICT or _GUARD_TOS_FROZEN_DICT → nop

I believe this should work, but I’m not entirely sure that I implemented _Py_uop_sym_get_probable_type correctly.

cc @Fidget-Spinner

@corona10 corona10 changed the title gh-145214: Add missing sym_set_type in _GUARD_TOS_ANY_SET optimizer gh-145214: Narrow _GUARD_TOS_ANY_{SET,DICT} using probable type in JIT optimizer Feb 25, 2026
@corona10 corona10 changed the title gh-145214: Narrow _GUARD_TOS_ANY_{SET,DICT} using probable type in JIT optimizer gh-145214: Narrow _GUARD_TOS_ANY_{SET,DICT} by using probable type Feb 25, 2026
@corona10
corona10 requested a review from markshannon February 25, 2026 17:43
Comment thread Python/optimizer_symbols.c Resolved
Comment thread Python/optimizer_bytecodes.c Outdated
if (sym_matches_type(tos, &PySet_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PySet_Type);

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 also don't need sym_set_type here? Because we checked the type in sym_matches_type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I’m not sure why we mark the types of tuples and other objects this way.
Maybe we need to better understand the original intention behind it.

op(_GUARD_TOS_LIST, (tos -- tos)) {
if (sym_matches_type(tos, &PyList_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PyList_Type);
}
op(_GUARD_NOS_LIST, (nos, unused -- nos, unused)) {
if (sym_matches_type(nos, &PyList_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(nos, &PyList_Type);
}
op(_GUARD_TOS_TUPLE, (tos -- tos)) {
if (sym_matches_type(tos, &PyTuple_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PyTuple_Type);
}
op(_GUARD_NOS_TUPLE, (nos, unused -- nos, unused)) {
if (sym_matches_type(nos, &PyTuple_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(nos, &PyTuple_Type);
}

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 need to set it here. If you want to make it clearer, you can put an else branch, but I think it isn't needed.

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.

Please use an else branch, it avoids doing needless work

Co-authored-by: Ken Jin <kenjin@python.org>
@corona10
corona10 requested a review from cocolato February 26, 2026 21:11
@Fidget-Spinner

Copy link
Copy Markdown
Member

_Py_uop_sym_get_probable_type looks good to me.

@corona10

corona10 commented Mar 2, 2026

Copy link
Copy Markdown
Member Author

@markshannon Gentle ping?

@markshannon markshannon 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.

Can you replace this pattern:

if (sym_matches_type(sym, T)) {
    ...
}
sym_set_type(sym, T);

with this:

if (sym_matches_type(sym, T)) {
    ...
}
else {
    sym_set_type(sym, T);
}

to avoid doing pointless work.

Comment thread Python/optimizer_bytecodes.c Outdated
if (sym_matches_type(tos, &PyDict_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PyDict_Type);

@markshannon markshannon Mar 2, 2026 •

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.

Suggested change
sym_set_type(tos, &PyDict_Type);
else {
sym_set_type(tos, &PyDict_Type);
}

Avoid the overhead of setting the type on a value we already know the type of

Comment thread Python/optimizer_bytecodes.c Outdated
if (sym_matches_type(tos, &PySet_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PySet_Type);

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.

Please use an else branch, it avoids doing needless work

@bedevere-app

bedevere-app Bot commented Mar 2, 2026

Copy link
Copy Markdown

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

@corona10

corona10 commented Mar 2, 2026

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Mar 2, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@markshannon: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from markshannon March 2, 2026 16:23

@markshannon markshannon 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.

Looks good, thanks.

@corona10
corona10 merged commit 6908372 into python:main Mar 3, 2026
129 of 134 checks passed
@corona10
corona10 deleted the gh-145214 branch March 3, 2026 00:58
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
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.

4 participants