Skip to content

Commit 42cd547

Browse files
committed
perf: Use Formatter.write_str instead of write! macro in the Display trait implementation for InlineError
1 parent 62d785d commit 42cd547

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

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 `Formatter.write_str` instead of `write!` macro in the `Display` trait implementation for `InlineError`. [#85](https://github.com/Stranger6667/css-inline/issues/85)
8+
59
## [0.5.0] - 2020-08-07
610

711
### Added

benches/inliner.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2-
use css_inline::inline;
2+
use css_inline::{inline, InlineError};
33

44
fn simple(c: &mut Criterion) {
55
let html = black_box(
@@ -25,6 +25,11 @@ fn simple(c: &mut Criterion) {
2525
c.bench_function("simple HTML", |b| b.iter(|| inline(html).unwrap()));
2626
}
2727

28+
fn error_formatting(c: &mut Criterion) {
29+
let error = black_box(InlineError::ParseError("Error description".to_string()));
30+
c.bench_function("error formatting", |b| b.iter(|| format!("{}", error)));
31+
}
32+
2833
fn merging(c: &mut Criterion) {
2934
let html = black_box(
3035
r#"<html>
@@ -156,5 +161,5 @@ BEGIN FOOTER
156161
c.bench_function("big email", |b| b.iter(|| inline(html).unwrap()));
157162
}
158163

159-
criterion_group!(benches, simple, merging, big_email);
164+
criterion_group!(benches, simple, merging, big_email, error_formatting);
160165
criterion_main!(benches);

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ impl Error for InlineError {}
3434
impl Display for InlineError {
3535
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
3636
match self {
37-
InlineError::IO(error) => write!(f, "{}", error),
38-
InlineError::Network(error) => write!(f, "{}", error),
39-
InlineError::ParseError(error) => write!(f, "{}", error),
37+
InlineError::IO(error) => f.write_str(error.to_string().as_str()),
38+
InlineError::Network(error) => f.write_str(error.to_string().as_str()),
39+
InlineError::ParseError(error) => f.write_str(error.as_str()),
4040
}
4141
}
4242
}

0 commit comments

Comments
 (0)