Skip to content

Commit

Permalink
improve filetree::new performance (gitui-org#760)
Browse files Browse the repository at this point in the history
we used a BTreeMap where ordering does not matter and HashMap performs much better
  • Loading branch information
Stephan Dilly authored Jun 1, 2021
1 parent a481760 commit 0e7ac4a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

## Fixed
- wrong file with same name shown in file tree ([#748](https://github.com/extrawurst/gitui/issues/748))
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

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

ARGS=-l
# ARGS=-l -d <some_path>

profile:
cargo run --features=timing,pprof -- -l
cargo run --features=timing,pprof -- ${ARGS}

debug:
RUST_BACKTRACE=true cargo run --features=timing -- -l
RUST_BACKTRACE=true cargo run --features=timing -- ${ARGS}

build-release:
cargo build --release
Expand Down
1 change: 1 addition & 0 deletions filetreelist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ categories = ["command-line-utilities"]
keywords = ["gui","cli","terminal","ui","tui"]

[dependencies]
scopetime = { path = "../scopetime", version = "0.1" }
thiserror = "1.0"

[dev-dependencies]
Expand Down
19 changes: 11 additions & 8 deletions filetreelist/src/filetreeitems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use crate::{error::Result, treeitems_iter::TreeItemsIterator};
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeSet, HashMap},
path::Path,
usize,
};
Expand Down Expand Up @@ -36,9 +36,12 @@ impl FileTreeItems {
fn create_items<'a>(
list: &'a [&str],
collapsed: &BTreeSet<&String>,
) -> Result<(Vec<FileTreeItem>, BTreeMap<&'a Path, usize>)> {
) -> Result<(Vec<FileTreeItem>, HashMap<&'a Path, usize>)> {
// scopetime::scope_time!("create_items");

let mut items = Vec::with_capacity(list.len());
let mut paths_added: BTreeMap<&Path, usize> = BTreeMap::new();
let mut paths_added: HashMap<&Path, usize> =
HashMap::with_capacity(list.len());

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

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

fn fold_paths(
items: &mut Vec<FileTreeItem>,
paths: &BTreeMap<&Path, usize>,
paths: &HashMap<&Path, usize>,
) {
let mut i = 0;

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

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

FileTreeItems::push_dirs(
Path::new("a/b/c"),
Expand Down

0 comments on commit 0e7ac4a

Please sign in to comment.