From 40cdd6b1e58130be57831b6cfbd74420aa51c7d5 Mon Sep 17 00:00:00 2001 From: Austin Seipp Date: Wed, 1 Nov 2023 22:48:50 -0500 Subject: [PATCH] lib: small memory-optimization for `RepoPath` Signed-off-by: Austin Seipp Change-Id: I2007f35f645b890ab94467614c6094c0 --- lib/src/repo_path.rs | 26 +++++++++++++++++--------- lib/tests/test_merge_trees.rs | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/src/repo_path.rs b/lib/src/repo_path.rs index c755b1441f4..1a0280a809a 100644 --- a/lib/src/repo_path.rs +++ b/lib/src/repo_path.rs @@ -19,6 +19,7 @@ use std::path::{Component, Path, PathBuf}; use compact_str::CompactString; use itertools::Itertools; +use smallvec::{smallvec, SmallVec}; use thiserror::Error; use crate::file_util; @@ -55,9 +56,11 @@ impl From for RepoPathComponent { } } +pub type RepoPathComponents = SmallVec<[RepoPathComponent; 8]>; + #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct RepoPath { - components: Vec, + components: RepoPathComponents, } impl Debug for RepoPath { @@ -68,7 +71,9 @@ impl Debug for RepoPath { impl RepoPath { pub fn root() -> Self { - RepoPath { components: vec![] } + RepoPath { + components: smallvec![], + } } pub fn from_internal_string(value: &str) -> Self { @@ -86,7 +91,7 @@ impl RepoPath { } } - pub fn from_components(components: Vec) -> Self { + pub fn from_components(components: RepoPathComponents) -> Self { RepoPath { components } } @@ -161,7 +166,7 @@ impl RepoPath { None } else { Some(RepoPath { - components: self.components[0..self.components.len() - 1].to_vec(), + components: SmallVec::from(&self.components[0..self.components.len() - 1]), }) } } @@ -174,7 +179,7 @@ impl RepoPath { } } - pub fn components(&self) -> &Vec { + pub fn components(&self) -> &RepoPathComponents { &self.components } } @@ -284,17 +289,20 @@ mod tests { #[test] fn test_components() { - assert_eq!(RepoPath::root().components(), &vec![]); + assert_eq!( + RepoPath::root().components(), + &smallvec![] as &RepoPathComponents + ); assert_eq!( RepoPath::from_internal_string("dir").components(), - &vec![RepoPathComponent::from("dir")] + &smallvec![RepoPathComponent::from("dir")] as &RepoPathComponents ); assert_eq!( RepoPath::from_internal_string("dir/subdir").components(), - &vec![ + &smallvec![ RepoPathComponent::from("dir"), RepoPathComponent::from("subdir") - ] + ] as &RepoPathComponents ); } diff --git a/lib/tests/test_merge_trees.rs b/lib/tests/test_merge_trees.rs index 69e707d9a76..656d123e033 100644 --- a/lib/tests/test_merge_trees.rs +++ b/lib/tests/test_merge_trees.rs @@ -18,6 +18,7 @@ use jj_lib::repo::Repo; use jj_lib::repo_path::{RepoPath, RepoPathComponent}; use jj_lib::rewrite::rebase_commit; use jj_lib::tree::{merge_trees, Tree}; +use smallvec::smallvec; use testutils::{create_single_tree, create_tree, TestRepo}; #[test] @@ -504,7 +505,7 @@ fn test_simplify_conflict() { match further_rebased_tree.value(&component).unwrap() { TreeValue::Conflict(id) => { let conflict = store - .read_conflict(&RepoPath::from_components(vec![component.clone()]), id) + .read_conflict(&RepoPath::from_components(smallvec![component.clone()]), id) .unwrap(); assert_eq!( conflict.removes(),