Skip to content

Commit

Permalink
Optimize deleting leafs in subtasks
Browse files Browse the repository at this point in the history
This finally beats the shitty implementation I posted here: tokio-rs/tokio#4172 (comment)

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX committed Oct 9, 2022
1 parent b351d76 commit a37a18c
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions fuc_engine/src/ops/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn run_deletion_scheduler<'a, F: IntoIterator<Item = &'a Path>>(
}
}

for dir in dirs.into_iter().rev() {
for dir in dirs.into_iter().rev().flatten() {
fs::remove_dir(&dir).map_err(|error| Error::Io {
error,
context: format!("Failed to delete directory: {dir:?}"),
Expand All @@ -85,8 +85,10 @@ async fn run_deletion_scheduler<'a, F: IntoIterator<Item = &'a Path>>(

fn delete_dir(
dir: PathBuf,
tasks: UnboundedSender<JoinHandle<Result<PathBuf, Error>>>,
) -> Result<PathBuf, Error> {
tasks: UnboundedSender<JoinHandle<Result<Option<PathBuf>, Error>>>,
) -> Result<Option<PathBuf>, Error> {
let mut has_children = false;

// TODO use getdents64 on linux
let files = fs::read_dir(&dir).map_err(|error| Error::Io {
error,
Expand All @@ -105,6 +107,7 @@ fn delete_dir(
})?
.is_dir();

has_children |= is_dir;
if is_dir {
tasks
.send(task::spawn_blocking({
Expand All @@ -121,5 +124,13 @@ fn delete_dir(
}
}

Ok(dir)
if has_children {
Ok(Some(dir))
} else {
fs::remove_dir(&dir).map_err(|error| Error::Io {
error,
context: format!("Failed to delete directory: {dir:?}"),
})?;
Ok(None)
}
}

0 comments on commit a37a18c

Please sign in to comment.