From 2939bf1a12bcdc310b0abb14a663c1fe3d31168d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Fri, 6 May 2022 23:52:23 +0000 Subject: [PATCH] style: Simplify a bit hot selector-matching loop Note that element.clone() is just copying a couple pointers. Have a single place where we compute the next element and check for linky-ness. This saves a couple checks (very very minor win in the micro-benchmark I've been looking at, but consistent). Depends on D145484 Differential Revision: https://phabricator.services.mozilla.com/D145485 --- components/selectors/matching.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/components/selectors/matching.rs b/components/selectors/matching.rs index 8a69e97f938..c80e81f98c4 100644 --- a/components/selectors/matching.rs +++ b/components/selectors/matching.rs @@ -490,19 +490,21 @@ where Combinator::PseudoElement => SelectorMatchingResult::NotMatchedGlobally, }; - let mut next_element = - next_element_for_combinator(element, combinator, &selector_iter, &context); - // Stop matching :visited as soon as we find a link, or a combinator for // something that isn't an ancestor. - let mut visited_handling = if element.is_link() || combinator.is_sibling() { + let mut visited_handling = if combinator.is_sibling() { VisitedHandlingMode::AllLinksUnvisited } else { context.visited_handling() }; + let mut element = element.clone(); loop { - let element = match next_element { + if element.is_link() { + visited_handling = VisitedHandlingMode::AllLinksUnvisited; + } + + element = match next_element_for_combinator(&element, combinator, &selector_iter, &context) { None => return candidate_not_found, Some(next_element) => next_element, }; @@ -549,12 +551,6 @@ where // matching on the next candidate element. _ => {}, } - - if element.is_link() { - visited_handling = VisitedHandlingMode::AllLinksUnvisited; - } - - next_element = next_element_for_combinator(&element, combinator, &selector_iter, &context); } }