4 ms·
I agree with the author. Bytes is basically just C++'s `std::string` (kind of don't bite my head off people who've memorized the C++17 standard). Its an ARC ba
by valarauca1 9y ago
I agree with the author.
Bytes is basically just C++'s `std::string` (kind of don't bite my head off people who've memorized the C++17 standard). Its an ARC backed array [1]. This suggests it should be a fairly fundamental abstraction.
Edit: ^^^My C++ is wrong sorry :(
Really I disagree with its purpose. In its immutable, non-threaded safe form you can create the same structure by just borrowing a value. This ofc requires making your peace with the borrow checker and `Cow<'a, T>` copy on write types.
By in large Bytes advertised purpose is network code. And for networking code its really only _super_ useful if your using a Packet Ring in Linux. As jemalloc will not return regularly re-allocated buffers.
Really this is all performance theater. How you manage/architect your socket reads/writes will have an order of magnitude larger effect then what abstraction you _store_ those bytes in once read.
[1] https://carllerche.github.io/bytes/bytes/struct.Bytes.htm https://carllerche.github.io/bytes/bytes/struct.Bytes.htm
- tatterdemalion 9y agoBytes is intended for use with the tokio ecoystem where you cna't use references often because borrowing across a yield point in a future would be a lifetime error.
- valarauca1 9y agoCan't `Vec<u8>` serve the same purpose?
- tatterdemalion 9y agoThen you have to do a deep clone every time. Arc<Vec<u8>> is also not sufficient, because Bytes lets you share a reference count among slices to different offset into the buffer.
- dbaupp 9y agoBeing an ARC backed array isn't like std::string, and I don't see why having that copy-on-write behaviour implies it is fundamental. Could you expand?
- valarauca1 9y agoThis is my mistake. I assumed shared pointer semantics and applied it to the wrong type :\
- wuch 9y agoIn pre C++11 it was quite typical for std::string to be implemented with COW semantics. Since C++11 standard it is no longer permitted, though it is not necessarily reflected in all stdlib implementations.