Check cancel in multithreaded fuzzy matching (#22525)

Michael Sloan created

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

Change summary

crates/fuzzy/src/paths.rs   | 13 ++++++++++++-
crates/fuzzy/src/strings.rs |  6 +++++-
2 files changed, 17 insertions(+), 2 deletions(-)

Detailed changes

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

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