-
Notifications
You must be signed in to change notification settings - Fork 292
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
io::Write
via buf::Writer
on BytesMut
significantly slower than on Vec<u8>
#531
Comments
I could be wrong, but at 2 versus 7 ns by iter you are not testing anything, it's just too close and < 10 ns is very very very small. Your two snipped |
|
This is definitely a small amount of time - but also consider that only a very small workload of writing 128 bytes is carried out. If you look at the throughput numbers, you can see that they are is still far from saturating main memory bandwidth. Also, while 2 and 7ns are close in absolute terms, they are definitely not in relative ones. Which matters if you do a large amount of small writes. I carried out the same experiment with a larger payload of 1024 bytes and also included a "nothing" benchmark: #[bench]
fn bytes_mut_io_write(b: &mut Bencher) {
use std::io::Write;
let mut buffer = BytesMut::with_capacity(1024);
let bytes = [1u8; 1024];
b.bytes = bytes.len() as u64;
b.iter(|| {
(&mut buffer).writer().write(&bytes).unwrap();
test::black_box(&buffer);
unsafe {
buffer.set_len(0);
}
})
}
#[bench]
fn vec_io_write(b: &mut Bencher) {
use std::io::Write;
let mut buffer = Vec::with_capacity(1024);
let bytes = [1u8; 1024];
b.bytes = bytes.len() as u64;
b.iter(|| {
buffer.write(&bytes).unwrap();
test::black_box(&buffer);
unsafe {
buffer.set_len(0);
}
})
}
#[bench]
fn array_write(b: &mut Bencher) {
let mut buffer = [0u8; 1024];
let bytes = [1u8; 1024];
b.bytes = bytes.len() as u64;
b.iter(|| {
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), buffer.as_mut_ptr(), bytes.len()) };
test::black_box(&buffer);
})
}
#[bench]
fn nothing(b: &mut Bencher) {
let buffer = [0u8; 1024];
let bytes = [1u8; 1024];
b.bytes = bytes.len() as u64;
b.iter(|| {
test::black_box(&buffer);
})
}
The relative difference between |
Heyo there,
when writing
serde_json::to_writer
onBytesMut
vs. writingserde_json::to_vec
I saw a slowdown of roughly 40% depending on input size – while I expected them to perform equally fast.The following benchmarks
yielded
Some more integrated benchmarks:
279.92 ns
226.44 ns
195.28 ns
Give that even writing to
Vec<u8>
and copying the result over to a newBytesMut
was faster than taking theWriter
, I think this could use some attention.I'm wondering if this is a conceputal difference in
BytesMut
or something that could be improved via #425/#478?The text was updated successfully, but these errors were encountered: