Skip to content

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

Open
corona10 wants to merge 3 commits intopython:mainfrom
corona10:gh-145214
Open

gh-145214: Narrow _GUARD_TOS_ANY_{SET,DICT} by using probable type#145215
corona10 wants to merge 3 commits intopython:mainfrom
corona10:gh-145214

Conversation

@corona10
Copy link
Member

@corona10 corona10 commented Feb 25, 2026

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

Copy link
Member

@markshannon markshannon left a comment

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
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
}

PyTypeObject *
_Py_uop_sym_get_probable_type(JitOptRef ref)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can refer the changes in b42e939.

if (sym_matches_type(tos, &PySet_Type)) {
ADD_OP(_NOP, 0, 0);
}
sym_set_type(tos, &PySet_Type);
Copy link
Contributor

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
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);
}

Co-authored-by: Ken Jin <kenjin@python.org>
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.

3 participants