Skip to content

Commit 0e7ac4a

Browse files
author
Stephan Dilly
authored
improve filetree::new performance (gitui-org#760)
we used a BTreeMap where ordering does not matter and HashMap performs much better
1 parent a481760 commit 0e7ac4a

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## Added
1111
- honor `config.showUntrackedFiles` improving speed with a lot of untracked items ([#752](https://github.com/extrawurst/gitui/issues/752))
12+
- improve performance when opening filetree-tab ([#756](https://github.com/extrawurst/gitui/issues/756))
1213

1314
## Fixed
1415
- wrong file with same name shown in file tree ([#748](https://github.com/extrawurst/gitui/issues/748))

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

22
.PHONY: debug build-release release-linux-musl test clippy clippy-pedantic install install-debug
33

4+
ARGS=-l
5+
# ARGS=-l -d <some_path>
6+
47
profile:
5-
cargo run --features=timing,pprof -- -l
8+
cargo run --features=timing,pprof -- ${ARGS}
69

710
debug:
8-
RUST_BACKTRACE=true cargo run --features=timing -- -l
11+
RUST_BACKTRACE=true cargo run --features=timing -- ${ARGS}
912

1013
build-release:
1114
cargo build --release

filetreelist/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ categories = ["command-line-utilities"]
1212
keywords = ["gui","cli","terminal","ui","tui"]
1313

1414
[dependencies]
15+
scopetime = { path = "../scopetime", version = "0.1" }
1516
thiserror = "1.0"
1617

1718
[dev-dependencies]

filetreelist/src/filetreeitems.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
};
66
use crate::{error::Result, treeitems_iter::TreeItemsIterator};
77
use std::{
8-
collections::{BTreeMap, BTreeSet},
8+
collections::{BTreeSet, HashMap},
99
path::Path,
1010
usize,
1111
};
@@ -36,9 +36,12 @@ impl FileTreeItems {
3636
fn create_items<'a>(
3737
list: &'a [&str],
3838
collapsed: &BTreeSet<&String>,
39-
) -> Result<(Vec<FileTreeItem>, BTreeMap<&'a Path, usize>)> {
39+
) -> Result<(Vec<FileTreeItem>, HashMap<&'a Path, usize>)> {
40+
// scopetime::scope_time!("create_items");
41+
4042
let mut items = Vec::with_capacity(list.len());
41-
let mut paths_added: BTreeMap<&Path, usize> = BTreeMap::new();
43+
let mut paths_added: HashMap<&Path, usize> =
44+
HashMap::with_capacity(list.len());
4245

4346
for e in list {
4447
{
@@ -81,7 +84,7 @@ impl FileTreeItems {
8184
nodes: &mut Vec<FileTreeItem>,
8285
// helps to only add new nodes for paths that were not added before
8386
// we also count the number of children a node has for later folding
84-
paths_added: &mut BTreeMap<&'a Path, usize>,
87+
paths_added: &mut HashMap<&'a Path, usize>,
8588
collapsed: &BTreeSet<&String>,
8689
) -> Result<()> {
8790
let mut ancestors =
@@ -91,7 +94,7 @@ impl FileTreeItems {
9194
for c in &ancestors {
9295
if c.parent().is_some() && !paths_added.contains_key(c) {
9396
// add node and set count to have no children
94-
paths_added.entry(c).or_insert(0);
97+
paths_added.insert(c, 0);
9598

9699
// increase the number of children in the parent node count
97100
if let Some(parent) = c.parent() {
@@ -241,7 +244,7 @@ impl FileTreeItems {
241244

242245
fn fold_paths(
243246
items: &mut Vec<FileTreeItem>,
244-
paths: &BTreeMap<&Path, usize>,
247+
paths: &HashMap<&Path, usize>,
245248
) {
246249
let mut i = 0;
247250

@@ -335,7 +338,7 @@ mod tests {
335338
#[test]
336339
fn test_push_path() {
337340
let mut items = Vec::new();
338-
let mut paths: BTreeMap<&Path, usize> = BTreeMap::new();
341+
let mut paths: HashMap<&Path, usize> = HashMap::new();
339342

340343
FileTreeItems::push_dirs(
341344
Path::new("a/b/c"),
@@ -361,7 +364,7 @@ mod tests {
361364
#[test]
362365
fn test_push_path2() {
363366
let mut items = Vec::new();
364-
let mut paths: BTreeMap<&Path, usize> = BTreeMap::new();
367+
let mut paths: HashMap<&Path, usize> = HashMap::new();
365368

366369
FileTreeItems::push_dirs(
367370
Path::new("a/b/c"),

0 commit comments

Comments
 (0)