Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-100239: Specialize concatenation of lists and tuples #128956

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,21 @@ def binary_op_add_extend():
self.assert_specialized(binary_op_add_extend, "BINARY_OP_EXTEND")
self.assert_no_opcode(binary_op_add_extend, "BINARY_OP")

def binary_op_add_extend_sequences():
l1 = [1, 2]
l2 = [None]
t1 = (1, 2)
t2 = (None,)
for _ in range(100):
list_sum = l1 + l2
self.assertEqual(list_sum, [1, 2, None])
tuple_sum = t1 + t2
self.assertEqual(tuple_sum, (1, 2, None))

binary_op_add_extend_sequences()
self.assert_specialized(binary_op_add_extend_sequences, "BINARY_OP_EXTEND")
self.assert_no_opcode(binary_op_add_extend_sequences, "BINARY_OP")

def binary_op_zero_division():
def compactlong_lhs(arg):
42 / arg
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Specialize ``BINARY_OP`` for concatenation of lists and tuples.
2 changes: 1 addition & 1 deletion Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ list_concat_lock_held(PyListObject *a, PyListObject *b)
return (PyObject *)np;
}

static PyObject *
PyObject *
list_concat(PyObject *aa, PyObject *bb)
{
if (!PyList_Check(bb)) {
Expand Down
2 changes: 1 addition & 1 deletion Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ PyTuple_GetSlice(PyObject *op, Py_ssize_t i, Py_ssize_t j)
return tuple_slice((PyTupleObject *)op, i, j);
}

static PyObject *
PyObject *
tuple_concat(PyObject *aa, PyObject *bb)
{
PyTupleObject *a = _PyTuple_CAST(aa);
Expand Down
42 changes: 42 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,46 @@ binary_op_fail_kind(int oparg, PyObject *lhs, PyObject *rhs)

/** Binary Op Specialization Extensions */

/* tuple-tuple*/

static int
tuple_tuple_guard(PyObject *lhs, PyObject *rhs)
{
return ( PyTuple_CheckExact(lhs) && PyTuple_CheckExact(rhs) );
}

extern PyObject * tuple_concat(PyObject *aa, PyObject *bb);

static PyObject * \
tuple_tuple_add(PyObject *lhs, PyObject *rhs) \
{
return tuple_concat(lhs, rhs);
}

static _PyBinaryOpSpecializationDescr tuple_tuple_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {tuple_tuple_guard, tuple_tuple_add},
};

/* list-list*/

static int
list_list_guard(PyObject *lhs, PyObject *rhs)
{
return ( PyList_CheckExact(lhs) && PyList_CheckExact(rhs) );
}

extern PyObject * list_concat(PyObject *aa, PyObject *bb);

static PyObject * \
list_list_add(PyObject *lhs, PyObject *rhs) \
{
return list_concat(lhs, rhs);
}

static _PyBinaryOpSpecializationDescr list_list_specs[NB_OPARG_LAST+1] = {
[NB_ADD] = {list_list_guard, list_list_add},
};

/* long-long */

static inline int
Expand Down Expand Up @@ -2566,6 +2606,8 @@ binary_op_extended_specialization(PyObject *lhs, PyObject *rhs, int oparg,
LOOKUP_SPEC(compactlong_float_specs, oparg);
LOOKUP_SPEC(float_compactlong_specs, oparg);
LOOKUP_SPEC(compactlongs_specs, oparg);
LOOKUP_SPEC(list_list_specs, oparg);
LOOKUP_SPEC(tuple_tuple_specs, oparg);
#undef LOOKUP_SPEC
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ Python/specialize.c - cache_requirements -
Python/specialize.c - compactlongs_specs -
Python/specialize.c - float_compactlong_specs -
Python/specialize.c - compactlong_float_specs -
Python/specialize.c - list_list_specs -
Python/specialize.c - tuple_tuple_specs -
Python/stdlib_module_names.h - _Py_stdlib_module_names -
Python/sysmodule.c - perf_map_state -
Python/sysmodule.c - _PySys_ImplCacheTag -
Expand Down
Loading