Skip to content

Commit 83b11fe

Browse files
committed
fix: Actually use the precompiled dictionaries which will optimize performance
- Use precompiled author-to-count dictionaries for contributions counting instead of re-fetching data for each contributor/team member - Optimize team member identification with user-manager relationship dictionaries - Improve performance by eliminating repeated API calls and loops This change addresses PR feedback by properly reusing already fetched data, significantly improving performance especially for repositories with many contributors and activities. Signed-off-by: Zack Koppert <zkoppert@github.com>
1 parent 92b1b37 commit 83b11fe

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

measure_innersource.py

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,34 @@ def main(): # pragma: no cover
119119
f"Original commit author: {original_commit_author}, \
120120
with manager: {original_commit_author_manager}"
121121
)
122+
# Create a dictionary mapping users to their managers for faster lookups
123+
user_to_manager = {}
124+
manager_to_reports = {}
125+
126+
for user, data in org_data.items():
127+
manager = data["manager"]
128+
user_to_manager[user] = manager
129+
130+
# Also create reverse mapping of manager to direct reports
131+
if manager not in manager_to_reports:
132+
manager_to_reports[manager] = []
133+
manager_to_reports[manager].append(user)
134+
122135
# Find all users that report up to the same manager as the original commit author
123136
team_members_that_own_the_repo.append(original_commit_author)
124137
team_members_that_own_the_repo.append(original_commit_author_manager)
125138

126-
for user, data in org_data.items():
127-
if data["manager"] == original_commit_author_manager:
128-
team_members_that_own_the_repo.append(user)
139+
# Add all users reporting to the same manager
140+
if original_commit_author_manager in manager_to_reports:
141+
team_members_that_own_the_repo.extend(
142+
manager_to_reports[original_commit_author_manager]
143+
)
129144

130-
# for each username in team_members_that_own_the_repo,
131-
# add everyone that has one of them listed as the manager
132-
for user, data in org_data.items():
145+
# Add everyone that has one of the team members listed as their manager
146+
for user, manager in user_to_manager.items():
133147
if (
134-
user not in team_members_that_own_the_repo
135-
and data["manager"] in team_members_that_own_the_repo
148+
manager in team_members_that_own_the_repo
149+
and user not in team_members_that_own_the_repo
136150
):
137151
team_members_that_own_the_repo.append(user)
138152

@@ -230,54 +244,51 @@ def main(): # pragma: no cover
230244

231245
print(f"Found and processed {total_issues} issues")
232246

233-
# Count contributions for each innersource contributor
247+
# Count contributions for each innersource contributor using precompiled dictionaries
234248
innersource_contribution_counts = {}
235249
print("Counting contributions for each innersource contributor...")
236250
for contributor in innersource_contributors:
237251
# Initialize counter for this contributor
238252
innersource_contribution_counts[contributor] = 0
239253

240-
# Count commits by this contributor
241-
for commit in commit_list:
242-
if (
243-
hasattr(commit.author, "login")
244-
and commit.author.login == contributor
245-
):
246-
innersource_contribution_counts[contributor] += 1
254+
# Add commit counts from the precompiled dictionary
255+
innersource_contribution_counts[contributor] += commit_author_counts.get(
256+
contributor, 0
257+
)
247258

248-
# Add PR and issue counts
249-
for pull in repo_data.pull_requests(state="all"):
250-
if pull.user.login == contributor:
251-
innersource_contribution_counts[contributor] += 1
259+
# Add PR counts from the precompiled dictionary
260+
innersource_contribution_counts[contributor] += pr_author_counts.get(
261+
contributor, 0
262+
)
252263

253-
for issue in repo_data.issues(state="all"):
254-
if hasattr(issue.user, "login") and issue.user.login == contributor:
255-
innersource_contribution_counts[contributor] += 1
264+
# Add issue counts from the precompiled dictionary
265+
innersource_contribution_counts[contributor] += issue_author_counts.get(
266+
contributor, 0
267+
)
256268

257269
print("Innersource contribution counts:")
258270
for contributor, count in innersource_contribution_counts.items():
259271
print(f" {contributor}: {count} contributions")
260272

261-
# count contributions for each user in team_members_that_own_the_repo
273+
# Count contributions for each team member using precompiled dictionaries
262274
team_member_contribution_counts = {}
263275
print("Counting contributions for each team member that owns the repo...")
264276
for member in team_members_that_own_the_repo:
265277
# Initialize counter for this team member
266278
team_member_contribution_counts[member] = 0
267279

268-
# Count commits by this team member
269-
for commit in commit_list:
270-
if hasattr(commit.author, "login") and commit.author.login == member:
271-
team_member_contribution_counts[member] += 1
280+
# Add commit counts from the precompiled dictionary
281+
team_member_contribution_counts[member] += commit_author_counts.get(
282+
member, 0
283+
)
272284

273-
# Add PR and issue counts
274-
for pull in repo_data.pull_requests(state="all"):
275-
if pull.user.login == member:
276-
team_member_contribution_counts[member] += 1
285+
# Add PR counts from the precompiled dictionary
286+
team_member_contribution_counts[member] += pr_author_counts.get(member, 0)
277287

278-
for issue in repo_data.issues(state="all"):
279-
if hasattr(issue.user, "login") and issue.user.login == member:
280-
team_member_contribution_counts[member] += 1
288+
# Add issue counts from the precompiled dictionary
289+
team_member_contribution_counts[member] += issue_author_counts.get(
290+
member, 0
291+
)
281292

282293
print("Team member contribution counts:")
283294
for member, count in team_member_contribution_counts.items():

0 commit comments

Comments
 (0)