Skip to content

Commit 8f482bd

Browse files
committed
perf: Pre-allocate more memory for output HTML to avoid resizing
1 parent d3d8dd7 commit 8f482bd

2 files changed

Lines changed: 6 additions & 4 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+
- Pre-allocate more memory for output HTML to avoid resizing.
8+
59
## [0.8.0] - 2022-01-09
610

711
### Added

css-inline/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,8 @@ impl<'a> CSSInliner<'a> {
208208
/// - Internal CSS selector parsing error;
209209
#[inline]
210210
pub fn inline(&self, html: &str) -> Result<String> {
211-
// Allocating the same amount of memory as the input HTML helps to avoid
212-
// some allocations, but most probably the output size will be different than
213-
// the original HTML
214-
let mut out = Vec::with_capacity(html.len());
211+
// Allocating more memory than the input HTML, as the inlined version is usually bigger
212+
let mut out = Vec::with_capacity(html.len() * 2);
215213
self.inline_to(html, &mut out)?;
216214
Ok(String::from_utf8_lossy(&out).to_string())
217215
}

0 commit comments

Comments
 (0)