Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize ``BINARY_SLICE`` for list, tuple, and unicode by avoiding temporary ``slice`` object creation.
76 changes: 60 additions & 16 deletions Modules/_testinternalcapi/test_cases.c.h

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

46 changes: 37 additions & 9 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,19 +866,47 @@ dummy_func(
}

op(_BINARY_SLICE, (container, start, stop -- res)) {
PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
PyStackRef_AsPyObjectSteal(stop));
PyObject *container_o = PyStackRef_AsPyObjectBorrow(container);
PyObject *start_o = PyStackRef_AsPyObjectBorrow(start);
PyObject *stop_o = PyStackRef_AsPyObjectBorrow(stop);
PyObject *res_o;
// Can't use ERROR_IF() here, because we haven't
// DECREF'ed container yet, and we still own slice.
if (slice == NULL) {
res_o = NULL;
if (PyList_CheckExact(container_o) ||
PyTuple_CheckExact(container_o) ||
PyUnicode_CheckExact(container_o))
{
Py_ssize_t len = PyUnicode_CheckExact(container_o)
? PyUnicode_GET_LENGTH(container_o)
: Py_SIZE(container_o);
Py_ssize_t istart = 0, istop = PY_SSIZE_T_MAX;
if (!_PyEval_SliceIndex(start_o, &istart) ||
!_PyEval_SliceIndex(stop_o, &istop))
{
res_o = NULL;
}
else {
PySlice_AdjustIndices(len, &istart, &istop, 1);
if (PyList_CheckExact(container_o)) {
res_o = PyList_GetSlice(container_o, istart, istop);
}
else if (PyTuple_CheckExact(container_o)) {
res_o = PyTuple_GetSlice(container_o, istart, istop);
}
else {
res_o = PyUnicode_Substring(container_o, istart, istop);
}
}
}
else {
res_o = PyObject_GetItem(PyStackRef_AsPyObjectBorrow(container), slice);
Py_DECREF(slice);
PyObject *slice = PySlice_New(start_o, stop_o, NULL);
Comment thread
cocolato marked this conversation as resolved.
if (slice == NULL) {
res_o = NULL;
}
else {
res_o = PyObject_GetItem(container_o, slice);
Py_DECREF(slice);
}
}
PyStackRef_CLOSE(container);
DECREF_INPUTS();
Comment thread
cocolato marked this conversation as resolved.
ERROR_IF(res_o == NULL);
res = PyStackRef_FromPyObjectSteal(res_o);
}
Expand Down
93 changes: 72 additions & 21 deletions Python/executor_cases.c.h

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

76 changes: 60 additions & 16 deletions Python/generated_cases.c.h

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

Loading