Skip to content

perf: double directory size calculation speed#2683

Merged
sxyazi merged 1 commit intomainfrom
pr-243362be
Apr 28, 2025
Merged

perf: double directory size calculation speed#2683
sxyazi merged 1 commit intomainfrom
pr-243362be

Conversation

@sxyazi
Copy link
Owner

@sxyazi sxyazi commented Apr 27, 2025

This PR greatly improves directory computation performance by dividing the task into chunks and reusing existing threads. Directory sorting and file trashing will be observably twice as fast.

By leveraging chunking, it now can produce real-time directory statistics reports. This means that for a large directory, you will be able to see the process of size changes in real-time, which is beneficial for the upcoming fs.calc_size() API.

The new implementation is still async cancelable, which is important for using fs.calc_size() in spotters, as it will allow canceling a still-incomplete directory computation when quickly switching between files, thereby avoiding wasted resources.

Benchmark

let p = std::path::Path::new("/tmp/yazi");

let now = std::time::Instant::now();
yazi_fs::calculate_size(p).await;
println!("Old: elapsed {:?}", now.elapsed());

let now = std::time::Instant::now();
yazi_fs::SizeCalculator::total(p).await?;
println!("New: elapsed {:?}", now.elapsed());

Run the benchmark script against a 10.16GB folder 5 times in release mode:

Old: elapsed 759.877791ms
New: elapsed 364.802583ms

Old: elapsed 734.5155ms
New: elapsed 360.103375ms

Old: elapsed 734.124167ms
New: elapsed 365.662458ms

Old: elapsed 733.27325ms
New: elapsed 365.694042ms

Old: elapsed 743.394875ms
New: elapsed 364.455083ms

@sxyazi sxyazi changed the title test perf: double directory size calculation speed Apr 28, 2025
@sxyazi sxyazi requested a review from Copilot April 28, 2025 11:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves directory size computation performance by switching from a synchronous, recursive approach to an async, chunked method using concurrency.

  • Replace calculate_size with SizeCalculator::total for asynchronous, cancelable directory size calculations.
  • Introduce a new calculator module with chunking logic.
  • Update logging messages and module imports accordingly.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
yazi-scheduler/src/prework/prework.rs Replaced calculate_size with SizeCalculator::total and updated imports.
yazi-scheduler/src/file/file.rs Updated file operations to use SizeCalculator::total and improved log formatting.
yazi-fs/src/lib.rs Added the calculator module to the module flattening.
yazi-fs/src/fns.rs Removed the deprecated calculate_size function.
yazi-fs/src/calculator.rs Introduced a new async chunked directory size calculation mechanism.
yazi-config/src/keymap/key.rs Modified string interpolation in the Display implementation for Key.
Comments suppressed due to low confidence (2)

yazi-scheduler/src/prework/prework.rs:76

  • Ensure that propagating errors using the '?' operator here is intended and that upstream error handling can appropriately manage errors from SizeCalculator::total.
let length = SizeCalculator::total(&task.target).await?;

yazi-scheduler/src/file/file.rs:321

  • Verify that the direct error propagation using '?' aligns with the desired behavior for file trash operations, and consider handling errors more explicitly if needed.
task.length = SizeCalculator::total(&task.target).await?;

@sxyazi sxyazi merged commit 6a9e7da into main Apr 28, 2025
6 checks passed
@sxyazi sxyazi deleted the pr-243362be branch April 28, 2025 11:51
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants