Skip to content

Commit bfa3cfa

Browse files
committed
perf: Avoid BTreeMap::insert when style attribute already exists
1 parent 3068898 commit bfa3cfa

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Performance
1010

1111
- Reduce the number of `String` allocations.
12+
- Avoid `BTreeMap::insert` when `style` attribute already exists
1213

1314
## [0.3.1] - 2020-06-25
1415

python/CHANGELOG.md

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

77
- Remove debug symbols from the release build
88

9+
### Performance
10+
11+
- Various performance improvements
12+
913
## [0.2.0] - 2020-06-25
1014

1115
### Added

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ fn process_css(document: &NodeRef, css: &str) -> Result<()> {
275275
// It can be borrowed if the current selector matches <link> tag, that is
276276
// already borrowed in `inline_to`. We can ignore such matches
277277
if let Ok(mut attributes) = matching_element.attributes.try_borrow_mut() {
278-
let style = if let Some(existing_style) = attributes.get("style") {
279-
merge_styles(existing_style, &rule.declarations)?
278+
if let Some(existing_style) = attributes.get_mut("style") {
279+
*existing_style = merge_styles(existing_style, &rule.declarations)?
280280
} else {
281281
let mut final_styles = String::with_capacity(32);
282282
for (name, value) in &rule.declarations {
@@ -285,9 +285,8 @@ fn process_css(document: &NodeRef, css: &str) -> Result<()> {
285285
final_styles.push_str(value);
286286
final_styles.push(';');
287287
}
288-
final_styles
288+
attributes.insert("style", final_styles);
289289
};
290-
attributes.insert("style", style);
291290
}
292291
}
293292
}

0 commit comments

Comments
 (0)