Skip to content

Commit 268f00b

Browse files
committed
grouped prefix optimization
1 parent 0497f17 commit 268f00b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

python/runfiles/runfiles.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
:::
2424
"""
2525
import collections.abc
26+
from collections import defaultdict
2627
import inspect
2728
import os
2829
import posixpath
@@ -53,6 +54,11 @@ def __init__(
5354
"""
5455
self._exact_mappings = exact_mappings
5556
self._prefixed_mappings = prefixed_mappings
57+
58+
# Group prefixed mappings by target_apparent for faster lookups
59+
self._grouped_prefixed_mappings = defaultdict(list)
60+
for (prefix_source, target_app), target_canonical in self._prefixed_mappings.items():
61+
self._grouped_prefixed_mappings[target_app].append((prefix_source, target_canonical))
5662

5763
@staticmethod
5864
def create_from_file(repo_mapping_path: Optional[str]) -> "_RepositoryMapping":
@@ -114,9 +120,10 @@ def __getitem__(self, key: Tuple[str, str]) -> str:
114120
return self._exact_mappings[key]
115121

116122
# Try prefixed mapping if no exact match found
117-
for (prefix_source, target_app), target_canonical in self._prefixed_mappings.items():
118-
if target_app == target_apparent and source_repo.startswith(prefix_source):
119-
return target_canonical
123+
if target_apparent in self._grouped_prefixed_mappings:
124+
for prefix_source, target_canonical in self._grouped_prefixed_mappings[target_apparent]:
125+
if source_repo.startswith(prefix_source):
126+
return target_canonical
120127

121128
# No mapping found
122129
raise KeyError(key)

0 commit comments

Comments
 (0)