Skip to content

Conversation

@JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Nov 25, 2025

The librt.strings.BytesWriter class allow building a bytes object from bytes or u8 components.

Simple example use case:

from librt.strings import BytesWriter

w = BytesWriter()
w.write(b'foo')
w.append(ord('!'))
print(w.getvalue())  # b'foo!'

It can be used at an alternative to bytearray or BytesIO in various use cases. The main benefit over stdlib classes is that we can provide (more) efficient mypyc primitives for various operations. The goal is to make it more efficient than either bytearray or BytesIO in many use cases, eventually.

The implementation is based on the implementation of librt.internal.WriteBuffer, but it is somewhat different, since it will support also direct indexed item access (a bit like bytearray), so just appending to the end is not the only important use case. Direct item access is not implemented yet, but I'll add more features in follow-up PRs.

This minimal implementation hasn't been optimized yet, and the API may not be final. It's experimental and only compiled if experimental features are explicitly enabled, so we can still make arbitrary API and ABI changes.

@github-actions

This comment has been minimized.

We don't care about the semantic difference really, since we'll be
dropping 3.9 support soon enough. Just make the test case pass.
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JukkaL JukkaL merged commit 688ea0c into master Nov 25, 2025
21 checks passed
@JukkaL JukkaL deleted the mypyc-bytes-builder branch November 25, 2025 17:20
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.

3 participants