Repository navigation
Conversation
| let checkout_workers = std::thread::available_parallelism() | ||
| .map(|n| n.get()) | ||
| .unwrap_or(1) | ||
| .min(8); |
There was a problem hiding this comment.
I am not sure if 8 is the right default. I expect checkout is IO heavy, but maybe not? I can benchmark more deeper though.
There was a problem hiding this comment.
Git has a benchmark result: git/git@7531e4b66e8c. 8 seems to be a sweet spot.
There was a problem hiding this comment.
My benchmark (LLM assisted). My takeaway.
- 8 vs 16 core rarly matters.
- A worker per core is wrong. 32 and 96 aren't better for small-to-mid size repos.
I think we can be a bit conservative with 8.
Benchmark result
git checkout time by checkout.workers (mean ± σ; % vs 8, the current cap)
| repository (files) | 1 | 2 | 4 | 8 | 16 | 32 | 96 |
|---|---|---|---|---|---|---|---|
| tpde (400) | 56.2 ± 13.9 ms | 38.1 ± 0.6 ms | 27.1 ± 0.6 ms | 25.0 ± 0.4 ms | 30.2 ± 0.9 ms (+21%) | 43.2 ± 1.7 ms (+73%) | 97.8 ± 3.9 ms (+292%) |
| rust-lang/cargo (3k) | 314.8 ± 2.3 ms | 223.9 ± 3.1 ms | 173.1 ± 11.7 ms | 150.2 ± 3.5 ms | 137.9 ± 4.6 ms (−8%) | 151.4 ± 6.8 ms (+1%) | 205.7 ± 7.5 ms (+37%) |
| rustc-perf (15k) | 1.29 ± 0.02 s | 836 ± 11 ms | 613 ± 13 ms | 502 ± 5 ms | 432 ± 4 ms (−14%) | 435 ± 14 ms (−13%) | 501 ± 23 ms (0%) |
| rust-lang/rust (64k) | 3.83 ± 0.04 s | 2.79 ± 0.02 s | 1.96 ± 0.04 s | 1.51 ± 0.02 s | 1.26 ± 0.01 s (−17%) | 1.18 ± 0.01 s (−22%) | 1.23 ± 0.06 s (−19%) |
| gcc (163k) | 11.34 ± 0.08 s | 7.85 ± 0.09 s | 5.51 ± 0.05 s | 4.44 ± 0.09 s | 3.82 ± 0.03 s (−14%) | 3.19 ± 0.05 s (−28%) | 3.18 ± 0.39 s (−28%) |
| llvm-project (181k) | 15.33 ± 0.13 s | 11.33 ± 0.12 s | 7.70 ± 0.09 s | 5.92 ± 0.09 s | 4.98 ± 0.08 s (−16%) | 4.73 ± 0.06 s (−20%) | 4.65 ± 0.11 s (−21%) |
| environment | |
|---|---|
| CPU | AMD EPYC 9R14, 96 cores (1 socket, 1 thread per core) |
| memory | 739 GiB |
| storage | Amazon EBS volume (3.75 TiB), ext4, noatime |
| OS / kernel | Amazon Linux 2, Linux 5.10.273 |
| git | 2.56.0 |
| page cache | warm (the database's objects are hardlinked, already cached) |
| timed | only git checkout --quiet --force --detach <rev> |
| untimed setup | per run: git init --template= plus hardlinked objects/, as Cargo's checkout does |
| config | user/system config and attributes ignored, core.fsmonitor=false, as in Cargo |
| tool | hyperfine 2.0.0, 1 warm-up, 10 runs (5 for trees over 50k files) |
16f5164 to
8087935
Compare
| .with_checkout(checkout) | ||
| .fetch_options(fopts) | ||
| .clone(url.as_str(), into)?; | ||
| // `git2` doesn't seem to handle shallow repos correctly when doing |
There was a problem hiding this comment.
With this, I wonder if we should abandon shallow clone for blobless clone.
There was a problem hiding this comment.
blobless clone still needs to download the full history, so for long history repos like rust-lang/rust it might cost more. The win also varies when you clone a huge repo or tiny one. I guess we needs more benchmark and design around.
There was a problem hiding this comment.
More than shallow? Yes. But shallow only really works if we have one db per sha. I assume long histories will still be a lot faster than getting blobs.
There was a problem hiding this comment.
It also depends on CI setup, no? If job doesn't cache git db, everything is oneshot. We may need user configuration for different use cases.
| let repo = repo.unwrap(); | ||
|
|
||
| let repo = if should_checkout_with_cli(gctx) { | ||
| checkout_with_cli(into, database, revision, gctx).map_err(|err| { |
There was a problem hiding this comment.
There might be a forward compatibility hazard. See in the PR description.
I'd like to know whether we want to have some hook point or configuration to help avoid that.
There was a problem hiding this comment.
Back to the old days, I'd like to say the forward compat hazard is unlikely to happen. I am not so sure now…
There was a problem hiding this comment.
This becomes a problem only when a user mixes git versions and does a git checkout. I'm assuming that is less common? Would we see that the repo is "corrupt" and restart?
There was a problem hiding this comment.
Yeah, you're right. Unless they have some shared caching, it is uncommon. Also, disk space is already an issue in Cargo, and people likely just nuke them if corrupted.
There was a problem hiding this comment.
Okay, I think we only nuke and restart when a repo cannot be opened, but for later reads, it would just fail and be stuck.
So here is one possible scenario:
- New cargo using new git CLI creates new checkouts.
- Old cargo using libgit2 can open but cannot read them.
- Or the same version of cargo but the stock libgit2 cannot read them
I think we anyway will hit this issue, if we rely on Git regardless via gitoxide or libgit2 or CLI.
We could possibly validate the "read" part before we write .cargo-ok file, so that we treat read failures as stale cache instead of hard failure. Not sure if this should be done in this PR
There was a problem hiding this comment.
Whats the difference between "open" and "later reads". Can we anticipate some failures and do some sanity checks?
As for libgit2, should we use a new set of directories? Likely will need to do that for partial clones.
There was a problem hiding this comment.
Whats the difference between "open" and "later reads". Can we anticipate some failures and do some sanity checks?
yes, just repo.index() is good enough for index format change I believe. I may send a separate PR for this change.
As for libgit2, should we use a new set of directories?
Yes, but not for this. I want this to be backward-compatible
There was a problem hiding this comment.
Whats the difference between "open" and "later reads". Can we anticipate some failures and do some sanity checks?
yes, just
repo.index()is good enough for index format change I believe. I may send a separate PR for this change.
I was kinda wrong. Cargo doesn't need git index to read files from a git checkout for cargo build. The only case still reads from git index is cargo vendor. If we sanity-checks this for cargo vendor, the cost will be every cargo build used to work start failing because of the sanity check of libgit/gix compatibility.
I think we are actually fine here for 99% of the workflow by doing nothing.
|
This also lets us use new features like blobless clones! |
|
r? @Muscraft rustbot has assigned @Muscraft. Use Why was this reviewer chosen?The reviewer was selected based on:
|
74d0b17 to
a31c5dd
Compare
This comment was marked as resolved.
This comment was marked as resolved.
a31c5dd to
9efb8dd
Compare
Before rust-lang#16459 cargo ran `git gc` in foreground. The switch to `--auto` implicit run it in background which was not discussed there or in rust-lang#4656. I believe this was an oversight. rust-lang#17813 hits this because it hardlinks git's object store, and during gc there will be trasient lockfile. libgit2 and git cli also have this problem but just never hit it.
### What does this PR try to resolve? git by default runs gc in a detached process in background, which may race with other git fetch and checkout (rust-lang#17813). This * disables implicit gc in `git fetch`. It is redundant since we have `maybe_gc_repo` run before fetch * make gc always run in foreground ### Notes to reviewers For my rationale, see commit messages. I am not sure if we want tests for them. It may need `GIT_TRACE` or something to probe it. --- 🤖 LLM involvement: Directed fix.
This comment has been minimized.
This comment has been minimized.
| let checkout_workers = std::thread::available_parallelism() | ||
| .map(|n| n.get()) | ||
| .unwrap_or(1) | ||
| .min(8); |
There was a problem hiding this comment.
Git has a benchmark result: git/git@7531e4b66e8c. 8 seems to be a sweet spot.
| let checkout_workers = std::thread::available_parallelism() | ||
| .map(|n| n.get()) | ||
| .unwrap_or(1) | ||
| .min(8); |
There was a problem hiding this comment.
My benchmark (LLM assisted). My takeaway.
- 8 vs 16 core rarly matters.
- A worker per core is wrong. 32 and 96 aren't better for small-to-mid size repos.
I think we can be a bit conservative with 8.
Benchmark result
git checkout time by checkout.workers (mean ± σ; % vs 8, the current cap)
| repository (files) | 1 | 2 | 4 | 8 | 16 | 32 | 96 |
|---|---|---|---|---|---|---|---|
| tpde (400) | 56.2 ± 13.9 ms | 38.1 ± 0.6 ms | 27.1 ± 0.6 ms | 25.0 ± 0.4 ms | 30.2 ± 0.9 ms (+21%) | 43.2 ± 1.7 ms (+73%) | 97.8 ± 3.9 ms (+292%) |
| rust-lang/cargo (3k) | 314.8 ± 2.3 ms | 223.9 ± 3.1 ms | 173.1 ± 11.7 ms | 150.2 ± 3.5 ms | 137.9 ± 4.6 ms (−8%) | 151.4 ± 6.8 ms (+1%) | 205.7 ± 7.5 ms (+37%) |
| rustc-perf (15k) | 1.29 ± 0.02 s | 836 ± 11 ms | 613 ± 13 ms | 502 ± 5 ms | 432 ± 4 ms (−14%) | 435 ± 14 ms (−13%) | 501 ± 23 ms (0%) |
| rust-lang/rust (64k) | 3.83 ± 0.04 s | 2.79 ± 0.02 s | 1.96 ± 0.04 s | 1.51 ± 0.02 s | 1.26 ± 0.01 s (−17%) | 1.18 ± 0.01 s (−22%) | 1.23 ± 0.06 s (−19%) |
| gcc (163k) | 11.34 ± 0.08 s | 7.85 ± 0.09 s | 5.51 ± 0.05 s | 4.44 ± 0.09 s | 3.82 ± 0.03 s (−14%) | 3.19 ± 0.05 s (−28%) | 3.18 ± 0.39 s (−28%) |
| llvm-project (181k) | 15.33 ± 0.13 s | 11.33 ± 0.12 s | 7.70 ± 0.09 s | 5.92 ± 0.09 s | 4.98 ± 0.08 s (−16%) | 4.73 ± 0.06 s (−20%) | 4.65 ± 0.11 s (−21%) |
| environment | |
|---|---|
| CPU | AMD EPYC 9R14, 96 cores (1 socket, 1 thread per core) |
| memory | 739 GiB |
| storage | Amazon EBS volume (3.75 TiB), ext4, noatime |
| OS / kernel | Amazon Linux 2, Linux 5.10.273 |
| git | 2.56.0 |
| page cache | warm (the database's objects are hardlinked, already cached) |
| timed | only git checkout --quiet --force --detach <rev> |
| untimed setup | per run: git init --template= plus hardlinked objects/, as Cargo's checkout does |
| config | user/system config and attributes ignored, core.fsmonitor=false, as in Cargo |
| tool | hyperfine 2.0.0, 1 warm-up, 10 runs (5 for trees over 50k files) |
the guard needs the repo path, and the repo has to exist first but when checking out it is not yet exist, so move it. Otherwise backends checkout split will need to be aware of guard The move is safe because * no code use the guard between old and new location * we anyway need to return a guard to caller
It is the only caller. Inline to prepare for multi-backend support
So git CLI checkout can share it.
`update_submodule` used to passes submodule's git dir `<path>/.git` as the checkout destination. That works because when init/clone, libgit2 accepts both `<workdir>/.git` or bare `<workdir>` as the same workdir, while git CLI take only `<workdir>`
With this PR, Git dependencies are now checked out with `git` CLI whenever `git` is available. Otherwise it falls back to libgit2 as before. As how Cargo checks out is an implementation detail, we don't plan to add any config but a temporary `__CARGO_TEMPORARY_GIT_CLI_CHECKOUT_OPT_OUT=1` to revert back to libgit2 checkout. Two motivations: * Speed. Git checks out about 2x faster than libgit2, even with a single worker. See the benchmark below. * Future removal of libgit2. Backward Compatibility ---------------------- The minimum supported git version (MSGV) is 2.0.0. Here is a list of git features used. | feature | since | with older git | | ---------------------------| ------ | ---------------------------------- | | `git checkout --detach` | 1.7.5 | below MSGV | | `git checkout --progress` | 2.7.0 | not passed | | `git init --object-format` | 2.27.0 | not passed, SHA-1 only | | `GIT_CONFIG_GLOBAL` | 2.32.0 | ignored, the `HOME` override works | | `checkout.workers` | 2.32.0 | ignored, checks out serially | | `git init --ref-format` | 2.44.0 | not passed, files only | Compared with libgit2: * Per-user and system attributes, like `eol=crlf` in `~/.config/git/attributes`, no longer apply. libgit2 applied them. But I think this is fine and more correct. * User settings affecting checkout like `core.eol` no longer apply. libgit2 applied them. Likewise, more correct. Forward compatibility --------------------- A newer `git` may write things that older Cargo can't read, for example in Git v4 splitIndex become the new default. This would fail old Cargo to read git checkouts generated by new Cargo. We can pin whatever config we knew already lke object format today, but we have no way to pin what we don't know. An escape hatch is to let people to configure our git process, if needed. Benchmark --------- | repository | files | metric | libgit2 | git CLI | speedup | | ------------------------------ | ----: | -------- | -------------: | -------------: | ------: | | rust-lang/cargo | 3k | total | 744 ± 35 ms | 532 ± 54 ms | 1.4x | | | | checkout | 510 ms | 270 ms | 1.9x | | rust-lang/rust, no submodules | 64k | total | 8.89 ± 0.05 s | 4.35 ± 0.05 s | 2.0x | | | | checkout | 8.43 ± 0.04 s | 3.89 ± 0.03 s | 2.2x | | rust-lang/rust with submodules | 432k | total | 76.01 ± 0.07 s | 35.01 ± 0.28 s | 2.2x | | | | checkout | 75.92 ± 0.39 s | 33.86 ± 0.04 s | 2.2x | * total: `cargo fetch --offline` after removing the checkout, with the git database already populated, measured by hyperfine (`-N -w 1`, 10/5/3 runs). * checkout: the sum of `checkout_with_*` spans from `CARGO_LOG_PROFILE=1` traces (mean of 3 runs), * rust-lang/cargo and rust-lang/rust without submodules are single-commit snapshots, * rust-lang/rust with submodules is `da0f0eae` with full history (346k commits, 1.73 GiB pack), and 19 checkouts including nested submodules. * 96 cores, NVMe, warm page cache, Linux 5.10, git 2.56.0. Mimimum supported git version by distributions ---------------------------------------------- Data from Repology 2026-11-19: | distribution | released | git | | ------------------ | -------- | ------- | | RHEL/CentOS 7 | 2014-06 | 1.8.3.1 | | Ubuntu 16.04 | 2016-04 | 2.7.4 | | Ubuntu 18.04 | 2018-04 | 2.17.1 | | Amazon Linux 2 | 2018-06 | 2.47.3 | | RHEL 8 (AlmaLinux) | 2019-05 | 2.43.7 | | Ubuntu 20.04 | 2020-04 | 2.25.1 | | Debian 11 | 2021-08 | 2.30.2 | | Ubuntu 22.04 | 2022-04 | 2.34.1 | | RHEL 9 (AlmaLinux) | 2022-05 | 2.52.0 | | Debian 12 | 2023-06 | 2.39.5 | | Ubuntu 24.04 | 2024-04 | 2.43.0 | | openSUSE Leap 15.6 | 2024-06 | 2.51.0 | | Debian 13 | 2025-08 | 2.47.3 |
git 2.32 can write files with parallel workers (`checkout.workers`). Benchmark --------- | repository | files | metric | 1 worker | 8 workers | speedup | | ------------------------------ | ----: | -------- | -------------: | -------------: | ------: | | rust-lang/cargo | 3k | total | 532 ± 54 ms | 360 ± 11 ms | 1.5x | | | | checkout | 270 ms | 133 ± 5 ms | 2.0x | | rust-lang/rust, no submodules | 64k | total | 4.35 ± 0.05 s | 1.91 ± 0.08 s | 2.3x | | | | checkout | 3.89 ± 0.03 s | 1.47 ± 0.02 s | 2.7x | | rust-lang/rust with submodules | 432k | total | 35.01 ± 0.28 s | 14.02 ± 0.08 s | 2.5x | | | | checkout | 33.86 ± 0.04 s | 13.43 ± 0.61 s | 2.5x | * total: `cargo fetch --offline` after removing the checkout, with the git database already populated, measured by hyperfine (`-N -w 1`, 10/5/3 runs). * checkout: the sum of `checkout_with_*` spans from `CARGO_LOG_PROFILE=1` traces (mean of 3 runs), * rust-lang/cargo and rust-lang/rust without submodules are single-commit snapshots. * rust-lang/rust with submodules is `da0f0eae` with full history (346k commits, 1.73 GiB pack), and 19 checkouts including nested submodules. * 96 cores, NVMe, warm page cache, Linux 5.10, git 2.56.0.
9efb8dd to
e820f4b
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
| } | ||
|
|
||
| /// Whether to check out git dependencies with `git` CLI. | ||
| pub(crate) fn should_checkout_with_cli(gctx: &GlobalContext) -> bool { |
There was a problem hiding this comment.
check out with git CLI by default
So the plan is to merge this and immediately switch over? Are we confident enough in that?
What are your thoughts on merging with this off and doing a Call for Testing before changing the default?
View all comments
What does this PR try to resolve?
With this PR,
Git dependencies are now checked out with
gitCLIwhenever
gitis available.Otherwise it falls back to libgit2 as before.
As how Cargo checks out is an implementation detail,
we don't plan to add any config but a temporary
__CARGO_TEMPORARY_GIT_CLI_CHECKOUT_OPT_OUT=1to revert back to libgit2 checkout.
Two motivations:
Git checks out about 2x faster than libgit2,
even with a single worker.
See the benchmark below.
Notes to reviewers
Some of the data below are initially generated by LLM and reviewed/modified by human. Make them hidden by default. Though some compatility issue we might want to pay attention to, especially forward compatibility.
(@0xPoe or someone reminded me that we have a VCS plugin/hook idea, maybe we should really explore that)
Backward Compatibility
The minimum supported git version (MSGV) is 2.0.0.
New features are added when detected git version supports it.
Click to expand
Here is a list of git features used.
git checkout --detachgit checkout --progressgit init --object-formatGIT_CONFIG_GLOBALHOMEoverride workscheckout.workersgit init --ref-formatCompared with libgit2:
like
eol=crlfin~/.config/git/attributes,no longer apply.
libgit2 seems to apply them.
But I think this is fine and more correct.
core.eolno longer apply.libgit2 seems to apply them.
Likewise, more correct.
Forward compatibility
A newer
gitmay write things that older Cargo can't read,for example in Git v4 splitIndex become the new default.
This would fail old Cargo to read git checkouts generated by new Cargo.
We can pin whatever config we knew already lke object format today,
but we have no way to pin what we don't know.
An escape hatch is to let people to configure our git process, if needed.
See #17813 (comment)
Benchmark
1.4-2.2x faster comparing with baseline against rust-lang/rust and rust-lang/cargo, and with 8 workers is 1.5x-2.5x faster than 1 worker.
Click to expand
With checkout worker
cargo fetch --offlineafter removing the checkout,with the git database already populated,
measured by hyperfine (
-N -w 1, 10/5/3 runs).checkout_with_*spansfrom
CARGO_LOG_PROFILE=1traces (mean of 3 runs),are single-commit snapshots,
da0f0eaewith full history(346k commits, 1.73 GiB pack),
and 19 checkouts including nested submodules.
Mimimum supported git version by distributions
Click to expand
Data from Repology 2026-11-19:
🤖 LLM involvement: Directed prototyping.