Open
Description
BytesMut
cares about keeping allocation implementation details private, which means no From<Vec<u8>>
and no Into<Vec<u8>>
:
Lines 825 to 830 in 4e2c9c0
However, this information is leaked in the FromIterator
impl:
Lines 1409 to 1413 in f8c7b57
which go through through this specialization:
https://github.com/rust-lang/rust/blob/6c6b3027ef62e911142cfc55589baef4e9f38ec8/library/alloc/src/vec/spec_from_iter.rs#L37-L64
So collection is basically a From<Vec<u8>>
with no overhead, which may not be intended by the authors.
use bytes::BytesMut; // 1.6.1
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc; // 0.3.3
fn main() {
let _guard = dhat::Profiler::new_heap();
let src = vec![1; 1024];
let mut_from_vec = measure(|| BytesMut::from_iter(src));
assert_eq!(mut_from_vec, 0); // doesn't allocate!
let mut_from_array = measure(|| BytesMut::from_iter([1; 1024]));
assert_eq!(mut_from_array, 1024);
}
fn measure<T>(f: impl FnOnce() -> T) -> u64 {
let before = dhat::HeapStats::get();
f();
let after = dhat::HeapStats::get();
after.total_bytes - before.total_bytes
}
OTOH, this probably doesn't count as "an easy way", so maybe it's fine to leave it exposed
Metadata
Metadata
Assignees
Labels
No labels