From 54561619b40f35040389c935f9e65c038c1cf7a1 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:39:44 +0100 Subject: [PATCH 1/3] refactor(du): avoid unnecessary `HashSet::contains` Replace the `contains` + `insert` pair used for inode tracking with a single `HashSet::insert` call. This reduces two hash table lookups to one while preserving existing behavior. --- src/uu/du/src/du.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 4cbd67ddc4..48dff14064 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -538,11 +538,11 @@ fn safe_du( } // Handle inodes - if let Some(inode) = this_stat.inode { - if seen_inodes.contains(&inode) && !options.count_links { - continue; - } - seen_inodes.insert(inode); + if let Some(inode) = this_stat.inode + && !seen_inodes.insert(inode) + && !options.count_links + { + continue; } // Process directories recursively @@ -702,14 +702,11 @@ fn du_regular( } } - if let Some(inode) = this_stat.inode { - // Check if the inode has been seen before and if we should skip it - if seen_inodes.contains(&inode) && !options.count_links { - // Skip further processing for this inode - continue; - } - // Mark this inode as seen - seen_inodes.insert(inode); + if let Some(inode) = this_stat.inode + && !seen_inodes.insert(inode) + && !options.count_links + { + continue; } if this_stat.metadata.is_dir() { @@ -1202,11 +1199,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let stat = Stat::new(&path, None, &traversal_options); if let Ok(stat) = stat.as_ref() && let Some(inode) = stat.inode + && !traversal_options.count_links + && !seen_inodes.insert(inode) { - if !traversal_options.count_links && seen_inodes.contains(&inode) { - continue 'loop_file; - } - seen_inodes.insert(inode); + continue 'loop_file; } if use_safe_traversal { From 34537090f3b22c2de0eaf45377e5797f97e8be86 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 30 Sep 2026 21:49:52 +0100 Subject: [PATCH 2/3] refactor(hostname): avoid unnecessary `HashSet::contains` --- src/uu/hostname/src/hostname.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uu/hostname/src/hostname.rs b/src/uu/hostname/src/hostname.rs index da1adeb58a..1c06c94433 100644 --- a/src/uu/hostname/src/hostname.rs +++ b/src/uu/hostname/src/hostname.rs @@ -101,7 +101,7 @@ fn display_hostname(matches: &ArgMatches) -> UResult<()> { let mut output = String::new(); for addr in addresses { // XXX: not sure why this is necessary... - if !hashset.contains(&addr) { + if hashset.insert(addr) { let mut ip = addr.to_string(); if ip.ends_with(":1") { let len = ip.len(); @@ -109,7 +109,6 @@ fn display_hostname(matches: &ArgMatches) -> UResult<()> { } output.push_str(&ip); output.push(' '); - hashset.insert(addr); } } let len = output.len(); From cf8fd9a1d13d7ade5ff6c0027d2a8246ad98f2c8 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 30 Sep 2026 21:49:58 +0100 Subject: [PATCH 3/3] refactor(cp): avoid unnecessary `HashSet::contains` --- src/uu/cp/src/cp.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 046ac6a873..639babccb9 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -1482,7 +1482,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult for source in sources { let normalized_source = normalize_path(source); - if options.backup == BackupMode::None && seen_sources.contains(&normalized_source) { + if options.backup == BackupMode::None && !seen_sources.insert(normalized_source) { let file_type = if source.symlink_metadata()?.file_type().is_dir() { "directory" } else { @@ -1538,7 +1538,6 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult copied_destinations.insert(dest.clone()); } } - seen_sources.insert(normalized_source); } if let Some(pb) = progress_bar {