Skip to content

Commit d92786e

Browse files
codexByron
authored andcommitted
perf(remote): index displayed ref names once per fetch
With `--prefetch`, Git writes destinations under `refs/prefetch/`. Those paths do not match the configured tracking refspec, so parsing each result used to scan and format the whole local ref inventory. Index the inventory by displayed name once and reuse the matching bucket for each result. Preserve every full path in each bucket so the decoder can still handle display-name collisions. The scripted prefetch regression detected repeated formatting before the change and passes on both backend selections. A local CLI prefetch of 4,001 unchanged branches took a median of 0.47 seconds over five runs, down from 4.77 seconds before indexing. Ruff and mypy passed. Agent-Model: unknown
1 parent 507448f commit d92786e

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎git/remote.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
NoReturn,
2323
Optional,
2424
Sequence,
25+
Set,
2526
TypeVar,
2627
Union,
2728
cast,
@@ -371,7 +372,13 @@ def _display_ref(path: str) -> str:
371372
return path
372373

373374
@classmethod
374-
def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs: Sequence[str] = ()) -> "FetchInfo":
375+
def _from_line(
376+
cls,
377+
repo: "Repo",
378+
line: str,
379+
refspecs: Sequence[str] = (),
380+
refs_by_display: Optional[Dict[str, Set[str]]] = None,
381+
) -> "FetchInfo":
375382
"""Decode Git's full verbose fetch output without reading FETCH_HEAD."""
376383
match = cls._re_fetch_result.match(line)
377384
if match is None:
@@ -425,10 +432,11 @@ def _from_line(cls, repo: "Repo", line: str, refspecs: Sequence[str] = (), refs:
425432
and cls._display_ref(local) == destination
426433
):
427434
candidates.add(local)
435+
matching_refs = refs_by_display.get(destination, ()) if refs_by_display is not None else ()
428436
if (control == "=" or old_commit is not None) and len(candidates) > 1:
429-
candidates.intersection_update(refs)
437+
candidates.intersection_update(matching_refs)
430438
if not candidates:
431-
candidates = {path for path in refs if cls._display_ref(path) == destination}
439+
candidates.update(matching_refs)
432440
# Git shortens refs/tags/refs/heads/main to refs/heads/main, so a
433441
# displayed name under refs/ is only a full name if nothing shortens to it.
434442
if not candidates and destination.startswith("refs/"):
@@ -839,14 +847,17 @@ def _get_fetch_info_from_stderr(
839847
continue
840848

841849
refs = self.repo.git._call_process_safe("for_each_ref", "--format=%(refname)").split("\n")
850+
refs_by_display: Dict[str, Set[str]] = {}
851+
for path in refs:
852+
refs_by_display.setdefault(FetchInfo._display_ref(path), set()).add(path)
842853
try:
843854
configured = self.config_reader.config.get_values(self._config_section_name(), "fetch")
844855
except (cp.NoSectionError, cp.NoOptionError):
845856
configured = []
846857
specs = [*refspecs, *configured]
847858
for line in fetch_info_lines:
848859
try:
849-
output.append(FetchInfo._from_line(self.repo, line, specs, refs))
860+
output.append(FetchInfo._from_line(self.repo, line, specs, refs_by_display))
850861
except ValueError as exc:
851862
_logger.warning("Cannot decode fetch result %r: %s", line, exc)
852863
# A source-only refspec reports both FETCH_HEAD and any opportunistic

‎test/test_remote_cli.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ def test_fetch_resolves_notes_with_a_tag_wildcard(remote_fixture):
8686
assert info.flags == flags
8787

8888

89+
@pytest.mark.parametrize("remote_fixture", [("prefetch",)], indirect=True)
90+
def test_prefetch_indexes_displayed_refs_once(remote_fixture):
91+
paths = {"refs/prefetch/remotes/origin/main", "refs/prefetch/remotes/origin/second"}
92+
with Repo(remote_fixture / "clone") as clone:
93+
with mock.patch.object(FetchInfo, "_display_ref", wraps=FetchInfo._display_ref) as display_ref:
94+
result = clone.remote().fetch(prefetch=True)
95+
assert {info.ref.path for info in result} == paths
96+
assert all(info.flags == FetchInfo.HEAD_UPTODATE for info in result)
97+
displayed = [args[0] for args, _ in display_ref.call_args_list if args[0] in paths]
98+
assert len(displayed) == len(set(displayed))
99+
100+
89101
@pytest.mark.parametrize("remote_fixture", [("quoted",)], indirect=True)
90102
def test_quoted_remote_and_submodule_names(remote_fixture):
91103
tmp_path = remote_fixture

‎tests/fixtures/remote-cli.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ case "$1" in
3434
git -C source update-ref refs/fixture/updated-notes refs/notes/commits
3535
git -C source update-ref refs/notes/commits "$initial_notes"
3636
;;
37+
prefetch)
38+
git -C source branch second
39+
git clone source clone
40+
git -C clone remote set-url origin ../source
41+
git -C clone fetch --prefetch
42+
;;
3743
quoted)
3844
git init --initial-branch=main parent
3945
;;

0 commit comments

Comments
 (0)