Repository navigation
perf(submodule): submodules ki listing ke dauraan repository ko sirf ek baar query karein - #2374
Closed
Keerthana-64 wants to merge 1 commit into
Closed
Keerthana-64 wants to merge 1 commit into
Keerthana-64 wants to merge 1 commit into
Conversation
`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-5
Member
|
I am going to close these PRs down while they aren't in English. This is because I have to see if they fulfill the contributing requirements. PS: Not the first time I am saying this. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Submodule.iter_items, jo Repo.submodules, Submodule.list_items, Submodule.children aur RootModule.update ke andar hone wale walk ko support karta hai, .gitmodules ke har section ke liye repository par do queries baar-baar execute karta tha.
Agar kisi section ka path, parent commit ke tree mein entry nahi hota tha, toh code repo.index aur index.entry() par fallback karta tha. IndexFile.entry poore index ko dobara iterate karta hai. Isliye har aise section ke liye ek aur git ls-files --stage command chalti thi, jo index ki har entry ko check karti thi.
Har woh section jo successfully resolve hota tha, apne parent commit ki comparison repo.commit() ke saath karta tha. Isse HEAD dobara resolve hota tha: CLI backend par har section ke liye do git symbolic-ref calls aur ek git show-ref call execute hoti thi.
Dono queries ke results har section ke liye same hote hain. Saath hi, project ke threat model ke according .gitmodules untrusted repository content hai. Isliye sections ki sankhya aur index ka size us repository ko create karne wale author ke control mein hota hai.
Complexity mein improvement
Agar S sections hain aur index mein E entries hain, toh:
Change se pehle: O(S*E) time aur O(S) repository operations.
Change ke baad: O(S+E) time aur O(1) repository operations.
Aisa isliye hai kyunki ab index ko maximum ek baar read kiya jata hai aur HEAD ko bhi maximum ek baar resolve kiya jata hai.
Benchmarks
Benchmarks Git 2.54.0, Python 3.12 aur CLI backend ke saath run kiye gaye.
Case 1: HEAD mein 400 submodule gitlinks aur unse match karne wale 400 sections
Change se pehle: Repo.submodules ko 51.62 seconds lage aur 1214 Git processes launch hue.
Change ke baad: 0.46 seconds lage aur sirf 17 Git processes launch hue.
Case 2: HEAD mein absent paths wale 400 sections aur 3001 entries wala index
Change se pehle: 20.85 seconds lage aur 419 processes launch hue.
Change ke baad: 0.43 seconds lage aur sirf 15 processes launch hue.
Yahan add kiye gaye twelve-section fixture par git._backend.statistics() ne ye operation counts record kiye:
CLI backend: change se pehle 43 operations aur baad mein 10.
Gix backend: change se pehle 44 operations aur baad mein 11.
Ek single section ke liye counts respectively 10 aur 11 the. Iska matlab hai ki har additional submodule ke liye hone wale teen extra operations ab completely eliminate ho gaye hain.
Results mein koi change nahi hua
IndexFile.entries path aur stage ke basis par keyed hota hai. Isliye entries.get((path, 0)) wahi stage 0 entry select karta hai jo pehle IndexFile.entry(path) return karta tha.
Agar key missing hoti hai, toh code wahi continue execute karta hai jo pehle KeyError ke case mein execute hota tha.
HEAD ab bhi sirf tab resolve hota hai jab koi section successfully resolve hota hai. Isliye agar .gitmodules mein koi bhi tracked entry nahi hai, toh HEAD ko bilkul resolve nahi kiya jata.
Naye test mein pehle twelve sections aur phir ek section ki listing ki jaati hai aur assert kiya jata hai ki dono cases mein recorded operation counts equal hain.
Yeh test in teen scenarios ke liye parametrized hai:
Tree mein maujood paths.
Sirf index mein staged paths.
Aise paths jo kahin bhi present nahi hain.
Change se pehle ye teeno tests fail hote the:
CLI backend par: 43 vs 10.
CLI backend par: 55 vs 11.
CLI backend par: 19 vs 8.
Change ke baad teeno tests dono backends par pass hote hain.
Motivation
Main audit kar raha tha ki GitPython untrusted .gitmodules content ko kaise handle karta hai. Is situation mein sections ki sankhya poori tarah repository author ke control mein hoti hai.
Is dauraan maine measure kiya ki sirf submodules ki listing karne wale caller ke liye bahut saare sections wali repository kitni expensive ho sakti hai.
Yeh wahi proactive performance improvement hai jiske liye contributing guide permission deta hai. Isi wajah se complexity statement aur upar diye gaye benchmark numbers ko downstream issue link ke replacement ke roop mein provide kiya gaya hai.