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
6 changes: 4 additions & 2 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3154,20 +3154,22 @@ def testfunc(n):
uops = get_opnames(ex)
self.assertNotIn("_CHECK_IS_NOT_PY_CALLABLE_KW", uops)

def test_call_len_string(self):
def test_call_len_string_frozen_set_dict(self):
def testfunc(n):
for _ in range(n):
_ = len("abc")
d = ''
_ = len(d)
_ = len(b"def")
_ = len(b"")
_ = len(FROZEN_SET_CONST)
_ = len(FROZEN_DICT_CONST)

_, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertNotIn("_CALL_LEN", uops)
self.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 8)
self.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 10)

def test_call_len_known_length_small_int(self):
# Make sure that len(t) is optimized for a tuple of length 5.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up converting a list to a tuple in the ``tuple(genexpr)`` fast path
and in starred tuple displays (e.g. ``(*a, *b)``) by stealing the list's
items into the tuple instead of copying them.
2 changes: 1 addition & 1 deletion Modules/_remote_debugging/code_objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ parse_code_object(RemoteUnwinderObject *unwinder,

#ifdef Py_GIL_DISABLED
// Handle thread-local bytecode (TLBC) in free threading builds
if (ctx->tlbc_index == 0 || unwinder->debug_offsets.code_object.co_tlbc == 0 || unwinder == NULL) {
if (ctx->tlbc_index == 0 || unwinder == NULL || unwinder->debug_offsets.code_object.co_tlbc == 0) {
// No TLBC or no unwinder - use main bytecode directly
addrq = (uint16_t *)ip - (uint16_t *)meta->addr_code_adaptive;
goto done_tlbc;
Expand Down
10 changes: 8 additions & 2 deletions Python/intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "pycore_genobject.h" // _PyAsyncGenValueWrapperNew
#include "pycore_interpframe.h" // _PyFrame_GetLocals()
#include "pycore_intrinsics.h" // INTRINSIC_PRINT
#include "pycore_list.h" // _PyList_AsTupleAndClear()
#include "pycore_object.h" // _PyObject_IsUniquelyReferenced()
#include "pycore_pyerrors.h" // _PyErr_SetString()
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_typevarobject.h" // _Py_make_typevar()
Expand Down Expand Up @@ -190,8 +192,12 @@ unary_pos(PyThreadState* unused, PyObject *value)
static PyObject *
list_to_tuple(PyThreadState* unused, PyObject *v)
{
assert(PyList_Check(v));
return PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
/* INTRINSIC_LIST_TO_TUPLE is only emitted by the compiler for a
freshly-built, uniquely-referenced temporary list, so steal its items
into the tuple instead of copying them. */
assert(PyList_CheckExact(v));
assert(_PyObject_IsUniquelyReferenced(v));
return _PyList_AsTupleAndClear((PyListObject *)v);
}

static PyObject *
Expand Down
8 changes: 7 additions & 1 deletion Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ dummy_func(void) {
res = sym_new_type(ctx, &PyLong_Type);
Py_ssize_t length = sym_tuple_length(arg);

// Not a tuple, check if it's a const string
// Not a tuple, check if it's another immutable const with known length
if (length < 0 && sym_is_const(ctx, arg)) {
PyObject *const_val = sym_get_const(ctx, arg);
if (const_val != NULL) {
Expand All @@ -2388,6 +2388,12 @@ dummy_func(void) {
else if (PyBytes_CheckExact(const_val)) {
length = PyBytes_GET_SIZE(const_val);
}
else if (PyFrozenDict_CheckExact(const_val)) {
length = PyDict_GET_SIZE(const_val);
}
else if (PyFrozenSet_CheckExact(const_val)) {
length = PySet_GET_SIZE(const_val);
}
}
}

Expand Down
11 changes: 6 additions & 5 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: 3 additions & 0 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
"PyStackRef_RefcountOnObject",
"PyStackRef_TYPE",
"PyStackRef_True",
"PyBytes_GET_SIZE",
"PyDict_GET_SIZE",
"PySet_GET_SIZE",
"PyTuple_GET_ITEM",
"PyTuple_GET_SIZE",
"PyType_HasFeature",
Expand Down
Loading