From 1d35833e3b62b17ced3ffcbcb7ceb6483b91b743 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 15 Nov 2023 13:45:06 -0800 Subject: [PATCH] Switch from AtomicU64 to Mutex. Not all platforms support AtomicU64, so this swaps the usage with Mutex. The difference in performance should be imperceptible. --- Cargo.lock | 2 +- crates/cargo-util/Cargo.toml | 2 +- crates/cargo-util/src/du.rs | 9 +++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eddc2b586cbf..fcd36d8f71d4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -416,7 +416,7 @@ dependencies = [ [[package]] name = "cargo-util" -version = "0.2.8" +version = "0.2.9" dependencies = [ "anyhow", "core-foundation", diff --git a/crates/cargo-util/Cargo.toml b/crates/cargo-util/Cargo.toml index d4376d05bd0c..42d2c3733317 100644 --- a/crates/cargo-util/Cargo.toml +++ b/crates/cargo-util/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-util" -version = "0.2.8" +version = "0.2.9" rust-version.workspace = true edition.workspace = true license.workspace = true diff --git a/crates/cargo-util/src/du.rs b/crates/cargo-util/src/du.rs index a4f2cbe8c993..14634c47bc1b 100644 --- a/crates/cargo-util/src/du.rs +++ b/crates/cargo-util/src/du.rs @@ -4,7 +4,6 @@ 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}; /// Determines the disk usage of all files in the given directory. @@ -40,7 +39,7 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result { .git_ignore(false) .git_exclude(false); let walker = builder.build_parallel(); - let total = Arc::new(AtomicU64::new(0)); + let total = Arc::new(Mutex::new(0u64)); // A slot used to indicate there was an error while walking. // // It is possible that more than one error happens (such as in different @@ -52,7 +51,8 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result { Ok(entry) => match entry.metadata() { Ok(meta) => { if meta.is_file() { - total.fetch_add(meta.len(), Ordering::SeqCst); + let mut lock = total.lock().unwrap(); + *lock += meta.len(); } } Err(e) => { @@ -73,5 +73,6 @@ fn du_inner(path: &Path, patterns: &[&str]) -> Result { return Err(e); } - Ok(total.load(Ordering::SeqCst)) + let total = *total.lock().unwrap(); + Ok(total) }