https://github.com/python/cpython/commit/289f19adb0abaaab0e914f52dceca831905a1967
commit: 289f19adb0abaaab0e914f52dceca831905a1967
branch: main
author: Donghee Na <[email protected]>
committer: corona10 <[email protected]>
date: 2026-04-04T12:32:12Z
summary:
gh-148083: Constant-fold _CONTAINS_OP_SET for frozenset (gh-148084)
files:
A
Misc/NEWS.d/next/Core_and_Builtins/2026-04-04-20-59-12.gh-issue-148083.9ZHNBN.rst
M Lib/test/test_capi/test_opt.py
M Python/optimizer_analysis.c
M Python/optimizer_bytecodes.c
M Python/optimizer_cases.c.h
M Python/optimizer_symbols.c
diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index 022f05bbe37fa4..56f90194b480a1 100644
--- a/Lib/test/test_capi/test_opt.py
+++ b/Lib/test/test_capi/test_opt.py
@@ -23,6 +23,9 @@
# For frozendict JIT tests
FROZEN_DICT_CONST = frozendict(x=1, y=2)
+# For frozenset JIT tests
+FROZEN_SET_CONST = frozenset({1, 2, 3})
+
class _GenericKey:
pass
@@ -2169,7 +2172,8 @@ def f(n):
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_GUARD_TOS_ANY_SET", uops)
- self.assertIn("_CONTAINS_OP_SET", uops)
+ # _CONTAINS_OP_SET is constant-folded away for frozenset literals
+ self.assertIn("_INSERT_2_LOAD_CONST_INLINE_BORROW", uops)
def test_remove_guard_for_known_type_tuple(self):
def f(n):
@@ -4399,6 +4403,20 @@ def testfunc(n):
# lookup result is folded to constant 1, so comparison is optimized
away
self.assertNotIn("_COMPARE_OP_INT", uops)
+ def test_contains_op_frozenset_const_fold(self):
+ def testfunc(n):
+ x = 0
+ for _ in range(n):
+ if 1 in FROZEN_SET_CONST:
+ x += 1
+ return x
+
+ res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
+ self.assertEqual(res, TIER2_THRESHOLD)
+ self.assertIsNotNone(ex)
+ uops = get_opnames(ex)
+ self.assertNotIn("_CONTAINS_OP_SET", uops)
+
def test_binary_subscr_list_slice(self):
def testfunc(n):
x = 0
diff --git
a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-04-20-59-12.gh-issue-148083.9ZHNBN.rst
b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-04-20-59-12.gh-issue-148083.9ZHNBN.rst
new file mode 100644
index 00000000000000..fea4659d0b9916
--- /dev/null
+++
b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-04-20-59-12.gh-issue-148083.9ZHNBN.rst
@@ -0,0 +1 @@
+Constant-fold ``_CONTAINS_OP_SET`` for :class:`frozenset`. Patch by Donghee Na.
diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c
index 4672a272fc9203..2953311b392600 100644
--- a/Python/optimizer_analysis.c
+++ b/Python/optimizer_analysis.c
@@ -30,6 +30,7 @@
#include "pycore_unicodeobject.h"
#include "pycore_ceval.h"
#include "pycore_floatobject.h"
+#include "pycore_setobject.h"
#include <stdarg.h>
#include <stdbool.h>
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index dfb97625bf924f..6e9a34384ba531 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -706,6 +706,9 @@ dummy_func(void) {
b = sym_new_type(ctx, &PyBool_Type);
l = left;
r = right;
+ if (sym_matches_type(right, &PyFrozenSet_Type)) {
+ REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b);
+ }
}
op(_CONTAINS_OP_DICT, (left, right -- b, l, r)) {
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index 4643a0ed0c5f9d..dc00b6bc1397f5 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -2993,6 +2993,50 @@
b = sym_new_type(ctx, &PyBool_Type);
l = left;
r = right;
+ if (sym_matches_type(right, &PyFrozenSet_Type)) {
+ if (
+ sym_is_safe_const(ctx, left) &&
+ sym_is_safe_const(ctx, right)
+ ) {
+ JitOptRef left_sym = left;
+ JitOptRef right_sym = right;
+ _PyStackRef left = sym_get_const_as_stackref(ctx,
left_sym);
+ _PyStackRef right = sym_get_const_as_stackref(ctx,
right_sym);
+ _PyStackRef b_stackref;
+ _PyStackRef l_stackref;
+ _PyStackRef r_stackref;
+ /* Start of uop copied from bytecodes for constant
evaluation */
+ PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
+ PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
+ assert(PyAnySet_CheckExact(right_o));
+ STAT_INC(CONTAINS_OP, hit);
+ int res = _PySet_Contains((PySetObject *)right_o, left_o);
+ if (res < 0) {
+ JUMP_TO_LABEL(error);
+ }
+ b_stackref = (res ^ oparg) ? PyStackRef_True :
PyStackRef_False;
+ l_stackref = left;
+ r_stackref = right;
+ /* End of uop copied from bytecodes for constant
evaluation */
+ (void)l_stackref;
+ (void)r_stackref;
+ b = sym_new_const_steal(ctx,
PyStackRef_AsPyObjectSteal(b_stackref));
+ if (sym_is_const(ctx, b)) {
+ PyObject *result = sym_get_const(ctx, b);
+ if (_Py_IsImmortal(result)) {
+ // Replace with _INSERT_2_LOAD_CONST_INLINE_BORROW
since we have two inputs and an immortal result
+ ADD_OP(_INSERT_2_LOAD_CONST_INLINE_BORROW, 0,
(uintptr_t)result);
+ }
+ }
+ CHECK_STACK_BOUNDS(1);
+ stack_pointer[-2] = b;
+ stack_pointer[-1] = l;
+ stack_pointer[0] = r;
+ stack_pointer += 1;
+ ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
+ break;
+ }
+ }
CHECK_STACK_BOUNDS(1);
stack_pointer[-2] = b;
stack_pointer[-1] = l;
diff --git a/Python/optimizer_symbols.c b/Python/optimizer_symbols.c
index a0ee175fd10c1a..2614bcd430a2c5 100644
--- a/Python/optimizer_symbols.c
+++ b/Python/optimizer_symbols.c
@@ -283,7 +283,8 @@ _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym)
(typ == &PyFloat_Type) ||
(typ == &_PyNone_Type) ||
(typ == &PyBool_Type) ||
- (typ == &PyFrozenDict_Type);
+ (typ == &PyFrozenDict_Type) ||
+ (typ == &PyFrozenSet_Type);
}
void
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]