Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-131798: Allow the JIT to remove an extra _TO_BOOL_BOOL for _CONTAINS_OP_DICT#132269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
brandtbucher merged 3 commits intopython:mainfromZheaoli:manjusaka/gh-130415
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletionsLib/test/test_capi/test_opt.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1629,6 +1629,33 @@ def testfunc(n):
self.assertIn("_CONTAINS_OP_SET", uops)
self.assertNotIn("_TO_BOOL_BOOL", uops)

def test_to_bool_bool_contains_op_dict(self):
"""
Test that _TO_BOOL_BOOL is removed from code like:

res = foo in some_dict
if res:
....

"""
def testfunc(n):
x = 0
s = {1: 1, 2: 2, 3: 3}
for _ in range(n):
a = 2
in_dict = a in s
if in_dict:
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.assertIn("_CONTAINS_OP_DICT", uops)
self.assertNotIn("_TO_BOOL_BOOL", uops)


def test_remove_guard_for_known_type_str(self):
def f(n):
for i in range(n):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Allow the JIT to remove an extra ``_TO_BOOL_BOOL`` instruction after
``_CONTAINS_OP_DICT`` by setting the return type to bool.
4 changes: 4 additions & 0 deletionsPython/optimizer_bytecodes.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -481,6 +481,10 @@ dummy_func(void) {
res = sym_new_type(ctx, &PyBool_Type);
}

op(_CONTAINS_OP_DICT, (left, right -- res)) {
res = sym_new_type(ctx, &PyBool_Type);
}

op(_LOAD_CONST, (-- value)) {
PyObject *val = PyTuple_GET_ITEM(co->co_consts, this_instr->oparg);
int opcode = _Py_IsImmortal(val) ? _LOAD_CONST_INLINE_BORROW : _LOAD_CONST_INLINE;
Expand Down
6 changes: 3 additions & 3 deletionsPython/optimizer_cases.c.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp