Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Constant-fold ``_CONTAINS_OP_SET`` for :class:`frozenset`. Patch by Donghee Na.
1 change: 1 addition & 0 deletions Python/optimizer_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
3 changes: 3 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
44 changes: 44 additions & 0 deletions Python/optimizer_cases.c.h

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

3 changes: 2 additions & 1 deletion Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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.

Ah wait this isnt really safe for the case of frozenset IN frozenset

Basically it's only safe if the LHS is a non-container safe type with pure hash function, and the RHS is frozenset

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.

So you need to add a check that says LHS != Frozenset/FrozenDict 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.

Same for frozendict btw.

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 will send a new PR for this :)

}

void
Expand Down
Loading