mirror of
https://github.com/solaeus/nucleo.git
synced 2024-12-22 09:57:49 +00:00
allow sorting to be canceled
this removes all left over UI lag
This commit is contained in:
parent
91a265eb6b
commit
4049cdbd13
@ -12,6 +12,7 @@ use crate::worker::Woker;
|
|||||||
pub use nucleo_matcher::{chars, Matcher, MatcherConfig, Utf32Str};
|
pub use nucleo_matcher::{chars, Matcher, MatcherConfig, Utf32Str};
|
||||||
|
|
||||||
mod boxcar;
|
mod boxcar;
|
||||||
|
mod par_sort;
|
||||||
mod pattern;
|
mod pattern;
|
||||||
mod utf32_string;
|
mod utf32_string;
|
||||||
mod worker;
|
mod worker;
|
||||||
|
@ -7,6 +7,7 @@ use nucleo_matcher::MatcherConfig;
|
|||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rayon::{prelude::*, ThreadPool};
|
use rayon::{prelude::*, ThreadPool};
|
||||||
|
|
||||||
|
use crate::par_sort::par_quicksort;
|
||||||
use crate::pattern::{self, MultiPattern};
|
use crate::pattern::{self, MultiPattern};
|
||||||
use crate::{boxcar, Match};
|
use crate::{boxcar, Match};
|
||||||
|
|
||||||
@ -200,14 +201,17 @@ impl<T: Sync + Send + 'static> Woker<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.canceled.load(atomic::Ordering::Relaxed) {
|
let canceled = par_quicksort(
|
||||||
self.was_canceled = true;
|
&mut self.matches,
|
||||||
} else {
|
|match1, match2| {
|
||||||
// TODO: cancel sort in progress?
|
if match1.score > match2.score {
|
||||||
self.matches.par_sort_unstable_by(|match1, match2| {
|
return true;
|
||||||
match2.score.cmp(&match1.score).then_with(|| {
|
}
|
||||||
if match1.idx == u32::MAX || match2.idx == u32::MAX {
|
if match1.idx == u32::MAX {
|
||||||
return match1.idx.cmp(&match2.idx);
|
return false;
|
||||||
|
}
|
||||||
|
if match2.idx == u32::MAX {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
// the tie breaker is comparitevly rarely needed so we keep it
|
// the tie breaker is comparitevly rarely needed so we keep it
|
||||||
// in a branch especially because we need to access the items
|
// in a branch especially because we need to access the items
|
||||||
@ -224,16 +228,23 @@ impl<T: Sync + Send + 'static> Woker<T> {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|haystack| haystack.len() as u32)
|
.map(|haystack| haystack.len() as u32)
|
||||||
.sum();
|
.sum();
|
||||||
(len1, match1.idx).cmp(&(len2, match2.idx))
|
if len1 == len2 {
|
||||||
})
|
match1.idx < match2.idx
|
||||||
});
|
} else {
|
||||||
// let old = self.matches.clone();
|
len1 < len2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
&self.canceled,
|
||||||
|
);
|
||||||
|
|
||||||
|
if canceled {
|
||||||
|
self.was_canceled = true;
|
||||||
|
} else {
|
||||||
self.matches
|
self.matches
|
||||||
.truncate(self.matches.len() - take(self.unmatched.get_mut()) as usize);
|
.truncate(self.matches.len() - take(self.unmatched.get_mut()) as usize);
|
||||||
}
|
|
||||||
|
|
||||||
if self.should_notify.load(atomic::Ordering::Acquire) {
|
if self.should_notify.load(atomic::Ordering::Acquire) {
|
||||||
(self.notify)();
|
(self.notify)();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user