Skip to content

Commit 28ae90e

Browse files
committed
perf: Reduce the number of allocations in CLI
1 parent 8e38cc2 commit 28ae90e

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
### Performance
1515

16-
- Use `codegen-units=1` and `lto=fat`
16+
- Use `codegen-units=1` and `lto=fat`.
17+
- Reduce the number of allocations in CLI.
1718

1819
## [0.3.3] - 2020-07-07
1920

src/main.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ fn main() -> Result<(), Box<dyn Error>> {
9797
io::stdin().read_to_string(&mut buffer)?;
9898
inliner.inline_to(buffer.as_str().trim(), &mut io::stdout())?;
9999
} else {
100-
let results: Vec<_> = args
101-
.files
100+
args.files
102101
.par_iter()
103102
.map(|filename| {
104103
File::open(filename)
@@ -115,22 +114,19 @@ fn main() -> Result<(), Box<dyn Error>> {
115114
})
116115
.map_err(|error| (filename, error))
117116
})
118-
.collect();
119-
for result in results {
120-
match result {
117+
.for_each(|result| match result {
121118
Ok((filename, result)) => match result {
122119
Ok(_) => println!("{}: SUCCESS", filename),
123120
Err(error) => println!("{}: FAILURE ({})", filename, error),
124121
},
125122
Err((filename, error)) => println!("{}: FAILURE ({})", filename, error),
126-
}
127-
}
123+
});
128124
}
129125
}
130126
Ok(())
131127
}
132128

133129
fn read_file(mut file: File) -> io::Result<String> {
134-
let mut contents = String::new();
130+
let mut contents = String::with_capacity(1024);
135131
file.read_to_string(&mut contents).and(Ok(contents))
136132
}

0 commit comments

Comments
 (0)