Skip to content

Commit

Permalink
lib: small memory-optimization for RepoPath
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Seipp <[email protected]>
Change-Id: I2007f35f645b890ab94467614c6094c0
  • Loading branch information
thoughtpolice committed Nov 2, 2023
1 parent f338727 commit 40cdd6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
26 changes: 17 additions & 9 deletions lib/src/repo_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,9 +56,11 @@ impl From<String> for RepoPathComponent {
}
}

pub type RepoPathComponents = SmallVec<[RepoPathComponent; 8]>;

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RepoPath {
components: Vec<RepoPathComponent>,
components: RepoPathComponents,
}

impl Debug for RepoPath {
Expand All @@ -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 {
Expand All @@ -86,7 +91,7 @@ impl RepoPath {
}
}

pub fn from_components(components: Vec<RepoPathComponent>) -> Self {
pub fn from_components(components: RepoPathComponents) -> Self {
RepoPath { components }
}

Expand Down Expand Up @@ -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]),
})
}
}
Expand All @@ -174,7 +179,7 @@ impl RepoPath {
}
}

pub fn components(&self) -> &Vec<RepoPathComponent> {
pub fn components(&self) -> &RepoPathComponents {
&self.components
}
}
Expand Down Expand Up @@ -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
);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/tests/test_merge_trees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 40cdd6b

Please sign in to comment.