diff options
| author | The8472 <git@infinite-source.de> | 2021-07-06 01:17:20 +0200 |
|---|---|---|
| committer | The8472 <git@infinite-source.de> | 2021-07-06 20:20:16 +0200 |
| commit | 5e877109b400a6f251dbefbbec94f2464c6f1b55 (patch) | |
| tree | 79e93339c7e3f8031abf7b4ad6e68af06ac72a3f /library/std/src/path | |
| parent | 5dcfec332ce9e003f100b4cf9dec895e5634edc3 (diff) | |
| download | rust-5e877109b400a6f251dbefbbec94f2464c6f1b55.tar.gz rust-5e877109b400a6f251dbefbbec94f2464c6f1b55.zip | |
optimize {Path,PathBuf,Components}::{cmp,partial_cmp} for shared prefixes
Diffstat (limited to 'library/std/src/path')
| -rw-r--r-- | library/std/src/path/tests.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/library/std/src/path/tests.rs b/library/std/src/path/tests.rs index 896d6c2a64c..6b7df78d3d7 100644 --- a/library/std/src/path/tests.rs +++ b/library/std/src/path/tests.rs @@ -1,7 +1,9 @@ use super::*; +use crate::collections::BTreeSet; use crate::rc::Rc; use crate::sync::Arc; +use core::hint::black_box; macro_rules! t( ($path:expr, iter: $iter:expr) => ( @@ -1392,3 +1394,52 @@ fn into_rc() { assert_eq!(&*rc2, path); assert_eq!(&*arc2, path); } + +#[bench] +fn bench_path_cmp_fast_path_buf_sort(b: &mut test::Bencher) { + let prefix = "my/home"; + let mut paths: Vec<_> = + (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {}.rs", num))).collect(); + + paths.sort(); + + b.iter(|| { + black_box(paths.as_mut_slice()).sort_unstable(); + }); +} + +#[bench] +fn bench_path_cmp_fast_path_long(b: &mut test::Bencher) { + let prefix = "/my/home/is/my/castle/and/my/castle/has/a/rusty/workbench/"; + let paths: Vec<_> = + (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {}.rs", num))).collect(); + + let mut set = BTreeSet::new(); + + paths.iter().for_each(|p| { + set.insert(p.as_path()); + }); + + b.iter(|| { + set.remove(paths[500].as_path()); + set.insert(paths[500].as_path()); + }); +} + +#[bench] +fn bench_path_cmp_fast_path_short(b: &mut test::Bencher) { + let prefix = "my/home"; + let paths: Vec<_> = + (0..1000).map(|num| PathBuf::from(prefix).join(format!("file {}.rs", num))).collect(); + + let mut set = BTreeSet::new(); + + paths.iter().for_each(|p| { + set.insert(p.as_path()); + }); + + b.iter(|| { + set.remove(paths[500].as_path()); + set.insert(paths[500].as_path()); + }); +} |
