Skip to content

Commit 82b4544

Browse files
committed
add benchmarks and tests for Hash and Eq impls on Path
The tests check for consistency between Ord, Eq and Hash
1 parent 07acdb4 commit 82b4544

File tree

1 file changed

+78
-2
lines changed

1 file changed

+78
-2
lines changed

library/std/src/path/tests.rs

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use super::*;
22

3-
use crate::collections::BTreeSet;
3+
use crate::collections::hash_map::DefaultHasher;
4+
use crate::collections::{BTreeSet, HashSet};
5+
use crate::hash::Hasher;
46
use crate::rc::Rc;
57
use crate::sync::Arc;
68
use core::hint::black_box;
@@ -1632,7 +1634,25 @@ fn into_rc() {
16321634
fn test_ord() {
16331635
macro_rules! ord(
16341636
($ord:ident, $left:expr, $right:expr) => ( {
1635-
assert_eq!(Path::new($left).cmp(&Path::new($right)), core::cmp::Ordering::$ord);
1637+
use core::cmp::Ordering;
1638+
1639+
let left = Path::new($left);
1640+
let right = Path::new($right);
1641+
assert_eq!(left.cmp(&right), Ordering::$ord);
1642+
if (core::cmp::Ordering::$ord == Ordering::Equal) {
1643+
assert_eq!(left, right);
1644+
1645+
let mut hasher = DefaultHasher::new();
1646+
left.hash(&mut hasher);
1647+
let left_hash = hasher.finish();
1648+
hasher = DefaultHasher::new();
1649+
right.hash(&mut hasher);
1650+
let right_hash = hasher.finish();
1651+
1652+
assert_eq!(left_hash, right_hash, "hashes for {:?} and {:?} must match", left, right);
1653+
} else {
1654+
assert_ne!(left, right);
1655+
}
16361656
});
16371657
);
16381658

@@ -1693,3 +1713,59 @@ fn bench_path_cmp_fast_path_short(b: &mut test::Bencher) {
16931713
set.insert(paths[500].as_path());
16941714
});
16951715
}
1716+
1717+
#[bench]
1718+
fn bench_path_hashset(b: &mut test::Bencher) {
1719+
let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
1720+
let paths: Vec<_> =
1721+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {}.rs", num))).collect();
1722+
1723+
let mut set = HashSet::new();
1724+
1725+
paths.iter().for_each(|p| {
1726+
set.insert(p.as_path());
1727+
});
1728+
1729+
b.iter(|| {
1730+
set.remove(paths[500].as_path());
1731+
set.insert(black_box(paths[500].as_path()))
1732+
});
1733+
}
1734+
1735+
#[bench]
1736+
fn bench_path_hashset_miss(b: &mut test::Bencher) {
1737+
let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/";
1738+
let paths: Vec<_> =
1739+
(0..1000).map(|num| PathBuf::from(prefix).join(format!("file {}.rs", num))).collect();
1740+
1741+
let mut set = HashSet::new();
1742+
1743+
paths.iter().for_each(|p| {
1744+
set.insert(p.as_path());
1745+
});
1746+
1747+
let probe = PathBuf::from(prefix).join("other");
1748+
1749+
b.iter(|| set.remove(black_box(probe.as_path())));
1750+
}
1751+
1752+
#[bench]
1753+
fn bench_hash_path_short(b: &mut test::Bencher) {
1754+
let mut hasher = DefaultHasher::new();
1755+
let path = Path::new("explorer.exe");
1756+
1757+
b.iter(|| black_box(path).hash(&mut hasher));
1758+
1759+
black_box(hasher.finish());
1760+
}
1761+
1762+
#[bench]
1763+
fn bench_hash_path_long(b: &mut test::Bencher) {
1764+
let mut hasher = DefaultHasher::new();
1765+
let path =
1766+
Path::new("/aaaaa/aaaaaa/./../aaaaaaaa/bbbbbbbbbbbbb/ccccccccccc/ddddddddd/eeeeeee.fff");
1767+
1768+
b.iter(|| black_box(path).hash(&mut hasher));
1769+
1770+
black_box(hasher.finish());
1771+
}

0 commit comments

Comments
 (0)