Skip to content

Commit

Permalink
Add multithreading
Browse files Browse the repository at this point in the history
Adds multithreading to the directory walk and for checking workspace
matches
  • Loading branch information
vinnymeller committed May 31, 2024
1 parent 9792d20 commit 8efdfcf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ pub fn handle_workspace_selection(args: &Arguments) -> Result<()> {
let config = config.clone();
std::thread::spawn(move || {
for dir in &config.search_paths {
find_workspaces_in_dir(dir, &config, injector.clone())
let _ = find_workspaces_in_dir(dir, &config, injector.clone());
}
});
match picker.get_selection()? {
Expand Down
71 changes: 37 additions & 34 deletions src/matches.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
use crate::config::TwmGlobal;
use crate::workspace::path_meets_workspace_conditions;
use anyhow::Result;

use jwalk::{
rayon::iter::{ParallelBridge, ParallelIterator},
WalkDir,
};
use nucleo::Injector;
use jwalk::{DirEntry, WalkDir};

pub fn find_workspaces_in_dir(dir: &str, config: &TwmGlobal, injector: Injector<String>) {
let is_excluded = |entry: &DirEntry<((), ())>| -> bool {
match entry
.path()
.components()
.last()
.expect("Surely there is always a last component?")
.as_os_str()
.to_str()
{
Some(s) => config.exclude_path_components.iter().any(|e| s == e),
None => true,
}
};

let walker = WalkDir::new(dir)
pub fn find_workspaces_in_dir(
dir: &str,
config: &TwmGlobal,
injector: Injector<String>,
) -> Result<()> {
WalkDir::new(dir)
.max_depth(config.max_search_depth)
.skip_hidden(false)
.into_iter()
.par_bridge()
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_dir() && !is_excluded(e));

for entry in walker {
for workspace_definition in &config.workspace_definitions {
if path_meets_workspace_conditions(&entry.path(), &workspace_definition.conditions) {
// just skip the path if it's not valid utf-8 since we can't use it
// skip here instead of checking earlier because i don't expect people having a bunch of non-utf8 paths to be common, so defer the check only if we have a match in the first place
if let Some(utf8_path) = entry.path().to_str() {
// previously we also stored which workspace type we matched on, but i decided to change it because we only ever need to know the workspace type for the workspace we're opening anyways
// having to re-lookup the workspace type on user selection is surely better than the hashmap we were using before, but better would probably be to just keep track of which WorkspaceDefinition matched here
// main reason I haven't yet is because I'm not entirely sure how to make that work nicely with the fuzzy finders
injector.push(utf8_path.to_string(), |_, dst| {
dst[0] = utf8_path.to_string().into()
});
.filter(|e| {
e.file_type().is_dir()
// this can definitely be improved in the future
&& !e.path().components().any(|c| match c.as_os_str().to_str() {
Some(s) => config.exclude_path_components.iter().any(|e| s == e),
None => true,
})
})
.for_each(|entry| {
for workspace_definition in &config.workspace_definitions {
if path_meets_workspace_conditions(&entry.path(), &workspace_definition.conditions)
{
// just skip the path if it's not valid utf-8 since we can't use it
// skip here instead of checking earlier because i don't expect people having a bunch of non-utf8 paths to be common, so defer the check only if we have a match in the first place
if let Some(utf8_path) = entry.path().to_str() {
// previously we also stored which workspace type we matched on, but i decided to change it because we only ever need to know the workspace type for the workspace we're opening anyways
// having to re-lookup the workspace type on user selection is surely better than the hashmap we were using before, but better would probably be to just keep track of which WorkspaceDefinition matched here
// main reason I haven't yet is because I'm not entirely sure how to make that work nicely with the fuzzy finders
injector.push(utf8_path.to_string(), |_, dst| {
dst[0] = utf8_path.to_string().into()
});
}
break;
}
break;
}
}
}
});
Ok(())
}

0 comments on commit 8efdfcf

Please sign in to comment.