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
3 changes: 3 additions & 0 deletions Include/cpython/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ _PyEval_RequestCodeExtraIndex(freefunc f) {

PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
PyAPI_FUNC(int) _PyEval_UnpackIndices(PyObject *, PyObject *,
Py_ssize_t,
Py_ssize_t *, Py_ssize_t *);


// Trampoline API
Expand Down
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.
74 changes: 58 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.

44 changes: 35 additions & 9 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -866,19 +866,45 @@ 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)) &&
(start_o == Py_None || PyLong_CheckExact(start_o)) &&
(stop_o == Py_None || PyLong_CheckExact(stop_o))) {
Py_ssize_t len = PyUnicode_CheckExact(container_o)
? PyUnicode_GET_LENGTH(container_o)
: Py_SIZE(container_o);
Py_ssize_t istart, istop;
int err = _PyEval_UnpackIndices(start_o, stop_o, len,
&istart, &istop);
if (err == 0) {
res_o = NULL;
}
else 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce redundant code, we've modified this to call PySlice_New to create the slice. This incurs a slight performance overhead; if needed, I can revert to the original implementation.

if (slice == NULL) {
res_o = NULL;
}
else {
res_o = PyObject_GetItem(container_o, slice);
Py_DECREF(slice);
}
}
PyStackRef_CLOSE(container);
DECREF_INPUTS();
ERROR_IF(res_o == NULL);
res = PyStackRef_FromPyObjectSteal(res_o);
}
Expand Down
20 changes: 20 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2922,6 +2922,26 @@ _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
return 1;
}

int
_PyEval_UnpackIndices(PyObject *start, PyObject *stop,
Py_ssize_t len,
Py_ssize_t *istart, Py_ssize_t *istop)
{
if (len < 0) {
return 0;
}
*istart = 0;
*istop = PY_SSIZE_T_MAX;
if (!_PyEval_SliceIndex(start, istart)) {
return 0;
}
if (!_PyEval_SliceIndex(stop, istop)) {
return 0;
}
PySlice_AdjustIndices(len, istart, istop, 1);
return 1;
}

PyObject *
_PyEval_ImportName(PyThreadState *tstate, PyObject *builtins,
PyObject *globals, PyObject *locals, PyObject *name,
Expand Down
91 changes: 71 additions & 20 deletions Python/executor_cases.c.h

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

74 changes: 58 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