-
Notifications
You must be signed in to change notification settings - Fork 286
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
Contradictory allocation behaviour compared to documentation #680
Comments
The documentation on this could probably be improved, but the capacity that the I agree that the wording here is confusing, but I think it is the right behavior. |
Is there some API which basically forces me to make reallocations explicit? I would be happy to see if I can improve the documentation a little bit when I have understood it better. |
I don't think we provide that, no. |
Hmm, OK - thank you. I was using a One idea to deal with this was to have two |
An API for getting the refcount is reasonable enough. |
This is based on tokio-rs#680, where it was noted that it is hard to use BytesMut without additional allocations in some circumstances.
This is based on tokio-rs#680, where it was noted that it is hard to use BytesMut without additional allocations in some circumstances.
This is based on tokio-rs#680, where it was noted that it is hard to use BytesMut without additional allocations in some circumstances.
I gave it some thought and for my usecase, an API like #686 seems more usable - it doesn't expose implementation details like refcount, and it gives a cheap way to re-use the original implementation. Do you think that approach is sensible? If so I'd be happy to bring it to the finish line. Thanks! |
This is based on tokio-rs#680, where it was noted that it is hard to use BytesMut without additional allocations in some circumstances.
I've thought about this too. Thanks for posting @shahn! In fact, the reason I first looked into the source code for this crate was because I didn't understand the conditions that cause allocation after reading the documentation. The following snippet from the docs is accurate, but it's not a complete picture of the
In particular, it's not clear that a This is a subtle distinction, but it's important to understand when choosing to use |
I think what you explained about the behaviour is also what I've learned from docs/reading code, hence the idea to use two One thing that confused me initially was that some old docs which are however still readily found using web search explicitly say that BytesMut doesn't implicitly grow its buffer - but that went away once I looked at the right docs. I noticed large memory usage from the OS pov, perhaps there is some fragmentation issue going on with frequent allocations of different sizes, really not sure. It could be fragmentation because the messages I receive have vastly different size. This seems to be better when switching between two buffers using the patch from #686. |
This is based on tokio-rs#680, where it was noted that it is hard to use BytesMut without additional allocations in some circumstances.
This fixes tokio-rs#680, where it was noted that it is hard to use BytesMut without additional allocations in some circumstances.
Closing with #686. |
I hope I'm not just hopelessly confused, but I fail to see what kind of guarantees are made around allocations and using the APIs. For example, https://docs.rs/bytes/latest/bytes/buf/trait.BufMut.html#method.put_slice says that
self must have enough remaining capacity to contain all of src.
but the implementation will happily extend the buffer of aBytesMut
beyond what's reported from a call tocapacity()
. Similarly, the documentation ofBufMut::put_bytes()
explicitly mentions a panic will happen if there is not enough capacity, yet the implementation again does not panic and instead allocates more memory - similarly for the other put* functions.Some functions, like
BytesMut::extend_from_slice()
, explicitly call out this re-allocation behaviour - but what then is the point of that function, if it does the same asput_slice()
?The text was updated successfully, but these errors were encountered: