https://github.com/python/cpython/commit/dfeeee990bbf846694b44edad0e325e4c62352bd
commit: dfeeee990bbf846694b44edad0e325e4c62352bd
branch: main
author: Pieter Eendebak <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-05-23T15:45:50+05:30
summary:
gh-145192: improve performance of `PySequence_GetSlice` (#145193)
files:
M Include/internal/pycore_sliceobject.h
M Modules/_testinternalcapi/test_cases.c.h
M Objects/sliceobject.c
M Python/bytecodes.c
M Python/executor_cases.c.h
M Python/generated_cases.c.h
diff --git a/Include/internal/pycore_sliceobject.h
b/Include/internal/pycore_sliceobject.h
index ba8b1f1cb27dee3..b6c821764886c36 100644
--- a/Include/internal/pycore_sliceobject.h
+++ b/Include/internal/pycore_sliceobject.h
@@ -12,7 +12,7 @@ extern "C" {
/* runtime lifecycle */
PyAPI_FUNC(PyObject *)
-_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop);
+_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop, PyObject *step);
#ifdef __cplusplus
}
diff --git a/Modules/_testinternalcapi/test_cases.c.h
b/Modules/_testinternalcapi/test_cases.c.h
index a2506524f0bb6dc..b463bb18b160564 100644
--- a/Modules/_testinternalcapi/test_cases.c.h
+++ b/Modules/_testinternalcapi/test_cases.c.h
@@ -12007,7 +12007,8 @@
v = stack_pointer[-4];
_PyFrame_SetStackPointer(frame, stack_pointer);
PyObject *slice =
_PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
- PyStackRef_AsPyObjectSteal(stop));
+ PyStackRef_AsPyObjectSteal(stop),
+ Py_None);
stack_pointer = _PyFrame_GetStackPointer(frame);
int err;
if (slice == NULL) {
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 95f10815687757e..96ff3118dc4405d 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -117,8 +117,8 @@ PyObject _Py_EllipsisObject =
_PyObject_HEAD_INIT(&PyEllipsis_Type);
index is present.
*/
-static PySliceObject *
-_PyBuildSlice_Consume2(PyObject *start, PyObject *stop, PyObject *step)
+PyObject *
+_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop, PyObject *step)
{
assert(start != NULL && stop != NULL && step != NULL);
PySliceObject *obj = _Py_FREELIST_POP(PySliceObject, slices);
@@ -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;
+ return (PyObject *)obj;
error:
Py_DECREF(start);
Py_DECREF(stop);
+ Py_DECREF(step);
return NULL;
}
@@ -153,15 +154,8 @@ 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);
-}
-
-PyObject *
-_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop)
-{
- assert(start != NULL && stop != NULL);
- return (PyObject *)_PyBuildSlice_Consume2(start, stop, Py_None);
+ return _PyBuildSlice_ConsumeRefs(Py_NewRef(start),
+ Py_NewRef(stop),
Py_NewRef(step));
}
PyObject *
@@ -177,9 +171,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, Py_None);
return slice;
}
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index f7487c7136962f1..300b7da753c2baf 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -1116,7 +1116,8 @@ dummy_func(
op(_STORE_SLICE, (v, container, start, stop -- )) {
PyObject *slice =
_PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
-
PyStackRef_AsPyObjectSteal(stop));
+
PyStackRef_AsPyObjectSteal(stop),
+ Py_None);
int err;
if (slice == NULL) {
err = 1;
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index efa61d7de74e88c..952860f01b8a682 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -6811,7 +6811,8 @@
ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__);
_PyFrame_SetStackPointer(frame, stack_pointer);
PyObject *slice =
_PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
- PyStackRef_AsPyObjectSteal(stop));
+ PyStackRef_AsPyObjectSteal(stop),
+ Py_None);
stack_pointer = _PyFrame_GetStackPointer(frame);
int err;
if (slice == NULL) {
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 53e09a8f4523c7c..83051cf41cc043b 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -12004,7 +12004,8 @@
v = stack_pointer[-4];
_PyFrame_SetStackPointer(frame, stack_pointer);
PyObject *slice =
_PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
- PyStackRef_AsPyObjectSteal(stop));
+ PyStackRef_AsPyObjectSteal(stop),
+ Py_None);
stack_pointer = _PyFrame_GetStackPointer(frame);
int err;
if (slice == NULL) {
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]