Avoid creating temporary objects in BINARY_SLICE
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
Currently BINARY_SLICE looks like this:
op(_BINARY_SLICE, (container, start, stop -- res)) {
PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start),
PyStackRef_AsPyObjectSteal(stop));
PyObject *res_o;
if (slice == NULL) {
res_o = NULL;
}
else {
res_o = PyObject_GetItem(PyStackRef_AsPyObjectBorrow(container), slice);
Py_DECREF(slice);
}
PyStackRef_CLOSE(container);
ERROR_IF(res_o == NULL);
res = PyStackRef_FromPyObjectSteal(res_o);
}
Note that we build a slice just for getitem, only to throw it away immediately after.
We can scalar replace the slice in BINARY_SLICE after recording the types. If we record the container type, we can call the slice dispatcher underneath directly without boxing the slice object. See for example BINARY_OP_SUBSCR_LIST_SLICE
We'd need to guard on the type recorded.
I propose to do this in the current optimizer pass rather than the partial evaluation pass, as this is just a simple optimization within a single op, while the PE pass can handle inter-instruction optimization.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
- gh-144590
- gh-156318
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Begin with the BINARY_SLICE entry point and compare it with BINARY_OP_SUBSCR_LIST_SLICE in the current optimizer pass; also read the partial evaluation pass to understand the stated boundary. Done means the recorded container type is guarded and slicing dispatch avoids creating a temporary slice object, with behavior preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- compilers, performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100