Skip to content

Commit bc80bcd

Browse files
committed
perf(cli): Use std::fs::read_to_string in CLI to avoid over/under allocating of the input buffer
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent fb35686 commit bc80bcd

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Use `std::fs::read_to_string` in CLI to avoid over/under allocating of the input buffer.
8+
59
## [0.7.5] - 2021-07-24
610

711
### Fixed

css-inline/src/main.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
borrow::Cow,
55
error::Error,
66
ffi::OsString,
7-
fs::File,
7+
fs::{read_to_string, File},
88
io::{self, Read, Write},
99
path::Path,
1010
};
@@ -101,8 +101,7 @@ fn main() -> Result<(), Box<dyn Error>> {
101101
args.files
102102
.par_iter()
103103
.map(|file_path| {
104-
File::open(file_path)
105-
.and_then(read_file)
104+
read_to_string(file_path)
106105
.and_then(|contents| {
107106
let path = Path::new(file_path);
108107
let mut new_filename = OsString::from("inlined.");
@@ -130,8 +129,3 @@ fn main() -> Result<(), Box<dyn Error>> {
130129
}
131130
Ok(())
132131
}
133-
134-
fn read_file(mut file: File) -> io::Result<String> {
135-
let mut contents = String::with_capacity(1024);
136-
file.read_to_string(&mut contents).and(Ok(contents))
137-
}

0 commit comments

Comments
 (0)