Skip to content

Commit 06a7865

Browse files
committed
perf: Do not do binary search on attributes
1 parent 8b1c6c9 commit 06a7865

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

bindings/javascript/__test__/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ test("invalid style", (t) => {
6464

6565
const inlinedHtml = `<html><head>
6666
67-
<link href="/rss.xml" rel="alternate" title="RSS" type="application/rss+xml">
67+
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml">
6868
6969
</head>
7070
<body>

bindings/python/tests-py/test_inlining.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_remote_stylesheet(href, kwargs):
118118
inlined
119119
== """<html><head>
120120
121-
<link href="/rss.xml" rel="alternate" title="RSS" type="application/rss+xml">
121+
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml">
122122
123123
</head>
124124
<body>

css-inline/src/html/attributes.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,30 +160,27 @@ impl Attributes {
160160
let attr = attributes.swap_remove(idx);
161161
class = Some(Class::new(attr.value));
162162
}
163-
attributes.sort_unstable_by(|a, b| a.name.cmp(&b.name));
164163
Attributes { attributes, class }
165164
}
166165

167-
pub(crate) fn find(&self, needle: &QualName) -> Option<usize> {
168-
if let Ok(idx) = self
169-
.attributes
170-
.binary_search_by(|probe| probe.name.cmp(needle))
171-
{
172-
Some(idx)
173-
} else {
174-
None
175-
}
166+
pub(crate) fn find(&self, needle: &QualName) -> Option<&str> {
167+
self.attributes.iter().find_map(|probe| {
168+
if probe.name == *needle {
169+
Some(&*probe.value)
170+
} else {
171+
None
172+
}
173+
})
176174
}
177175

178176
/// Checks if the attributes map contains a given local name.
179177
pub(crate) fn contains(&self, local: html5ever::LocalName) -> bool {
180-
let needle = QualName::new(None, ns!(), local);
181-
self.find(&needle).is_some()
178+
self.get(local).is_some()
182179
}
183180

184181
/// Get the value of the attribute with the given local name, if it exists.
185182
pub(crate) fn get(&self, local: html5ever::LocalName) -> Option<&str> {
186183
let needle = QualName::new(None, ns!(), local);
187-
self.find(&needle).map(|idx| &*self.attributes[idx].value)
184+
self.find(&needle)
188185
}
189186
}

css-inline/src/html/element.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ impl<'a> selectors::Element for Element<'a> {
163163
ns_url.clone(),
164164
local_name.clone().into_inner(),
165165
))
166-
.map(|idx| &*attrs.attributes[idx].value)
167166
.map_or(false, |value| operation.eval_str(value)),
168167
}
169168
}

css-inline/src/html/parser.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ impl TreeSink for Sink {
231231
.expect("not an element");
232232
let attributes = &mut element.attributes;
233233
for attr in attrs {
234-
if let Err(idx) = attributes
234+
if attributes
235235
.attributes
236-
.binary_search_by(|entry| entry.name.cmp(&attr.name))
236+
.iter()
237+
.any(|entry| entry.name == attr.name)
237238
{
238-
attributes.attributes.insert(idx, attr);
239+
attributes.attributes.push(attr);
239240
}
240241
}
241242
}

0 commit comments

Comments
 (0)