sorting.rs

 1use criterion::{Criterion, criterion_group, criterion_main};
 2use project::{Entry, EntryKind, GitEntry, ProjectEntryId};
 3use project_panel::par_sort_worktree_entries_with_mode;
 4use settings::ProjectPanelSortMode;
 5use std::sync::Arc;
 6use util::rel_path::RelPath;
 7
 8fn load_linux_repo_snapshot() -> Vec<GitEntry> {
 9    let file = std::fs::read_to_string(concat!(
10        env!("CARGO_MANIFEST_DIR"),
11        "/benches/linux_repo_snapshot.txt"
12    ))
13    .expect("Failed to read file");
14    file.lines()
15        .filter_map(|line| {
16            let kind = match line.chars().next() {
17                Some('f') => EntryKind::File,
18                Some('d') => EntryKind::Dir,
19                _ => return None,
20            };
21
22            let entry = Entry {
23                kind,
24                path: Arc::from(RelPath::unix(&(line.trim_end()[2..])).unwrap()),
25                id: ProjectEntryId::default(),
26                size: 0,
27                inode: 0,
28                mtime: None,
29                canonical_path: None,
30                is_ignored: false,
31                is_always_included: false,
32                is_external: false,
33                is_private: false,
34                is_hidden: false,
35                char_bag: Default::default(),
36                is_fifo: false,
37            };
38            Some(GitEntry {
39                entry,
40                git_summary: Default::default(),
41            })
42        })
43        .collect()
44}
45fn criterion_benchmark(c: &mut Criterion) {
46    let snapshot = load_linux_repo_snapshot();
47
48    c.bench_function("Sort linux worktree snapshot", |b| {
49        b.iter_batched(
50            || snapshot.clone(),
51            |mut snapshot| {
52                par_sort_worktree_entries_with_mode(
53                    &mut snapshot,
54                    ProjectPanelSortMode::DirectoriesFirst,
55                )
56            },
57            criterion::BatchSize::LargeInput,
58        );
59    });
60
61    c.bench_function("Sort linux worktree snapshot (Mixed)", |b| {
62        b.iter_batched(
63            || snapshot.clone(),
64            |mut snapshot| {
65                par_sort_worktree_entries_with_mode(&mut snapshot, ProjectPanelSortMode::Mixed)
66            },
67            criterion::BatchSize::LargeInput,
68        );
69    });
70
71    c.bench_function("Sort linux worktree snapshot (FilesFirst)", |b| {
72        b.iter_batched(
73            || snapshot.clone(),
74            |mut snapshot| {
75                par_sort_worktree_entries_with_mode(&mut snapshot, ProjectPanelSortMode::FilesFirst)
76            },
77            criterion::BatchSize::LargeInput,
78        );
79    });
80}
81
82criterion_group!(benches, criterion_benchmark);
83criterion_main!(benches);