Skip to content

Commit 1aadaff

Browse files
committed
perf: Drop intermediate iterator
1 parent 1070716 commit 1aadaff

1 file changed

Lines changed: 9 additions & 29 deletions

File tree

css-inline/src/html/iter.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,16 @@ pub(crate) fn select<'a, 'b>(
1515
selectors: &'b str,
1616
) -> Result<Select<'a>, ParseError<'b>> {
1717
Selectors::compile(selectors).map(|selectors| Select {
18-
elements: Elements {
19-
document,
20-
iter: document.elements.iter(),
21-
},
18+
document,
19+
iter: document.elements.iter(),
2220
selectors,
2321
})
2422
}
2523

26-
/// An internal iterator that traverses a document.
27-
struct Elements<'a> {
28-
document: &'a Document,
29-
iter: std::slice::Iter<'a, (NodeId, RefCell<NthIndexCache>)>,
30-
}
31-
32-
impl<'a> Iterator for Elements<'a> {
33-
type Item = (Element<'a>, &'a RefCell<NthIndexCache>);
34-
35-
fn next(&mut self) -> Option<Self::Item> {
36-
if let Some((element_id, cache)) = self.iter.next() {
37-
let NodeData::Element { element, .. } = &self.document[*element_id].data else {
38-
unreachable!("Element ids always point to element nodes")
39-
};
40-
Some((Element::new(self.document, *element_id, element), cache))
41-
} else {
42-
// No more elements in the document
43-
None
44-
}
45-
}
46-
}
47-
4824
/// An element iterator adaptor that yields elements matching given selectors.
4925
pub(crate) struct Select<'a> {
50-
elements: Elements<'a>,
26+
document: &'a Document,
27+
iter: std::slice::Iter<'a, (NodeId, RefCell<NthIndexCache>)>,
5128
/// The selectors to be matched.
5229
selectors: Selectors,
5330
}
@@ -65,8 +42,11 @@ impl<'a> Iterator for Select<'a> {
6542

6643
#[inline]
6744
fn next(&mut self) -> Option<Element<'a>> {
68-
// Filter the underlying iterator to only return elements that match any of the selectors
69-
for (element, cache) in self.elements.by_ref() {
45+
for (element_id, cache) in self.iter.by_ref() {
46+
let NodeData::Element { element, .. } = &self.document[*element_id].data else {
47+
unreachable!("Element ids always point to element nodes")
48+
};
49+
let element = Element::new(self.document, *element_id, element);
7050
let mut cache = cache.borrow_mut();
7151
for selector in self.selectors.iter() {
7252
if element.matches(selector, &mut cache) {

0 commit comments

Comments
 (0)