Skip to content

Perf regression in Vec<u8> Write impl? #24095

Closed
@frankmcsherry

Description

@frankmcsherry

Hi folks. A bunch of my serialization code recently got a lot slower, and I think I've tracked it down to writing binary data into a Vec<u8>. This used to be quite fast, well above the 8-10GB/s range. It is now about 1GB/s for me. The following main.rs demos the performance I'm seeing.

#![feature(test)]
extern crate test;

use test::Bencher;
use std::io::Write;

#[bench] 
fn bench(bencher: &mut Bencher) {
    let data = &[0u8; 4096];
    let mut buffer = Vec::with_capacity(data.len());
    bencher.bytes = data.len() as u64;
    bencher.iter(|| {
        buffer.clear();
        buffer.write_all(data).unwrap();
    });
}

fn main() {
    let data = &[0u8; 4096];
    let mut buffer = Vec::with_capacity(data.len());

    // writes 4GB, takes .. 4s+
    for _ in (0..(1 << 20)) {
        buffer.clear();
        buffer.write_all(data).unwrap();
    };
}

The perf numbers look like (where main writes 4GB in 4KB chunks):

Echidnatron% cargo bench; time cargo run --release 
     Running target/release/bench-5e9b1b37cda85a22

running 1 test
test bench ... bench:      4086 ns/iter (+/- 596) = 1002 MB/s

test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured

     Running `target/release/bench`
cargo run --release  4.06s user 0.03s system 99% cpu 4.094 total
Echidnatron% 

Sorry if this is old news, or a misdiagnosis. Something is a bit slower now, though. The comments in the source (Vec<T>::push_all) do suggest it isn't stable yet because the impl might get faster; I didn't expect it to get 10x slower on me though :).

Echidnatron% cargo --version
cargo 0.0.1-pre-nightly (d71f748 2015-04-03) (built 2015-04-04)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions