Skip to content

Commit

Permalink
perf: increase performance of watch_files (#3407)
Browse files Browse the repository at this point in the history
Fixes #3401
  • Loading branch information
jdx authored Dec 8, 2024
1 parent d7fc2f1 commit cfda918
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ fslock = "0.2.1"
git2 = "<1"
glob = "0.3"
globset = "0.4"
globwalk = "0.9"
heck = "0.5"
home = "0.5"
humantime = "2"
Expand Down
24 changes: 11 additions & 13 deletions src/hook_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use base64::prelude::*;
use eyre::Result;
use flate2::write::{ZlibDecoder, ZlibEncoder};
use flate2::Compression;
use globwalk::GlobWalkerBuilder;
use itertools::Itertools;
use serde_derive::{Deserialize, Serialize};

Expand Down Expand Up @@ -46,7 +45,13 @@ pub fn should_exit_early(watch_files: impl IntoIterator<Item = WatchFilePattern>
hooks::schedule_hook(hooks::Hooks::Leave);
return false;
}
let watch_files = get_watch_files(watch_files);
let watch_files = match get_watch_files(watch_files) {
Ok(w) => w,
Err(e) => {
warn!("error getting watch files: {e}");
return false;
}
};
match &*env::__MISE_WATCH {
Some(watches) => {
if have_files_been_modified(watches, watch_files) {
Expand Down Expand Up @@ -132,7 +137,7 @@ pub fn build_watches(
watch_files: impl IntoIterator<Item = WatchFilePattern>,
) -> Result<HookEnvWatches> {
let mut max_modtime = UNIX_EPOCH;
for cf in get_watch_files(watch_files) {
for cf in get_watch_files(watch_files)? {
if let Ok(Ok(modified)) = cf.metadata().map(|m| m.modified()) {
max_modtime = std::cmp::max(modified, max_modtime);
}
Expand All @@ -149,28 +154,21 @@ pub fn build_watches(

pub fn get_watch_files(
watch_files: impl IntoIterator<Item = WatchFilePattern>,
) -> BTreeSet<PathBuf> {
) -> Result<BTreeSet<PathBuf>> {
let mut watches = BTreeSet::new();
if dirs::DATA.exists() {
watches.insert(dirs::DATA.to_path_buf());
}
for (root, patterns) in &watch_files.into_iter().chunk_by(|wfp| wfp.root.clone()) {
if let Some(root) = root {
let patterns = patterns.flat_map(|wfp| wfp.patterns).collect::<Vec<_>>();
let glob = GlobWalkerBuilder::from_patterns(root, &patterns)
.follow_links(true)
.build();
if let Ok(glob) = glob {
for entry in glob.flatten() {
watches.insert(entry.path().to_path_buf());
}
}
watches.extend(watch_files::glob(&root, &patterns)?);
} else {
watches.extend(patterns.flat_map(|wfp| wfp.patterns).map(PathBuf::from));
}
}

watches
Ok(watches)
}

/// gets a hash of all MISE_ environment variables
Expand Down
7 changes: 5 additions & 2 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::ui::tree::TreeItem;
use console::{truncate_str, Color};
use either::Either;
use eyre::{eyre, Result};
use globset::Glob;
use globset::GlobBuilder;
use itertools::Itertools;
use once_cell::sync::Lazy;
use petgraph::prelude::*;
Expand Down Expand Up @@ -464,7 +464,10 @@ where
{
fn get_matching(&self, pat: &str) -> Result<Vec<&T>> {
let normalized = pat.split(':').collect::<PathBuf>();
let matcher = Glob::new(&normalized.to_string_lossy())?.compile_matcher();
let matcher = GlobBuilder::new(&normalized.to_string_lossy())
.literal_separator(true)
.build()?
.compile_matcher();

Ok(self
.iter()
Expand Down
47 changes: 45 additions & 2 deletions src/watch_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::config::{Config, SETTINGS};
use crate::dirs;
use crate::toolset::Toolset;
use eyre::Result;
use globset::{Glob, GlobSetBuilder};
use globset::{GlobBuilder, GlobSetBuilder};
use itertools::Itertools;
use rayon::prelude::*;
use std::collections::BTreeSet;
use std::iter::once;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -95,7 +96,7 @@ fn has_matching_files<'a>(
) -> Result<Vec<&'a PathBuf>> {
let mut glob = GlobSetBuilder::new();
for pattern in &wf.patterns {
match Glob::new(pattern) {
match GlobBuilder::new(pattern).literal_separator(true).build() {
Ok(g) => {
glob.add(g);
}
Expand All @@ -116,3 +117,45 @@ fn has_matching_files<'a>(
})
.collect())
}

pub fn glob(root: &Path, patterns: &[String]) -> Result<Vec<PathBuf>> {
if patterns.is_empty() {
return Ok(vec![]);
}
let opts = glob::MatchOptions {
require_literal_separator: true,
..Default::default()
};
Ok(patterns
.par_iter()
.map(|pattern| root.join(pattern).to_string_lossy().to_string())
.filter_map(|pattern| glob::glob_with(&pattern, opts).ok())
.collect::<Vec<_>>()
.into_iter()
.flat_map(|paths| paths.filter_map(|p| p.ok()))
.collect())

// let mut overrides = ignore::overrides::OverrideBuilder::new(root);
// for pattern in patterns {
// overrides.add(&format!("./{pattern}"))?;
// }
// let files = Arc::new(Mutex::new(vec![]));
// ignore::WalkBuilder::new(root)
// .overrides(overrides.build()?)
// .standard_filters(false)
// .follow_links(true)
// .build_parallel()
// .run(|| {
// let files = files.clone();
// Box::new(move |entry| {
// if let Ok(entry) = entry {
// let mut files = files.lock().unwrap();
// files.push(entry.path().to_path_buf());
// }
// WalkState::Continue
// })
// });
//
// let files = files.lock().unwrap();
// Ok(files.to_vec())
}

0 comments on commit cfda918

Please sign in to comment.