Skip to content

Commit b18ec81

Browse files
committed
perf: use btree entry
1 parent 9a04ae6 commit b18ec81

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

css-inline/src/html/attributes.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use html5ever::{local_name, namespace_url, ns, tendril::StrTendril, QualName};
2-
use std::collections::BTreeMap;
2+
use std::collections::{btree_map::Entry, BTreeMap};
33

44
/// A collection of HTML attributes.
55
#[derive(Debug)]
@@ -39,15 +39,8 @@ impl Attributes {
3939
.map(|value| &**value)
4040
}
4141

42-
pub(crate) fn get_style_mut(&mut self) -> Option<&mut StrTendril> {
42+
pub(crate) fn get_style_entry(&mut self) -> Entry<'_, QualName, StrTendril> {
4343
self.map
44-
.get_mut(&QualName::new(None, ns!(), local_name!("style")))
45-
}
46-
47-
pub(crate) fn set_style(&mut self, style: String) {
48-
self.map.insert(
49-
QualName::new(None, ns!(), local_name!("style")),
50-
style.into(),
51-
);
44+
.entry(QualName::new(None, ns!(), local_name!("style")))
5245
}
5346
}

css-inline/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use indexmap::IndexMap;
3535
use smallvec::{smallvec, SmallVec};
3636
use std::{
3737
borrow::Cow,
38+
collections::btree_map::Entry,
3839
io::{ErrorKind, Write},
3940
};
4041

@@ -257,18 +258,22 @@ impl<'a> CSSInliner<'a> {
257258
};
258259
styles.sort_unstable_by(|_, (a, _), _, (b, _)| a.cmp(b));
259260
let attributes = &mut element.attributes;
260-
if let Some(existing_style) = attributes.get_style_mut() {
261-
merge_styles(existing_style, styles)?;
262-
} else {
263-
let mut final_styles = String::with_capacity(128);
264-
for (name, (_, value)) in styles {
265-
final_styles.push_str(name.as_str());
266-
final_styles.push(':');
267-
replace_double_quotes!(final_styles, name, value);
268-
final_styles.push(';');
261+
match attributes.get_style_entry() {
262+
Entry::Vacant(entry) => {
263+
let mut final_styles = String::with_capacity(128);
264+
for (name, (_, value)) in styles {
265+
final_styles.push_str(name.as_str());
266+
final_styles.push(':');
267+
replace_double_quotes!(final_styles, name, value);
268+
final_styles.push(';');
269+
}
270+
entry.insert(final_styles.into());
269271
}
270-
attributes.set_style(final_styles);
271-
};
272+
Entry::Occupied(mut entry) => {
273+
let existing_style = entry.get_mut();
274+
merge_styles(existing_style, styles)?;
275+
}
276+
}
272277
}
273278
Ok(document)
274279
}

0 commit comments

Comments
 (0)