Repository navigation
[mypyc] Add primitives for bytearray len, indexing, item assignment and slicing - #22408
Open
Jahongir-Qurbonov wants to merge 2 commits into
Open
Jahongir-Qurbonov wants to merge 2 commits into
Jahongir-Qurbonov wants to merge 2 commits into
Conversation
…nd slicing Add fast paths for len(b), b[i], b[i] = x and b[i:j] when b is a bytearray. The C implementations live in bytearray_extra_ops.c. Indexing, item assignment and slicing only use the fast path for exact bytearray objects with short int arguments, and otherwise fall back to the generic implementation, so subclass overrides and error messages for very large indexes are preserved. On free-threaded builds the item array is accessed while holding the object's lock, since a bytearray can be resized concurrently. Part of mypyc/mypyc#644.
A subclass may override __getitem__ to return something other than a bytearray. Also make the subclass tests use bytearray as the static type, so that they exercise the primitives.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add primitives for these
bytearrayoperations:len(b): readsob_sizedirectly, as forbytes,listandtupleb[i]b[i] = xb[i:j]: coversb[i:]andb[:j]too, and always returns abytearrayThe C implementations are in
bytearray_extra_ops.c.Indexing, item assignment and slicing take the fast path only for exact
bytearrayobjects with short int arguments. Other cases fall back to the generic implementation, which preserves:__getitem__and__setitem__(the result of slicing a subclass is checked to be abytearray, as before)Item assignment checks the value before the index, as CPython does.
On free-threaded builds, the item array is accessed while holding the object's critical section, because a bytearray can be resized concurrently.
Tests compare each operation against a generic call through
Any. They cover:They pass on Python 3.10, 3.13, 3.14 and 3.14t (free-threaded).
b[i] += xstill goes through the generic path, because index assignment targets are typed asobject(the existing TODO inAssignmentTargetIndex).Micro-benchmark (2000 × 1024 operations on a 1024-byte bytearray, the same module compiled before and after this change, Python 3.13):
len(b)b[i]b[i] = xb[i:i + 8]Part of mypyc/mypyc#644.