Skip to content
Open
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
50 changes: 50 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ def assert_no_opcode(self, f, opname):
opnames = {instruction.opname for instruction in instructions}
self.assertNotIn(opname, opnames)

def adaptive_counters(self, f):
"""Map each specialized instruction in f to its adaptive counter."""
counters = {}
for instruction in dis.get_instructions(f, adaptive=True):
if instruction.opname == instruction.baseopname:
continue
if instruction.baseopname in ("RESUME", "JUMP_BACKWARD"):
continue
cache = {name: data for name, _, data in instruction.cache_info}
if "counter" in cache:
counters[instruction.offset] = (instruction.opname,
cache["counter"])
return counters

def assert_specialization_stable(self, f, *args, calls=10):
"""Assert that no specialized instruction in f misses its guard.

A guard miss advances the instruction's adaptive counter. f should
have a fresh code object (see reset_code()), otherwise leftover
specializations from earlier runs can show up as misses.
"""
before = self.adaptive_counters(f)
self.assertTrue(before, f"{f.__qualname__} has no specialized "
"instructions")
for _ in range(calls):
f(*args)
after = self.adaptive_counters(f)
# Ignore instructions that only specialize during these calls.
moved = []
for off, (op, counter) in before.items():
if after.get(off) != (op, counter):
now = after[off][1] if off in after else "unspecialized"
moved.append(f"{op} at offset {off}: counter {counter} -> {now}")
self.assertEqual(moved, [],
f"specialized instructions in {f.__qualname__} "
f"missed their guard during {calls} calls")


class TestLoadSuperAttrCache(unittest.TestCase):
def test_descriptor_not_double_executed_on_spec_fail(self):
Expand Down Expand Up @@ -1977,6 +2014,19 @@ def binary_subscr_str_int_non_compact():
self.assert_specialized(binary_subscr_str_int_non_compact, "BINARY_OP_SUBSCR_USTR_INT")
self.assert_no_opcode(binary_subscr_str_int_non_compact, "BINARY_OP_SUBSCR_STR_INT")

@reset_code
def binary_subscr_str_int_latin1():
# Latin-1 characters are interned too, so reads must not miss.
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = "olá mundo, ça va?"
for idx, expected in enumerate(a):
self.assertEqual(a[idx], expected)

binary_subscr_str_int_latin1()
self.assert_specialized(binary_subscr_str_int_latin1, "BINARY_OP_SUBSCR_USTR_INT")
self.assert_no_opcode(binary_subscr_str_int_latin1, "BINARY_OP_SUBSCR_STR_INT")
self.assert_specialization_stable(binary_subscr_str_int_latin1)

def binary_subscr_getitems():
class C:
def __init__(self, val):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Speed up subscripting a non-ASCII string with an integer when the character
read is latin-1. ``BINARY_OP_SUBSCR_USTR_INT`` now returns the interned
one-character string for characters below 256 instead of only ASCII, so
reads of accented characters no longer fall back to the generic path.
6 changes: 4 additions & 2 deletions Modules/_testinternalcapi/test_cases.c.h

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

8 changes: 5 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,11 +1224,13 @@ dummy_func(
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject*)sub));
Py_ssize_t index = ((PyLongObject*)sub)->long_value.ob_digit[0];
EXIT_IF(PyUnicode_GET_LENGTH(str) <= index);
// Specialize for reading an ASCII character from any string:
// Interned one-character strings exist for latin-1 only:
Py_UCS4 c = PyUnicode_READ_CHAR(str, index);
EXIT_IF(Py_ARRAY_LENGTH(_Py_SINGLETON(strings).ascii) <= c);
EXIT_IF(c >= 256);
STAT_INC(BINARY_OP, hit);
PyObject *res_o = (PyObject*)&_Py_SINGLETON(strings).ascii[c];
PyObject *res_o = (c < 128)
? (PyObject*)&_Py_SINGLETON(strings).ascii[c]
: (PyObject*)&_Py_SINGLETON(strings).latin1[c - 128];
s = str_st;
i = sub_st;
INPUTS_DEAD();
Expand Down
6 changes: 4 additions & 2 deletions Python/executor_cases.c.h

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

6 changes: 4 additions & 2 deletions Python/generated_cases.c.h

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

Loading