Skip to content

Commit 6270ac7

Browse files
committed
memoizing codeowners
1 parent c1478dd commit 6270ac7

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

src/cache/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct GlobalCache {
1717
file_owner_cache: Option<Box<Mutex<HashMap<PathBuf, FileOwnerCacheEntry>>>>,
1818
}
1919

20-
const DEFAULT_CACHE_CAPACITY: usize = 10000;
20+
const DEFAULT_CACHE_CAPACITY: usize = 50000;
2121

2222
impl Caching for GlobalCache {
2323
fn get_file_owner(&self, path: &Path) -> Result<Option<FileOwnerCacheEntry>, Error> {

src/ownership/codeowners_file_parser.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,7 @@ impl Parser {
4444
return Ok(HashMap::new());
4545
}
4646

47-
let codeowners_entries: Vec<(String, String)> =
48-
build_codeowners_lines_in_priority(self.codeowners_file_path.to_string_lossy().into_owned())
49-
.iter()
50-
.map(|line| {
51-
line.split_once(' ')
52-
.map(|(glob, team_name)| (glob.to_string(), team_name.to_string()))
53-
.ok_or_else(|| IoError::new(std::io::ErrorKind::InvalidInput, "Invalid line"))
54-
})
55-
.collect::<Result<_, IoError>>()
56-
.map_err(|e| Box::new(e) as Box<dyn Error>)?;
47+
let codeowners_entries = parse_codeowners_entries(self.codeowners_file_path.to_string_lossy().into_owned());
5748

5849
let teams_by_name = teams_by_github_team_name(self.absolute_team_files_globs());
5950

@@ -111,7 +102,6 @@ fn teams_by_github_team_name(team_file_glob: Vec<String>) -> HashMap<String, Tea
111102
teams
112103
}
113104

114-
#[memoize]
115105
fn build_codeowners_lines_in_priority(codeowners_file_path: String) -> Vec<String> {
116106
let codeowners_file = match fs::read_to_string(codeowners_file_path) {
117107
Ok(codeowners_file) => codeowners_file,
@@ -124,6 +114,16 @@ fn build_codeowners_lines_in_priority(codeowners_file_path: String) -> Vec<Strin
124114
stripped_lines_by_priority(&codeowners_file)
125115
}
126116

117+
fn parse_codeowners_entries(codeowners_file_path: String) -> Vec<(String, String)> {
118+
build_codeowners_lines_in_priority(codeowners_file_path)
119+
.iter()
120+
.filter_map(|line| {
121+
line.split_once(' ')
122+
.map(|(glob, team_name)| (glob.to_string(), team_name.to_string()))
123+
})
124+
.collect()
125+
}
126+
127127
#[derive(Debug, Clone, PartialEq, Eq)]
128128
struct Section {
129129
heading: String,

src/runner/api.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,10 @@ pub fn teams_for_files_from_codeowners(
7373
}
7474

7575
pub fn team_for_file_from_codeowners(run_config: &RunConfig, file_path: &str) -> error_stack::Result<Option<Team>, Error> {
76-
let relative_file_path = crate::path_utils::relative_to(&run_config.project_root, Path::new(file_path));
77-
78-
let config = config_from_path(&run_config.config_path)?;
79-
let res = crate::ownership::codeowners_query::team_for_file_from_codeowners(
80-
&run_config.project_root,
81-
&run_config.codeowners_file_path,
82-
&config.team_file_glob,
83-
Path::new(relative_file_path),
84-
)
85-
.map_err(Error::Io)?;
86-
Ok(res)
76+
let result = teams_for_files_from_codeowners(run_config, &[file_path.to_string()])?;
77+
// Since we only passed one file, there should be exactly one result
78+
debug_assert_eq!(result.len(), 1);
79+
Ok(result.into_values().next().flatten())
8780
}
8881

8982
// Fast path that avoids creating a full Runner for single file queries

0 commit comments

Comments
 (0)