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

Use atomic instead of mutex in du function #13253

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
32 changes: 28 additions & 4 deletions crates/cargo-util/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use anyhow::{Context, Result};
use ignore::overrides::OverrideBuilder;
use ignore::{WalkBuilder, WalkState};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

#[cfg(target_has_atomic = "64")]
use std::sync::atomic::{AtomicU64, Ordering};

/// Determines the disk usage of all files in the given directory.
///
/// The given patterns are gitignore style patterns relative to the given
Expand Down Expand Up @@ -40,7 +42,16 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result<u64> {
.git_ignore(false)
.git_exclude(false);
let walker = builder.build_parallel();

// Not all targets support atomics, so a mutex is used as a fallback
// https://github.com/rust-lang/cargo/pull/12981
// https://doc.rust-lang.org/std/sync/atomic/index.html#portability
#[cfg(target_has_atomic = "64")]
let total = AtomicU64::new(0);

#[cfg(not(target_has_atomic = "64"))]
let total = Arc::new(Mutex::new(0_u64));

// A slot used to indicate there was an error while walking.
//
// It is possible that more than one error happens (such as in different
Expand All @@ -52,8 +63,17 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result<u64> {
Ok(entry) => match entry.metadata() {
Ok(meta) => {
if meta.is_file() {
// Note that fetch_add may wrap the u64.
total.fetch_add(meta.len(), Ordering::Relaxed);
#[cfg(target_has_atomic = "64")]
{
// Note that fetch_add may wrap the u64.
total.fetch_add(meta.len(), Ordering::Relaxed);
}

#[cfg(not(target_has_atomic = "64"))]
{
let mut lock = total.lock().unwrap();
*lock += meta.len();
}
}
}
Err(e) => {
Expand All @@ -74,5 +94,9 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result<u64> {
return Err(e);
}

Ok(total.load(Ordering::Relaxed))
#[cfg(target_has_atomic = "64")]
return Ok(total.load(Ordering::Relaxed));
Comment on lines +97 to +98
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed to use a return here so it would compile. Rust doesn't know that the two cfg blocks are mutually exclusive.


#[cfg(not(target_has_atomic = "64"))]
Ok(*total.lock().unwrap())
}
Loading