Skip to content

Commit

Permalink
Check cancel in multithreaded fuzzy matching (#22525)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgsloan authored Dec 31, 2024
1 parent 6ef5d8f commit 51ac2d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion crates/fuzzy/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{
borrow::Cow,
cmp::{self, Ordering},
path::Path,
sync::{atomic::AtomicBool, Arc},
sync::{
atomic::{self, AtomicBool},
Arc,
},
};

use crate::{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion crates/fuzzy/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
cmp::{self, Ordering},
iter,
ops::Range,
sync::atomic::AtomicBool,
sync::atomic::{self, AtomicBool},
};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51ac2d3

Please sign in to comment.