# Add zero-copy conversion of \`bytearray\` to \`bytes\` by providing \`\_\_bytes\_\_()\`

**URL:** <https://discuss.python.org/t/add-zero-copy-conversion-of-bytearray-to-bytes-by-providing-bytes/79164>\
**Category:** Ideas\
**Tags:** performance\
**Created:** [February 2, 2025, 4:25am UTC](https://discuss.python.org/t/add-zero-copy-conversion-of-bytearray-to-bytes-by-providing-bytes/79164 "2025-02-02T04:25:16Z")\
**Posts on this page:** 1\
**Showing post:** 70

<div class="post-metadata">

**Author:** ![cosmicexplorer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cosmicexplorer/32/31291_2.png) [@cosmicexplorer](https://discuss.python.org/u/cosmicexplorer)\
**Post date:** [January 15, 2026, 11:21am UTC](https://discuss.python.org/t/add-zero-copy-conversion-of-bytearray-to-bytes-by-providing-bytes/79164/70 "2026-01-15T11:21:21Z")

</div>

Hello, I just found [gh-139871: Add `bytearray.take_bytes([n])` to efficiently extract `by… · python/cpython@732224e · GitHub](https://github.com/python/cpython/commit/732224e1139f7ed4fe0259a2dad900f84910949e) and realized so so so many of the changes I had been making on my own were being realized! This work is so so exciting!!!

I am not sure of the best venue to mention this, but I have been working for much of last year on path strings and vfs (filesystem) operations in `std::{fs,path}` in the rust stdlib. In particular the `getdents()` libc call provides much greater atomicity guarantees and has been standardized by POSIX as of 2024 [Add musl and glibc bindings for getdents{,64} by cosmicexplorer · Pull Request #4522 · rust-lang/libc · GitHub](https://github.com/rust-lang/libc/pull/4522) and was immediately supported in musl libc.

This thread investigates the goal of zero-copy i/o, which I’m just delighted to see. There are a few avenues I’ve been investigating along these lines:

1. First of all, the `getdents()` libc call introduces a very peculiar set of alignment and lifetime constraints: [https://github.com/rust-lang/rust/issues/43467#issuecomment-3741642799](https://github.com/rust-lang/rust/issues/43467#issuecomment-3741642799)

2. On the subject of buffers: `readlink{,at}()` is sometimes done with an allocating loop, as in the rust stdlib.

3. SIMD operation checks in the configure script for byte scanning: [Comparing python:main...cosmicexplorer:byte-set-splitting · python/cpython · GitHub](https://github.com/python/cpython/compare/main...cosmicexplorer:cpython:byte-set-splitting#diff-49473dca262eeab3b4a43002adb08b4db31020d190caaad1594b47f1d5daa810R8163)

I am absolutely not yet an expert on bits and bytes and simd, but I know I can make finding SIMD instructions in our configure script extremely robust. I am actually looking to do a phd thesis on parsing and text search some day and would love to help contribute to this kind of work in any way I can.

Once again: I was _overjoyed_ to see someone else working on this and identifying how generally useful it can be for perf. I have two specific cases (url quoting and reading directory entries) where I think we can improve perf a huge amount. I also think URL parsing can be improved in this and other ways.

Please let me know if any of that would be useful to investigate further! I have \*not" yet prototyped using getdents() over readdir() in cpython (but getdents is just _perfect_ for python coroutines!!!).

Thanks!!

---

_[View the full topic](https://discuss.python.org/t/add-zero-copy-conversion-of-bytearray-to-bytes-by-providing-bytes/79164)._
