Skip to content

Commit

Permalink
perf: use BufWriter when writing to file
Browse files Browse the repository at this point in the history
BufWriter instead of OpenOptions:
```
Benchmark 1: E:\Phill030.de\Rust\KiWad-Unpacker\target\release\kiwad_unpacker.exe
  Time (mean ± σ):      3.259 s ±  2.254 s    [User: 0.348 s, System: 4.189 s]
  Range (min … max):    0.734 s …  6.740 s    10 runs
```
  • Loading branch information
Phill030 committed May 25, 2024
1 parent 970def1 commit f40ffad
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use binary_modifier::{BinaryError, BinaryReader, Endian};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::{
collections::VecDeque,
fs::{create_dir_all, OpenOptions},
io::{Cursor, Read, Write},
fs::{create_dir_all, File},
io::{BufWriter, Cursor, Read, Write},
path::PathBuf,
};

Expand Down Expand Up @@ -115,14 +115,11 @@ impl<'a> Library<'a> {
}

// Prepare the file path
let mut file_path = path.clone();
file_path.push(&file_record.file_name);
let path = path.join(&file_record.file_name);
create_dir_all(path.parent().unwrap()).unwrap();

create_dir_all(file_path.parent().unwrap()).unwrap();

let mut file = OpenOptions::new().write(true).create(true).open(&file_path).unwrap();
file.write_all(&buffer)
.unwrap_or_else(|e| eprintln!("Could not write to file! {e}"));
let mut file = BufWriter::new(File::create(&path).unwrap());
file.write_all(&buffer).unwrap();
// Write to the file
});
}
Expand Down

0 comments on commit f40ffad

Please sign in to comment.