Skip to content

Commit

Permalink
pythonGH-130415: Narrow str to "" based on boolean tests (pythonGH-13…
Browse files Browse the repository at this point in the history
  • Loading branch information
fluhus authored Mar 4, 2025
1 parent b6769e9 commit 691354c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,39 @@ def f(n):
# But all of the appends we care about are still there:
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))

def test_narrow_type_to_constant_str_empty(self):
def f(n):
trace = []
for i in range(n):
# Hopefully the optimizer can't guess what the value is.
# empty is always "", but we can only prove that it's a string:
false = i == TIER2_THRESHOLD
empty = "X"[:false]
trace.append("A")
if not empty: # Kept.
trace.append("B")
if not empty: # Removed!
trace.append("C")
trace.append("D")
if empty: # Removed!
trace.append("X")
trace.append("E")
trace.append("F")
if empty: # Removed!
trace.append("X")
trace.append("G")
return trace

trace, ex = self._run_with_optimizer(f, TIER2_THRESHOLD)
self.assertEqual(trace, list("ABCDEFG") * TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
# Only one guard remains:
self.assertEqual(uops.count("_GUARD_IS_FALSE_POP"), 1)
self.assertEqual(uops.count("_GUARD_IS_TRUE_POP"), 0)
# But all of the appends we care about are still there:
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))

def global_identity(x):
return x

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,7 @@ Keenan Lau
Piers Lauder
Ben Laurie
Yoni Lavi
Amit Lavon
Simon Law
Julia Lawall
Chris Lawrence
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve JIT's ability to optimize strings in boolean contexts.
2 changes: 1 addition & 1 deletion Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ dummy_func(void) {

op(_TO_BOOL_STR, (value -- res)) {
if (!optimize_to_bool(this_instr, ctx, value, &res)) {
res = sym_new_type(ctx, &PyBool_Type);
res = sym_new_truthiness(ctx, value, true);
sym_set_type(value, &PyUnicode_Type);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Python/optimizer_cases.c.h

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

3 changes: 3 additions & 0 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ _Py_uop_sym_set_const(JitOptContext *ctx, JitOptSymbol *sym, PyObject *const_val
else if (type == &PyLong_Type) {
_Py_uop_sym_set_const(ctx, value, Py_GetConstant(Py_CONSTANT_ZERO));
}
else if (type == &PyUnicode_Type) {
_Py_uop_sym_set_const(ctx, value, Py_GetConstant(Py_CONSTANT_EMPTY_STR));
}
// TODO: More types (GH-130415)!
make_const(sym, const_val);
return;
Expand Down

0 comments on commit 691354c

Please sign in to comment.