Search code examples
filerustio

Rust IO when in --release mode incrediably fast, but writing time is horriably slow


Tell me if I need to provide more info. So, My program can eat a 75 gig .bin file in no time at all, but writing those files takes a long time, I wonder if it is because even though they are .bin files, i can still read them as text. Here is my writing to Disk code:

println!("\nWriting to Disk...\n");
fs::create_dir(Path::new(&name_path)).unwrap();
let mut layer_path = format!("quirkscape/saves/{}/world.bin", name.trim());
let mut world_file = File::create(layer_path).expect("Uh Oh");
for k in data {
    write!(world_file, "{}", k).unwrap();
}

Solution

  • You can use a BufWriter so that any writes are done in batches.

    It can be excessively inefficient to work directly with something that implements Write. For example, every call to write on TcpStream results in a system call. A BufWriter<W> keeps an in-memory buffer of data and writes it to an underlying writer in large, infrequent batches.

    let mut world_file = BufWriter::new(File::create(layer_path).expect("Uh Oh"));
                
    for k in data {
        write!(world_file, "{}", k).unwrap();
    }