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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of :c:func:`PySequence_GetSlice`.
Copy link
Contributor

Choose a reason for hiding this comment

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

The Change Looks really good to me, thank You for fixing this!

I only suggest going a Bit more in Detail for the Description, but very nice fix!

LGTM!

Suggested change
Improve performance of :c:func:`PySequence_GetSlice`.
Improve performance of :c:func:`PySequence_GetSlice` by avoiding unnecessary reference count decrements.

15 changes: 7 additions & 8 deletions Objects/sliceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ PyObject _Py_EllipsisObject = _PyObject_HEAD_INIT(&PyEllipsis_Type);
*/

static PySliceObject *
_PyBuildSlice_Consume2(PyObject *start, PyObject *stop, PyObject *step)
_PyBuildSlice_Consume3(PyObject *start, PyObject *stop, PyObject *step)
{
assert(start != NULL && stop != NULL && step != NULL);
PySliceObject *obj = _Py_FREELIST_POP(PySliceObject, slices);
Expand All @@ -131,13 +131,14 @@ _PyBuildSlice_Consume2(PyObject *start, PyObject *stop, PyObject *step)

obj->start = start;
obj->stop = stop;
obj->step = Py_NewRef(step);
obj->step = step;

_PyObject_GC_TRACK(obj);
return obj;
error:
Py_DECREF(start);
Py_DECREF(stop);
Py_DECREF(step);
return NULL;
}

Expand All @@ -153,15 +154,15 @@ PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
if (stop == NULL) {
stop = Py_None;
}
return (PyObject *)_PyBuildSlice_Consume2(Py_NewRef(start),
Py_NewRef(stop), step);
return (PyObject *)_PyBuildSlice_Consume3(Py_NewRef(start),
Py_NewRef(stop), Py_NewRef(step));
}

PyObject *
_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop)
{
assert(start != NULL && stop != NULL);
return (PyObject *)_PyBuildSlice_Consume2(start, stop, Py_None);
return (PyObject *)_PyBuildSlice_Consume3(start, stop, Py_None);
}

PyObject *
Expand All @@ -177,9 +178,7 @@ _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
return NULL;
}

slice = PySlice_New(start, end, NULL);
Py_DECREF(start);
Py_DECREF(end);
slice = _PyBuildSlice_ConsumeRefs(start, end);
return slice;
}

Expand Down
Loading