Skip to content

Commit 5ca58de

Browse files
committed
perf: Avoid creating unused caches
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent 2c7cbff commit 5ca58de

File tree

8 files changed

+32
-14
lines changed

8 files changed

+32
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Performance
66

77
- Slightly improve performance of HTML serialization.
8+
- Avoid creating unused caches.
89

910
## [0.14.4] - 2024-12-27
1011

bindings/c/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Slightly improve performance of HTML serialization.
8+
- Avoid creating unused caches.
9+
510
## [0.14.4] - 2024-12-27
611

712
### Changed

bindings/javascript/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Slightly improve performance of HTML serialization.
8+
- Avoid creating unused caches.
9+
510
### Changed
611

712
- Bump MSRV to `1.71.1`.

bindings/python/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Slightly improve performance of HTML serialization.
8+
- Avoid creating unused caches.
9+
510
### Changed
611

712
- Update `PyO3` to `0.23.0`.

bindings/ruby/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
### Performance
6+
7+
- Slightly improve performance of HTML serialization.
8+
- Avoid creating unused caches.
9+
510
### Changed
611

712
- Bump MSRV to `1.71.1`.

css-inline/src/html/document.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,10 @@ impl Document {
297297
pub(crate) fn select<'a, 'b, 'c>(
298298
&'a self,
299299
selectors: &'b str,
300-
caches: &'c mut [NthIndexCache],
300+
caches: &'c mut NthIndexCache,
301301
) -> Result<Select<'a, 'c>, ParseError<'b>> {
302302
select(self, selectors, caches)
303303
}
304-
305-
pub(crate) fn build_caches(&self) -> Vec<NthIndexCache> {
306-
(0..self.elements.len())
307-
.map(|_| NthIndexCache::default())
308-
.collect()
309-
}
310304
}
311305

312306
impl std::ops::Index<NodeId> for Document {

css-inline/src/html/iter.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@ use super::{
66
Specificity,
77
};
88
use selectors::NthIndexCache;
9-
use std::iter::Zip;
109

1110
/// Compile selectors from a string and create an element iterator that yields elements matching these selectors.
1211
#[inline]
1312
pub(crate) fn select<'a, 'b, 'c>(
1413
document: &'a Document,
1514
selectors: &'b str,
16-
caches: &'c mut [NthIndexCache],
15+
caches: &'c mut NthIndexCache,
1716
) -> Result<Select<'a, 'c>, ParseError<'b>> {
1817
Selectors::compile(selectors).map(|selectors| Select {
1918
document,
20-
iter: document.elements.iter().zip(caches.iter_mut()),
19+
caches,
20+
iter: document.elements.iter(),
2121
selectors,
2222
})
2323
}
2424

2525
/// An element iterator adaptor that yields elements matching given selectors.
2626
pub(crate) struct Select<'a, 'c> {
2727
document: &'a Document,
28-
iter: Zip<std::slice::Iter<'a, NodeId>, std::slice::IterMut<'c, NthIndexCache>>,
28+
caches: &'c mut NthIndexCache,
29+
iter: std::slice::Iter<'a, NodeId>,
2930
/// The selectors to be matched.
3031
selectors: Selectors,
3132
}
@@ -43,13 +44,13 @@ impl<'a> Iterator for Select<'a, '_> {
4344

4445
#[inline]
4546
fn next(&mut self) -> Option<Element<'a>> {
46-
for (element_id, cache) in self.iter.by_ref() {
47+
for element_id in self.iter.by_ref() {
4748
let NodeData::Element { element, .. } = &self.document[*element_id].data else {
4849
unreachable!("Element ids always point to element nodes")
4950
};
5051
let element = Element::new(self.document, *element_id, element);
5152
for selector in self.selectors.iter() {
52-
if element.matches(selector, cache) {
53+
if element.matches(selector, self.caches) {
5354
return Some(element);
5455
}
5556
}

css-inline/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use error::InlineError;
3737
use indexmap::IndexMap;
3838
#[cfg(feature = "stylesheet-cache")]
3939
use lru::{DefaultHasher, LruCache};
40+
use selectors::NthIndexCache;
4041
use std::{borrow::Cow, fmt::Formatter, hash::BuildHasherDefault, io::Write, sync::Arc};
4142

4243
use crate::html::ElementStyleMap;
@@ -439,7 +440,8 @@ impl<'a> CSSInliner<'a> {
439440
{
440441
rule_list.push(rule);
441442
}
442-
let mut caches = document.build_caches();
443+
// This cache is unused but required in the `selectors` API
444+
let mut caches = NthIndexCache::default();
443445
for (selectors, (start, end)) in &rule_list {
444446
// Only CSS Syntax Level 3 is supported, therefore it is OK to split by `,`
445447
// With `is` or `where` selectors (Level 4) this split should be done on the parser level

0 commit comments

Comments
 (0)