Skip to content

[mypyc] Add inline primitives for bytes.__getitem__ - #20552

Merged
JukkaL merged 7 commits into
masterfrom
bytes-get-item
Jan 9, 2026
Merged

JukkaL merged 7 commits into
masterfrom
bytes-get-item

Conversation

@JukkaL

@JukkaL JukkaL commented Jan 9, 2026 •

Copy link
Copy Markdown
Collaborator

These are similar to the ones we have for BytesWriter. These are a bit faster that the old primitive in typical cases (based on a microbenchmark), but in an ideal scenario these allow vectorization by the C compiler and something like 10x performance improvement.

I don't remove the old primitive yet, since it's used in some places which can't currently use the specializer. I'll look at running the specializer in more contexts as a follow-up PR. This PR should cover some of the most common use cases.

Also refactor to share some code with BytesWriter specialization.

@JukkaL
JukkaL merged commit 4912d77 into master Jan 9, 2026
@JukkaL
JukkaL deleted the bytes-get-item branch January 9, 2026 16:12
JukkaL added a commit that referenced this pull request Jan 13, 2026
The approach is similar to #20552, which added a fast inlined
implementation of bytes get item. However, we do it for `ord(str[n])`
instead of just `str[n]`, since the latter produces a reference-counted
string of length 1, which is often too slow for performance-critical
code. Later on the idea is to add a string writer class that supports
quickly appending unicode code points represented as integers.

This makes a micro-benchmark that finds the highest unicode code point
in a string about 18x faster.
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
The approach is similar to python#20552, which added a fast inlined
implementation of bytes get item. However, we do it for `ord(str[n])`
instead of just `str[n]`, since the latter produces a reference-counted
string of length 1, which is often too slow for performance-critical
code. Later on the idea is to add a string writer class that supports
quickly appending unicode code points represented as integers.

This makes a micro-benchmark that finds the highest unicode code point
in a string about 18x faster.
michaelm-openai pushed a commit to michaelm-openai/mypy that referenced this pull request Jan 16, 2026
The approach is similar to python#20552, which added a fast inlined
implementation of bytes get item. However, we do it for `ord(str[n])`
instead of just `str[n]`, since the latter produces a reference-counted
string of length 1, which is often too slow for performance-critical
code. Later on the idea is to add a string writer class that supports
quickly appending unicode code points represented as integers.

This makes a micro-benchmark that finds the highest unicode code point
in a string about 18x faster.
JukkaL pushed a commit that referenced this pull request Nov 27, 2026
The inline `bytes.__getitem__` primitives added in #20552 were used for
any index type, so indexing `bytes` with a slice object failed when the
index wasn't a slice expression. For example, with `s: slice` (or `int |
slice`, or `Any` holding a slice), `b[s]` raised `TypeError: 'slice'
object cannot be interpreted as an integer` instead of returning a
`bytes` object.

Only use the inline primitives when the index is an `int` or a native
int, and fall back to the generic `__getitem__` otherwise. The check is
in `translate_getitem_with_bounds_check`, which is shared with the `str`
and `librt.strings` writer specializations (those callers already check
the index type, or only accept `i64` indices).

The new run test fails without the fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants