-
Notifications
You must be signed in to change notification settings - Fork 347
Improve codeowners lookup performance #12058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daniel-mohedano
wants to merge
1
commit into
master
Choose a base branch
from
daniel.mohedano/improve-codeowners-lookup-performance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...t/agent-ci-visibility/src/main/java/datadog/trace/civisibility/codeowners/EntryIndex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package datadog.trace.civisibility.codeowners; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Resolves entries by rule priority, switching large rule sets from linear scans to fixed-prefix | ||
| * buckets while retaining a fallback for patterns that cannot be indexed. | ||
| */ | ||
| final class EntryIndex { | ||
|
|
||
| private static final int MIN_INDEX_SIZE = 512; | ||
|
|
||
| private List<IndexedEntry> entries = new ArrayList<>(); | ||
| private Map<String, List<IndexedEntry>> entriesByKey; | ||
| private List<IndexedEntry> unindexedEntries; | ||
| private int indexableEntryCount; | ||
|
|
||
| void add(Entry entry, int order) { | ||
| IndexedEntry indexedEntry = new IndexedEntry(entry, order); | ||
| if (entriesByKey != null) { | ||
| index(indexedEntry); | ||
| } else { | ||
| entries.add(indexedEntry); | ||
| if (entry.getIndexKey() != null) { | ||
| indexableEntryCount++; | ||
| } | ||
| if (entries.size() >= MIN_INDEX_SIZE && indexableEntryCount * 2 >= entries.size()) { | ||
| entriesByKey = new HashMap<>(); | ||
| unindexedEntries = new ArrayList<>(); | ||
| for (IndexedEntry existingEntry : entries) { | ||
| index(existingEntry); | ||
| } | ||
| entries = Collections.emptyList(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| Entry find(String path) { | ||
| IndexedEntry entry = findIndexedEntry(path); | ||
| return entry != null ? entry.entry : null; | ||
| } | ||
|
|
||
| private @Nullable IndexedEntry findIndexedEntry(String path) { | ||
| if (entriesByKey == null) { | ||
| return findFirstMatch(entries, path); | ||
| } | ||
| IndexedEntry unindexedMatch = findFirstMatch(unindexedEntries, path); | ||
|
|
||
| int firstSeparator = path.indexOf('/'); | ||
| if (firstSeparator < 0) { | ||
| return unindexedMatch; | ||
| } | ||
| int secondSeparator = path.indexOf('/', firstSeparator + 1); | ||
| int keyEnd = secondSeparator >= 0 ? secondSeparator : path.length(); | ||
| List<IndexedEntry> indexedEntries = entriesByKey.get(path.substring(0, keyEnd)); | ||
| IndexedEntry indexedMatch = findFirstMatch(indexedEntries, path); | ||
|
|
||
| if (unindexedMatch == null) { | ||
| return indexedMatch; | ||
| } | ||
| if (indexedMatch == null) { | ||
| return unindexedMatch; | ||
| } | ||
| return indexedMatch.order > unindexedMatch.order ? indexedMatch : unindexedMatch; | ||
| } | ||
|
|
||
| private void index(IndexedEntry indexedEntry) { | ||
| String indexKey = indexedEntry.entry.getIndexKey(); | ||
| if (indexKey != null) { | ||
| entriesByKey.computeIfAbsent(indexKey, key -> new ArrayList<>()).add(indexedEntry); | ||
| } else { | ||
| unindexedEntries.add(indexedEntry); | ||
| } | ||
| } | ||
|
|
||
| private static @Nullable IndexedEntry findFirstMatch( | ||
| @Nullable List<IndexedEntry> entries, String path) { | ||
| if (entries != null) { | ||
| for (int i = entries.size() - 1; i >= 0; i--) { | ||
| IndexedEntry entry = entries.get(i); | ||
| if (entry.entry.getMatcher().consume(path, 0) >= 0) { | ||
| return entry; | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static final class IndexedEntry { | ||
|
|
||
| private final Entry entry; | ||
| private final int order; | ||
|
|
||
| private IndexedEntry(Entry entry, int order) { | ||
| this.entry = entry; | ||
| this.order = order; | ||
| } | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...gent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/codeowners/Section.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package datadog.trace.civisibility.codeowners; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Groups ownership and exclusion rules for a CODEOWNERS section while preserving rule order. */ | ||
| final class Section { | ||
|
|
||
| private final EntryIndex entries = new EntryIndex(); | ||
| private final EntryIndex exclusions = new EntryIndex(); | ||
| private int entryOrder; | ||
|
|
||
| void add(Entry entry) { | ||
| if (entry.isExclusion()) { | ||
| exclusions.add(entry, entryOrder++); | ||
| } else { | ||
| entries.add(entry, entryOrder++); | ||
| } | ||
| } | ||
|
|
||
| boolean isExcluded(String path) { | ||
| return exclusions.find(path) != null; | ||
| } | ||
|
|
||
| @Nullable | ||
| Entry findMatchingEntry(String path) { | ||
| return entries.find(path); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In large sections where the index activates, a two-segment trailing-slash rule such as
/services/api/is bucketed underservices/api, but the existing matcher still treats that rule as a plain prefix because no end-of-segment matcher is appended for trailing slashes. This makes behavior depend on file size: with 511 prior indexable entriesservices/api-v2/Test.javais matched by the linear scan, while with 512 entries the indexed lookup only checks theservices/api-v2bucket and returns no owner; exclusions have the same issue. Either keep these trailing-slash patterns in the fallback until the matcher semantics are tightened, or make the matcher and index agree.Useful? React with 👍 / 👎.