Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: merge multiple file operations into one to greatly speed up updates in large directories #1745

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 44 additions & 64 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ anyhow = "1.0.89"
arc-swap = "1.7.1"
base64 = "0.22.1"
bitflags = "2.6.0"
clap = { version = "4.5.18", features = [ "derive" ] }
clap = { version = "4.5.19", features = [ "derive" ] }
crossterm = { version = "0.28.1", features = [ "event-stream" ] }
dirs = "5.0.1"
futures = "0.3.30"
futures = "0.3.31"
globset = "0.4.15"
libc = "0.2.159"
md-5 = "0.10.6"
Expand Down
2 changes: 1 addition & 1 deletion yazi-boot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde = { workspace = true }

[build-dependencies]
clap = { workspace = true }
clap_complete = "4.5.29"
clap_complete = "4.5.32"
clap_complete_fig = "4.5.2"
clap_complete_nushell = "4.5.3"
vergen-gitcl = { version = "1.0.1", features = [ "build" ] }
2 changes: 1 addition & 1 deletion yazi-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ toml_edit = "0.22.22"
# External build dependencies
anyhow = { workspace = true }
clap = { workspace = true }
clap_complete = "4.5.29"
clap_complete = "4.5.32"
clap_complete_fig = "4.5.2"
clap_complete_nushell = "4.5.3"
serde_json = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion yazi-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ arc-swap = { workspace = true }
bitflags = { workspace = true }
crossterm = { workspace = true }
globset = { workspace = true }
indexmap = { version = "2.5.0", features = [ "serde" ] }
indexmap = { version = "2.6.0", features = [ "serde" ] }
ratatui = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }
Expand Down
11 changes: 5 additions & 6 deletions yazi-core/src/manager/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ impl Watcher {
let mut cached: HashMap<_, _> = HashMap::new();

let _permit = WATCHER.acquire().await.unwrap();
let mut ops = Vec::with_capacity(urls.len());
let mut reload = Vec::with_capacity(urls.len());

for u in urls {
let Some((parent, urn)) = u.pair() else { continue };
let Ok(file) = File::from(u).await else {
FilesOp::Deleting(parent, HashSet::from_iter([urn])).emit();
ops.push(FilesOp::Deleting(parent, HashSet::from_iter([urn])));
continue;
};

Expand All @@ -136,19 +137,17 @@ impl Watcher {
|| realname_unchecked(u, &mut cached).await.is_ok_and(|s| urn.as_urn() == s);

if !eq {
FilesOp::Deleting(parent, HashSet::from_iter([urn])).emit();
ops.push(FilesOp::Deleting(parent, HashSet::from_iter([urn])));
continue;
}

if !file.is_dir() {
reload.push(file.clone());
}
FilesOp::Upserting(parent, HashMap::from_iter([(urn, file)])).emit();
ops.push(FilesOp::Upserting(parent, HashMap::from_iter([(urn, file)])));
}

if reload.is_empty() {
continue;
}
FilesOp::mutate(ops);
if let Err(e) = isolate::fetch("mime", reload).await {
error!("Fetch `mime` failed in watcher: {e}");
}
Expand Down
3 changes: 3 additions & 0 deletions yazi-plugin/src/isolate/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use super::slim_lua;
use crate::{bindings::Cast, elements::Rect, file::File, loader::LOADER};

pub async fn fetch(name: &str, files: Vec<yazi_shared::fs::File>) -> mlua::Result<u8> {
if files.is_empty() {
return Ok(1);
}
LOADER.ensure(name).await.into_lua_err()?;

let name = name.to_owned();
Expand Down
22 changes: 22 additions & 0 deletions yazi-shared/src/fs/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ impl FilesOp {
}
}

pub fn mutate(ops: Vec<Self>) {
let mut parents: HashMap<_, (HashMap<_, _>, HashSet<_>)> = Default::default();
for op in ops {
match op {
Self::Upserting(p, map) => parents.entry(p).or_default().0.extend(map),
Self::Deleting(p, urns) => parents.entry(p).or_default().1.extend(urns),
_ => unreachable!(),
}
}
for (p, (u, d)) in parents {
match (u.is_empty(), d.is_empty()) {
(true, true) => unreachable!(),
(true, false) => Self::Deleting(p, d).emit(),
(false, true) => Self::Upserting(p, u).emit(),
(false, false) => {
Self::Deleting(p.clone(), d).emit();
Self::Upserting(p, u).emit();
}
}
}
}

pub fn rebase(&self, new: &Url) -> Self {
macro_rules! files {
($files:expr) => {{ $files.iter().map(|f| f.rebase(new)).collect() }};
Expand Down