Repository navigation
Commit 14fd740
committed
perf(submodule): query the repository once when listing submodules
`Submodule.iter_items`, which backs `Repo.submodules`, `Submodule.list_items`,
`Submodule.children` and the walk in `RootModule.update`, issued two
loop-invariant repository queries once per section of `.gitmodules`:
- A section whose `path` is not an entry of the parent commit's tree fell back
to `repo.index` and `index.entry(path)`. `IndexFile.entry` iterates
`iter_entries()`, which reads the whole index again, so each such section ran
another `git ls-files --stage` (CLI backend) over every index entry.
- Every section that did resolve compared its parent commit against
`repo.commit()`, resolving `HEAD` again. On the CLI backend that is two
`git symbolic-ref` calls plus a `git show-ref` per section.
Both answers are the same for every section. `.gitmodules` is untrusted
repository content under the project's threat model, so the number of sections
is chosen by whoever authored the repository, and the index size is too. For
`S` sections and an index of `E` entries, listing therefore cost `O(S*E)` in
time and `O(S)` repository operations. The index is now read at most once and
`HEAD` resolved at most once, which makes it `O(S+E)` in time and `O(1)`
repository operations beyond the per-section bookkeeping.
Measured with Git 2.54.0, Python 3.12 and the CLI backend:
| `Repo.submodules` on | before | after |
| --- | --- | --- |
| a superproject whose `HEAD` tree holds 400 submodule gitlinks, all 400 declared | 51.62 s, 1214 Git processes | 0.46 s, 17 Git processes |
| 400 declared sections whose paths are absent from `HEAD`, index of 3001 entries | 20.85 s, 419 Git processes | 0.43 s, 15 Git processes |
Per-section growth on the 12-section fixture added here, counted through
`git._backend.statistics()` with all paths held in the tree: 43 operations
before and 10 after on the CLI backend, 44 before and 11 after on Gix, against
10 and 11 respectively for a single section. That is three operations per
additional submodule before the change and none after; with the paths staged
in the index only, where both queries repeat, it is four.
Results are unchanged. `IndexFile.entries` is keyed by path and stage, so
`entries.get((path, 0))` selects the same stage 0 entry that
`IndexFile.entry(path)` returned, and a missing key takes the same
`continue` the previous `KeyError` did. `HEAD` is still resolved only once a
section resolves, so a repository whose `.gitmodules` lists nothing that is
tracked does not resolve it at all.
Adds `tests/fixtures/submodule-many.sh`, a repository with twelve submodule
gitlinks and matching sections, and
`test_listing_submodules_queries_the_repository_once` in
`test/test_submodule.py`. It lists the submodules for twelve sections and for
one and asserts the recorded operation counts are equal, parametrized over
paths held in the tree, paths staged in the index only, and absent paths. All
three cases fail before the change (43 against 10, 55 against 11 and 19
against 8 on the CLI backend; 44 against 11, 56 against 12 and 20 against 9 on
Gix) and pass after on both backends.
Validated on macOS with Git 2.54.0, Python 3.12 and GixPython 0.4.0:
`test/test_submodule.py` passes with `GIT_PYTHON_BACKEND=cli` (482 passed, 6
skipped, 1 xfailed) and, together with `test/test_submodule_no_fetch.py`, with
`gix` (553 passed, 5 skipped, 1 xfailed), both `-m "not net"`; so do
`test/test_fixture_health.py`, `test/test_cli_process_counts.py`,
`test/test_docs.py` and the `Repo.submodules` cases of `test/test_repo.py`.
`ruff check`, `ruff format --check`, `mypy` and `shellcheck` are clean, and
`basedpyright --warnings` reports only a pre-existing unresolved `ddt` import
in `test/test_git.py`.
Agent-Model: claude-opus-51 parent 1c9ef7d commit 14fd740
3 files changed
Lines changed: 61 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
2021 | 2021 | | |
2022 | 2022 | | |
2023 | 2023 | | |
| 2024 | + | |
| 2025 | + | |
| 2026 | + | |
| 2027 | + | |
2024 | 2028 | | |
2025 | 2029 | | |
2026 | 2030 | | |
| |||
2032 | 2036 | | |
2033 | 2037 | | |
2034 | 2038 | | |
2035 | | - | |
2036 | 2039 | | |
2037 | 2040 | | |
2038 | 2041 | | |
2039 | 2042 | | |
2040 | | - | |
2041 | | - | |
2042 | | - | |
2043 | | - | |
2044 | | - | |
| 2043 | + | |
| 2044 | + | |
| 2045 | + | |
| 2046 | + | |
| 2047 | + | |
2045 | 2048 | | |
2046 | 2049 | | |
2047 | 2050 | | |
2048 | | - | |
| 2051 | + | |
2049 | 2052 | | |
2050 | 2053 | | |
2051 | 2054 | | |
| |||
2054 | 2057 | | |
2055 | 2058 | | |
2056 | 2059 | | |
2057 | | - | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
2058 | 2063 | | |
2059 | 2064 | | |
2060 | 2065 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
84 | 114 | | |
85 | 115 | | |
86 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
0 commit comments