Skip to content

Commit 68ed09d

Browse files
authored
Merge pull request #13 from unsecretised/optimisations
SUPER FAST RUSTCAST
2 parents daa7e05 + 4953241 commit 68ed09d

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ objc2-application-services = { version = "0.3.2", default-features = false, feat
1818
] }
1919
objc2-foundation = "0.3.2"
2020
rand = "0.9.2"
21+
rayon = "1.11.0"
2122
serde = { version = "1.0.228", features = ["derive"] }
2223
tokio = { version = "1.48.0", features = ["full"] }
2324
toml = "0.9.8"

src/app.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use iced::{
1818

1919
use objc2::rc::Retained;
2020
use objc2_app_kit::NSRunningApplication;
21+
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
22+
use rayon::slice::ParallelSliceMut;
2123

2224
use std::cmp::min;
2325
use std::process::Command;
@@ -154,22 +156,22 @@ impl Tile {
154156
.show_icons
155157
.unwrap();
156158

157-
let home = std::env::var("HOME").unwrap();
159+
let user_local_path = std::env::var("HOME").unwrap() + "/Applications/";
158160

159-
let mut apps = get_installed_apps("/Applications/", store_icons);
160-
apps.append(&mut get_installed_apps(
161+
let paths = vec![
162+
"/Applications/",
163+
user_local_path.as_str(),
161164
"/System/Applications/",
162-
store_icons,
163-
));
164-
apps.append(&mut get_installed_apps(
165-
home + "/Applications/",
166-
store_icons,
167-
));
168-
apps.append(&mut get_installed_apps(
169165
"/System/Applications/Utilities/",
170-
store_icons,
171-
));
172-
apps.sort_by_key(|x| x.name.len());
166+
];
167+
168+
let mut apps: Vec<App> = paths
169+
.par_iter()
170+
.map(|path| get_installed_apps(path, store_icons))
171+
.flatten()
172+
.collect();
173+
174+
apps.par_sort_by_key(|x| x.name.len());
173175

174176
(
175177
Self {
@@ -413,27 +415,29 @@ impl Tile {
413415
}
414416

415417
pub fn handle_search_query_changed(&mut self) {
416-
let filter_vec = if self.query_lc.starts_with(&self.prev_query_lc) {
418+
let filter_vec: &Vec<App> = if self.query_lc.starts_with(&self.prev_query_lc) {
417419
self.prev_query_lc = self.query_lc.to_owned();
418-
&self.results.clone()
420+
&self.results
419421
} else {
420422
&self.options
421423
};
422424

423-
self.results = vec![];
424-
self.results.extend(
425-
&mut filter_vec
426-
.iter()
427-
.filter(|x| x.name_lc == self.query_lc)
428-
.map(|x| x.to_owned()),
429-
);
430-
431-
self.results.extend(
432-
&mut filter_vec
433-
.iter()
434-
.filter(|x| x.name_lc != self.query_lc && x.name_lc.starts_with(&self.query_lc))
435-
.map(|x| x.to_owned()),
436-
);
425+
let query = self.query_lc.clone();
426+
427+
let mut exact: Vec<App> = filter_vec
428+
.par_iter()
429+
.filter(|x| x.name_lc == query)
430+
.cloned()
431+
.collect();
432+
433+
let mut prefix: Vec<App> = filter_vec
434+
.par_iter()
435+
.filter(|x| x.name_lc != query && x.name_lc.starts_with(&query))
436+
.cloned()
437+
.collect();
438+
439+
exact.append(&mut prefix);
440+
self.results = exact;
437441
}
438442

439443
pub fn capture_frontmost(&mut self) {

src/utils.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use global_hotkey::hotkey::Code;
99
use iced::widget::image::Handle;
1010
use icns::IconFamily;
1111
use image::RgbaImage;
12+
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1213

1314
use crate::app::App;
1415

@@ -45,18 +46,21 @@ pub(crate) fn handle_from_icns(path: &Path) -> Option<Handle> {
4546
}
4647

4748
pub(crate) fn get_installed_apps(dir: impl AsRef<Path>, store_icons: bool) -> Vec<App> {
48-
fs::read_dir(dir)
49+
let entries: Vec<_> = fs::read_dir(dir.as_ref())
4950
.unwrap_or_else(|x| {
5051
log_error_and_exit(&x.to_string());
5152
exit(-1)
5253
})
5354
.filter_map(|x| x.ok())
55+
.collect();
56+
57+
entries
58+
.into_par_iter()
5459
.filter_map(|x| {
5560
let file_type = x.file_type().unwrap_or_else(|e| {
5661
log_error(&e.to_string());
5762
exit(-1)
5863
});
59-
6064
if !file_type.is_dir() {
6165
return None;
6266
}
@@ -66,12 +70,12 @@ pub(crate) fn get_installed_apps(dir: impl AsRef<Path>, store_icons: bool) -> Ve
6670
log_error(e.to_str().unwrap_or(""));
6771
exit(-1)
6872
});
69-
7073
if !file_name.ends_with(".app") {
7174
return None;
7275
}
7376

74-
let path_str = x.path().to_str().map(|x| x.to_string()).unwrap_or_else(|| {
77+
let path = x.path();
78+
let path_str = path.to_str().map(|x| x.to_string()).unwrap_or_else(|| {
7579
log_error("Unable to get file_name");
7680
exit(-1)
7781
});
@@ -146,8 +150,8 @@ pub(crate) fn get_installed_apps(dir: impl AsRef<Path>, store_icons: bool) -> Ve
146150
} else {
147151
None
148152
};
149-
let name = file_name.strip_suffix(".app").unwrap().to_string();
150153

154+
let name = file_name.strip_suffix(".app").unwrap().to_string();
151155
Some(App {
152156
open_command: format!("open {}", path_str),
153157
icons,

0 commit comments

Comments
 (0)