-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider switching Bytes
to a "trait object", supporting custom memory management strategies.
#294
Comments
I expect that in a lot of cases, llvm will be able to inline through these function pointers, as long as it can prove to itself that I think another way would be to use the C++ pattern for allocator-aware containers, where Is there a good reason to prefer this sample code above with an explicit vtable pointer, to letting the compiler generate that and using a what the language calls a trait object? My impression was that the latter approach is more idiomatic but maybe there's something I'm missing |
Personally I'd prefer C++ allocator-aware style container, where user defines allocator and controls all allocations via static interface rather than using plain function pointers. It must be noted that stateful allocator is likely to be necessity, and it would make sense for data to be stored in allocator instance, rather than in This is btw what
I wouldn't trust compiler to do it properly. |
Could you provide a concrete example of this? |
The main concern is state, which will have to go as argument and be stored by Another thing is that vtable is unlikely to be inlined unless you're carefully designing it, which would imply each allocation/reallocation incur extra cost (which is ofc minor, but still) vtable interface is likely to be inflexible and limiting as user will be limited in available methods and most likely will just have to define allocator object, have |
Currently,
Bytes
uses its own internal implementation for memory management. It supports a few strategies:Vec<u8>
Arc<Vec<u8>>
However, using a fixed strategy prohibits alternate strategies that would be more appropriate to the use case in question. One example is initializing a
Bytes
instance backed by a file usingmmap
.Proposal
The
Bytes
struct would be updated to:An additional
Bytes::from_raw
constructor would be added. The rest of theBytes
API can be implemented using the above APIs.Provided strategies
By default, the
bytes
crate should provide some strategies for usingBytes
. A default ofArc<Vec<u8>>
seems sufficient. Feature flags can be used to opt-out of the default implementation.BytesMut
The
BytesMut
structure is no longer a core part of thebytes
API. It is unclear if it should stay in thebytes
crate as an option or move out to a separate crate.Inline representation
This proposal would remove the ability to initialize a
Bytes
instance using the "inline" representation. It is unclear how big of an impact this would have. It is also unclear if use cases that take advantage of "inline" representation would be able to use an alternate strategy enabled with this change.Refs #269
The text was updated successfully, but these errors were encountered: