From fa2ad327fcda8ef48d1f5692cce8f8b6692bab54 Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 6 Mar 2020 20:28:23 +0100 Subject: [PATCH] Limit channel size and add a BuffWriter to speed up io --- src/main.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 76b7c6c..3e9f85f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ use rpassword::read_password_from_tty; use spinners::{Spinner, Spinners}; use std::fs; use std::fs::File; -use std::io::Write; -use std::sync::mpsc::channel; +use std::io::{BufWriter, Write}; +use std::sync::mpsc::sync_channel; use std::thread; use std::time::Duration; use structopt::StructOpt; @@ -173,9 +173,9 @@ fn decrypt(_opts: &Opts, args: &Decrypt) { /// Creates a dictionary from an input file and writes it to the output file fn create_dictionary(_opts: &Opts, args: &CreateDictionary) { let input: String = (*args.input).parse().unwrap(); - //let reader = BufReader::new(File::open(input).expect("Failed to open input file.")); // TODO: Some form of removing duplicates (without itertools) - let mut fout = File::create(args.output.clone()).unwrap(); + let fout = File::create(args.output.clone()).unwrap(); + let mut writer = BufWriter::new(fout); let handle; { let content = fs::read_to_string(input).expect("Failed to read content"); @@ -185,13 +185,14 @@ fn create_dictionary(_opts: &Opts, args: &CreateDictionary) { pb = ProgressBar::new(lines.clone().count() as u64); } pb.set_max_refresh_rate(Some(Duration::from_millis(200))); - let (rx, tx) = channel::(); + let (rx, tx) = sync_channel::(10_000_000); handle = thread::spawn(move || { for line in tx { - fout.write(&line.as_bytes()).unwrap(); + writer.write(&line.as_bytes()).unwrap(); pb.inc(); } pb.finish(); + writer.flush().expect("Failed to flush the file writer."); }); lines .map(|pw| -> String {