From 51ac2d366789fea42e9d1ee9294fe6065e128fe8 Mon Sep 17 00:00:00 2001 From: Michael Sloan Date: Tue, 31 Dec 2024 15:37:41 -0700 Subject: [PATCH] Check cancel in multithreaded fuzzy matching (#22525) For both the strings and paths multithreaded matching it would still aggregate the response even though it is unneeded. It now checks cancel. In the paths matcher, cancel is now checked within the loop, since it was calling `match_candidates` even though no further results would be computed. Release Notes: - N/A --- crates/fuzzy/src/paths.rs | 13 ++++++++++++- crates/fuzzy/src/strings.rs | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/fuzzy/src/paths.rs b/crates/fuzzy/src/paths.rs index 9f93b497b50d7..bc3e399dc233c 100644 --- a/crates/fuzzy/src/paths.rs +++ b/crates/fuzzy/src/paths.rs @@ -3,7 +3,10 @@ use std::{ borrow::Cow, cmp::{self, Ordering}, path::Path, - sync::{atomic::AtomicBool, Arc}, + sync::{ + atomic::{self, AtomicBool}, + Arc, + }, }; use crate::{ @@ -154,6 +157,10 @@ pub async fn match_path_sets<'a, Set: PathMatchCandidateSet<'a>>( let mut tree_start = 0; for candidate_set in candidate_sets { + if cancel_flag.load(atomic::Ordering::Relaxed) { + break; + } + let tree_end = tree_start + candidate_set.len(); if tree_start < segment_end && segment_start < tree_end { @@ -202,6 +209,10 @@ pub async fn match_path_sets<'a, Set: PathMatchCandidateSet<'a>>( }) .await; + if cancel_flag.load(atomic::Ordering::Relaxed) { + return Vec::new(); + } + let mut results = segment_results.concat(); util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a)); results diff --git a/crates/fuzzy/src/strings.rs b/crates/fuzzy/src/strings.rs index da9b737e663c4..458278739ab2a 100644 --- a/crates/fuzzy/src/strings.rs +++ b/crates/fuzzy/src/strings.rs @@ -8,7 +8,7 @@ use std::{ cmp::{self, Ordering}, iter, ops::Range, - sync::atomic::AtomicBool, + sync::atomic::{self, AtomicBool}, }; #[derive(Clone, Debug)] @@ -178,6 +178,10 @@ pub async fn match_strings( }) .await; + if cancel_flag.load(atomic::Ordering::Relaxed) { + return Vec::new(); + } + let mut results = segment_results.concat(); util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a)); results