Skip to content

gh-144569: Scalar replacement of BINARY_SLICE for list, tuple, and unicode#144590

Open
cocolato wants to merge 4 commits intopython:mainfrom
cocolato:optimize/BINARY_SLICE
Open

gh-144569: Scalar replacement of BINARY_SLICE for list, tuple, and unicode#144590
cocolato wants to merge 4 commits intopython:mainfrom
cocolato:optimize/BINARY_SLICE

Conversation

@cocolato
Copy link
Contributor

@cocolato cocolato commented Feb 8, 2026

When the tier 2 optimizer can determine the type of the container being sliced, _BINARY_SLICE can be replaced with a specialized micro-op that bypasses the PySliceObject allocation entirely.

This PR implements scalar replacement of BINARY_SLICE by add new tier2 op _BINARY_SLICE_LIST, _BINARY_SLICE_TUPLE, and _BINARY_SLICE_LIST.

@cocolato
Copy link
Contributor Author

cocolato commented Feb 8, 2026

I think the tests failure that occurred is unrelated to the PR.

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

Pretty close, thanks!

@Fidget-Spinner
Copy link
Member

You forgot to add the new recorder to BINARY_SLICE macro op

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

lgtm just one comment and add news please

PySlice_AdjustIndices(PyUnicode_GET_LENGTH(container_o), &istart, &istop, 1);
res_o = PyUnicode_Substring(container_o, istart, istop);
}
DECREF_INPUTS();
Copy link
Member

Choose a reason for hiding this comment

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

We should split this into the POP_TOP POP_TOP form, can you please do that in a follow up PR?

@Fidget-Spinner
Copy link
Member

Actually I've changed my mind. Can you please open a PR to convert BINARY_SLICE to the POP_TOP form please? Then after we merge that PR we can update this one

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

Let's leave the DECREF_INPUTS alone for now and try to remove it in a future PR.

The code has repeated instances that should be factored out to a micro-op

Comment on lines +888 to +892
if (_PyEval_SliceIndex(PyStackRef_AsPyObjectBorrow(start), &istart) &&
_PyEval_SliceIndex(PyStackRef_AsPyObjectBorrow(stop), &istop)) {
PySlice_AdjustIndices(PyList_GET_SIZE(container_o), &istart, &istop, 1);
res_o = PyList_GetSlice(container_o, istart, istop);
}
Copy link
Member

@Fidget-Spinner Fidget-Spinner Feb 8, 2026

Choose a reason for hiding this comment

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

This is repeated code, it should be factored out to a _UNPACK_INDICES micro-op that looks like this:

Suggested change
if (_PyEval_SliceIndex(PyStackRef_AsPyObjectBorrow(start), &istart) &&
_PyEval_SliceIndex(PyStackRef_AsPyObjectBorrow(stop), &istop)) {
PySlice_AdjustIndices(PyList_GET_SIZE(container_o), &istart, &istop, 1);
res_o = PyList_GetSlice(container_o, istart, istop);
}
int err = _PyEval_SliceIndex(PyStackRef_AsPyObjectBorrow(start), &istart);
ERROR_IF(err == 0);
err = _PyEval_SliceIndex(PyStackRef_AsPyObjectBorrow(stop), &istop);
ERROR_IF(err = 0);
...
istart = PyStackRef_Wrap(...)
...

type == &PyTuple_Type)
{
if (type == &PyList_Type) {
ADD_OP(_BINARY_SLICE_LIST, 0, 0);
Copy link
Member

@Fidget-Spinner Fidget-Spinner Feb 8, 2026

Choose a reason for hiding this comment

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

After factoring out, you can write

ADD_OP(_UNPACK_INDICES,...)
ADD_OP(_BINARY_SLICE_LIST,...)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants