Skip to content

Commit 5bd5e25

Browse files
committed
Cache code ownership lookups for files to improve performance
Add caching to avoid repeated RustCodeOwners calls for files with known ownership, reducing redundant lookups and speeding up team resolution.
1 parent bd79fcd commit 5bd5e25

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/code_ownership/private/team_finder.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,29 @@ def for_file(file_path, allow_raise: false)
3232

3333
sig { params(files: T::Array[String], allow_raise: T::Boolean).returns(T::Hash[String, T.nilable(CodeTeams::Team)]) }
3434
def teams_for_files(files, allow_raise: false)
35-
::RustCodeOwners.teams_for_files(files).each_with_object({}) do |path_team, hash|
35+
result = {}
36+
37+
# Collect cached results and identify non-cached files
38+
not_cached_files = []
39+
files.each do |file_path|
40+
if FilePathTeamCache.cached?(file_path)
41+
result[file_path] = FilePathTeamCache.get(file_path)
42+
else
43+
not_cached_files << file_path
44+
end
45+
end
46+
47+
return result if not_cached_files.empty?
48+
49+
# Process non-cached files
50+
::RustCodeOwners.teams_for_files(not_cached_files).each do |path_team|
3651
file_path, team = path_team
3752
found_team = team ? find_team!(team[:team_name], allow_raise: allow_raise) : nil
3853
FilePathTeamCache.set(file_path, found_team)
39-
hash[file_path] = found_team
54+
result[file_path] = found_team
4055
end
56+
57+
result
4158
end
4259

4360
sig { params(klass: T.nilable(T.any(T::Class[T.anything], Module))).returns(T.nilable(::CodeTeams::Team)) }

spec/lib/code_ownership_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@
184184
it 'returns the correct team' do
185185
expect(subject).to eq CodeTeams.find('Bar')
186186
end
187+
188+
context 'when ownership is cached' do
189+
it 'returns the correct team' do
190+
expect(subject).to eq CodeTeams.find('Bar')
191+
allow(RustCodeOwners).to receive(:teams_for_files)
192+
expect(CodeOwnership.for_file(file_path)).to eq CodeTeams.find('Bar')
193+
expect(RustCodeOwners).not_to have_received(:teams_for_files)
194+
end
195+
end
187196
end
188197

189198
context 'when ownership is found but team is not found' do

0 commit comments

Comments
 (0)