reseve_inner will double the size of the underlying shared vector instead of just extending the BytesMut buffer in place if a call to BytesMut::reserve would fit inside the current allocated shared buffer. If this happens repeatedly you will eventually attempt to allocate a buffer that is too large.
Repro
fn reserve_in_arc_unique_does_not_overallocate_after_split() {
let mut bytes = BytesMut::from(LONG);
let orig_capacity = bytes.capacity();
drop(bytes.split_off(LONG.len() / 2));
let new_capacity = bytes.capacity();
bytes.reserve(orig_capacity - new_capacity);
assert_eq!(bytes.capacity(), orig_capacity);
}
Summary
reseve_inner
will double the size of the underlying shared vector instead of just extending the BytesMut buffer in place if a call toBytesMut::reserve
would fit inside the current allocated shared buffer. If this happens repeatedly you will eventually attempt to allocate a buffer that is too large.Repro