From 3c3699ba9057e9bc8c9d376a177fdf7547bee7b8 Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:25:30 +0200 Subject: [PATCH] perf: hoist out common subexpressions in satisfies_text_predicates This commit stores the result of text predicates evaluation in a separate variable to ensure that they're computed just once. As is, it is possible for e.g. #match predicates to match node against a regex twice. --- lib/binding_rust/lib.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index bb96753a..afbe029e 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -2551,10 +2551,11 @@ impl<'tree> QueryMatch<'_, 'tree> { let mut text2 = text_provider.text(node2); let text1 = node_text1.get_text(&mut text1); let text2 = node_text2.get_text(&mut text2); - if (text1 == text2) != *is_positive && *match_all_nodes { + let is_positive_match = text1 == text2; + if is_positive_match != *is_positive && *match_all_nodes { return false; } - if (text1 == text2) == *is_positive && !*match_all_nodes { + if is_positive_match == *is_positive && !*match_all_nodes { return true; } } @@ -2565,10 +2566,11 @@ impl<'tree> QueryMatch<'_, 'tree> { for node in nodes { let mut text = text_provider.text(node); let text = node_text1.get_text(&mut text); - if (text == s.as_bytes()) != *is_positive && *match_all_nodes { + let is_positive_match = text == s.as_bytes(); + if is_positive_match != *is_positive && *match_all_nodes { return false; } - if (text == s.as_bytes()) == *is_positive && !*match_all_nodes { + if is_positive_match == *is_positive && !*match_all_nodes { return true; } } @@ -2579,10 +2581,11 @@ impl<'tree> QueryMatch<'_, 'tree> { for node in nodes { let mut text = text_provider.text(node); let text = node_text1.get_text(&mut text); - if (r.is_match(text)) != *is_positive && *match_all_nodes { + let is_positive_match = r.is_match(text); + if is_positive_match != *is_positive && *match_all_nodes { return false; } - if (r.is_match(text)) == *is_positive && !*match_all_nodes { + if is_positive_match == *is_positive && !*match_all_nodes { return true; } }