Skip to content

Reuse the result of which git#8224

Merged
charliermarsh merged 2 commits intoastral-sh:mainfrom
j178:git
Oct 15, 2024
Merged

Reuse the result of which git#8224
charliermarsh merged 2 commits intoastral-sh:mainfrom
j178:git

Conversation

@j178
Copy link
Contributor

@j178 j178 commented Oct 15, 2024

Summary

Cache the path to git executable in a LazyLock and reuse it throughout the process. This might reduce some costs on finding the git executable.

@zanieb zanieb added the performance Potential performance improvement label Oct 15, 2024
@charliermarsh charliermarsh requested a review from konstin October 15, 2024 16:28
@konstin
Copy link
Member

konstin commented Oct 15, 2024

Sounds like a good idea, do you have some numbers how much overhead finding git is?

@j178
Copy link
Contributor Author

j178 commented Oct 15, 2024

I wrote a simple script to measure the overhead of finding git. Running it 50 times reduces the time by ~39ms on my machine.

use std::time::{Duration, Instant};
use std::process::Command;

fn run(git: &str, n: u32) -> Duration {
    let start = Instant::now();
    for _ in 0..n {
        let status = Command::new(git)
            .arg("--version")
            .stdout(std::process::Stdio::null())
            .status()
            .unwrap();
        assert!(status.success());
    }
    start.elapsed()
}

fn main() {
    let n = 50;
    let d1 = run("git", n);
    let d2 = run("/opt/homebrew/bin/git", n);
    println!("diff: {}ms", (d1 - d2).as_millis());
}

@j178
Copy link
Contributor Author

j178 commented Oct 15, 2024

The overhead depends on the path's position in PATH. If I move the Git path to the end (18th) of PATH, the reduced time can increase to approximately 500ms.

@charliermarsh
Copy link
Member

Looks reasonable to me.

@charliermarsh charliermarsh merged commit 0b5cc95 into astral-sh:main Oct 15, 2024
@j178 j178 deleted the git branch October 15, 2024 18:28
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Oct 16, 2024
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [astral-sh/uv](https://github.com/astral-sh/uv) | patch | `0.4.21` -> `0.4.22` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>astral-sh/uv (astral-sh/uv)</summary>

### [`v0.4.22`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0422)

[Compare Source](astral-sh/uv@0.4.21...0.4.22)

##### Enhancements

-   Respect `[tool.uv.sources]` in build requirements ([#&#8203;7172](astral-sh/uv#7172))

##### Preview features

-   Add a dedicated `uv publish` error message for missing usernames ([#&#8203;8045](astral-sh/uv#8045))
-   Support interactive input in `uv publish` ([#&#8203;8158](astral-sh/uv#8158))
-   Use raw filenames in `uv publish` ([#&#8203;8204](astral-sh/uv#8204))

##### Performance

-   Reuse the result of `which git` ([#&#8203;8224](astral-sh/uv#8224))

##### Bug fixes

-   Avoid environment check optimization for `uv pip install --exact` ([#&#8203;8219](astral-sh/uv#8219))
-   Do not use free-threaded interpreters without a free-threaded request ([#&#8203;8191](astral-sh/uv#8191))
-   Don't recommend `--prerelease=allow` during build requirement resolution errors ([#&#8203;8192](astral-sh/uv#8192))
-   Prefer optimized builds for free-threaded Python downloads ([#&#8203;8196](astral-sh/uv#8196))
-   Retain old `python-build-standalone` releases ([#&#8203;8216](astral-sh/uv#8216))
-   Run `uv build` builds in the source distribution bucket ([#&#8203;8220](astral-sh/uv#8220))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants