[Performance] Memoize hasGit check#7937
Conversation
Checking for Git presence via `git --version` is an expensive operation that involves spawning a new process. This check is performed multiple times during the execution of various CLI commands. Since Git's presence is unlikely to change during the execution of a single command, we can memoize the result to improve performance. This PR memoizes the `hasGit` function in `packages/cli-kit/src/public/node/context/local.ts` using a module-level promise. - Introduced `hasGitPromise` to cache the result. - Updated `hasGit` to return the cached promise if available. - Added and exported `_resetHasGit` for test isolation. - Updated unit tests in `packages/cli-kit/src/public/node/context/local.test.ts` to verify memoization.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Differences in type declarationsWe detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:
New type declarationsWe found no new type declarations in this PR Existing type declarationspackages/cli-kit/dist/public/node/context/local.d.ts@@ -132,6 +132,11 @@ export declare function cloudEnvironment(env?: NodeJS.ProcessEnv): {
* @returns A promise that resolves with the value.
*/
export declare function hasGit(): Promise<boolean>;
+/**
+ * Resets the memoized value for the hasGit check.
+ * This is only intended for use in tests.
+ */
+export declare function _resetHasGit(): void;
/**
* Gets info on the CI platform the CLI is running on, if applicable.
*
|
WHY are these changes introduced?
Checking for Git presence via
git --versionis an expensive operation that involves spawning a new process. This check is performed multiple times during the execution of various CLI commands. Since Git's presence is unlikely to change during the execution of a single command, we can memoize the result to improve performance.WHAT is this pull request doing?
This PR memoizes the
hasGitfunction inpackages/cli-kit/src/public/node/context/local.tsusing a module-level promise.hasGitPromiseto cache the result.hasGitto return the cached promise if available._resetHasGitfor test isolation.packages/cli-kit/src/public/node/context/local.test.tsto verify memoization.How to test your changes?
CI
Post-release steps
Checklist
patchfor bug fixes ·minorfor new features ·majorfor breaking changes) and added a changeset withpnpm changeset addPR created automatically by Jules for task 1744815129505296381 started by @gonzaloriestra