Skip to content

Reduce CLI startup latency by deferring eager startup paths#20643

Closed
wolph wants to merge 4 commits intogoogle-gemini:mainfrom
wolph:fix/startup-latency-10726
Closed

Reduce CLI startup latency by deferring eager startup paths#20643
wolph wants to merge 4 commits intogoogle-gemini:mainfrom
wolph:fix/startup-latency-10726

Conversation

@wolph
Copy link
Copy Markdown

@wolph wolph commented Feb 28, 2026

Fixes #10726

Summary

This change reduces startup latency by removing several eager startup paths that were being triggered even for lightweight or non-interactive invocations.

What changed

  • Added a --version fast path in packages/cli/index.ts so version checks return before loading the full CLI runtime.
  • Broke command parsing import cycles by moving initializeOutputListenersAndFlush into src/utils/outputListeners.ts and updating command modules to import from that utility.
  • Deferred AppContainer loading to interactive startup (startInteractiveUI) instead of importing it at module load time.
  • Avoided automatic child-process relaunch for non-interactive runs (kept for interactive mode).
  • Avoided eager editor command probing at module import time by computing editor availability lazily in EditorSettingsManager.
  • Added/updated tests for new startup behavior and lazy editor discovery.

Benchmarks

Measured on Windows (same machine), using local built CLI (
ode packages/cli/dist/index.js).

Before (clean checkout, pre-patch):

  • --version: avg 10,393.4 ms
  • --help: avg 9,562.7 ms
  • -p /quit: avg 27,780.1 ms

After (this branch):

  • --version: avg 85.4 ms
  • --help: avg 7,223.8 ms
  • -p /quit: avg 7,157.1 ms

Validation


  • pm run typecheck --workspace @google/gemini-cli

  • px eslint on all modified files

  • pm run test --workspace @google/gemini-cli -- src/gemini.test.tsx src/commands/extensions.test.tsx src/commands/skills.test.tsx src/commands/mcp.test.ts src/ui/editors/editorSettingsManager.test.ts
  • ⚠️
    pm run test --workspace @google/gemini-cli (full suite) still has pre-existing environment-specific failures unrelated to touched files (policy-engine integration expectations, terminal notification assertions, and snapshot differences in stats/session summary components).

@wolph wolph requested a review from a team as a code owner February 28, 2026 01:45
@google-cla
Copy link
Copy Markdown

google-cla bot commented Feb 28, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the CLI's startup performance by strategically deferring several eager initialization paths. The changes ensure that heavyweight modules and UI components are only loaded when strictly necessary, such as for interactive sessions, while providing a rapid response for lightweight commands like --version. This optimization aims to provide a snappier user experience across various CLI invocations.

Highlights

  • Version Fast Path: Implemented a --version fast path in packages/cli/index.ts to allow version checks to return quickly without loading the full CLI runtime, drastically reducing startup time for this specific command.
  • Command Parsing Import Cycle Resolution: Resolved command parsing import cycles by moving the initializeOutputListenersAndFlush function into a new utility file (src/utils/outputListeners.ts) and updating all command modules to import it from the new location.
  • Deferred UI Loading: Deferred the loading of AppContainer and related UI initialization to occur only during interactive startup (startInteractiveUI), preventing unnecessary UI component loading for non-interactive invocations.
  • Conditional Child-Process Relaunch: Modified the CLI to avoid automatic child-process relaunch for non-interactive runs, retaining this behavior only for interactive mode to optimize non-interactive command execution.
  • Lazy Editor Command Probing: Prevented eager editor command probing at module import time by implementing lazy computation of editor availability within EditorSettingsManager.
  • Test Updates: Added and updated tests to cover the new startup behavior and the lazy editor discovery mechanism.
Changelog
  • packages/cli/index.ts
    • Added a fast path for --version and -v arguments to immediately print the CLI version and exit.
    • Refactored the main execution logic into an asynchronous run function.
    • Dynamically imported core CLI modules (./src/gemini.js, @google/gemini-cli-core, ./src/utils/cleanup.js) using Promise.all to defer their loading.
  • packages/cli/src/commands/extensions.test.tsx
    • Updated the mock path for initializeOutputListenersAndFlush to reflect its new location.
  • packages/cli/src/commands/extensions.tsx
    • Updated the import path for initializeOutputListenersAndFlush from ../gemini.js to ../utils/outputListeners.js.
  • packages/cli/src/commands/hooks.tsx
    • Updated the import path for initializeOutputListenersAndFlush from ../gemini.js to ../utils/outputListeners.js.
  • packages/cli/src/commands/mcp.ts
    • Updated the import path for initializeOutputListenersAndFlush from ../gemini.js to ../utils/outputListeners.js.
  • packages/cli/src/commands/skills.test.tsx
    • Updated the mock path for initializeOutputListenersAndFlush to reflect its new location.
  • packages/cli/src/commands/skills.tsx
    • Updated the import path for initializeOutputListenersAndFlush from ../gemini.js to ../utils/outputListeners.js.
  • packages/cli/src/gemini.tsx
    • Removed the eager import of AppContainer and deferred its loading until startInteractiveUI is called.
    • Removed OutputPayload, ConsoleLogPayload, UserFeedbackPayload, and CoreEvent type imports, as initializeOutputListenersAndFlush was moved.
    • Moved the initializeOutputListenersAndFlush function to a new dedicated utility file.
    • Introduced conditional UI initialization logic, ensuring initializeApp and related UI setup only run if interactive mode or Zed integration is required.
    • Adjusted the relaunchAppInChildProcess call to only occur if partialConfig.isInteractive() is true.
  • packages/cli/src/ui/editors/editorSettingsManager.test.ts
    • Added a new test file to verify the lazy computation of editor availability in editorSettingsManager.
  • packages/cli/src/ui/editors/editorSettingsManager.ts
    • Modified EditorSettingsManager to lazily compute availableEditors only when getAvailableEditorDisplays() is first called, rather than during construction.
  • packages/cli/src/utils/outputListeners.ts
    • Added a new file containing the initializeOutputListenersAndFlush function, previously located in gemini.tsx.
Activity
  • The pull request was created by wolph to address startup latency issues.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant startup latency improvements by deferring expensive initialization paths. The strategies employed, such as adding a fast path for --version, lazy loading UI components and editor checks, and avoiding unnecessary process relaunches for non-interactive runs, are well-executed and the benchmark results are impressive. The refactoring to break an import cycle is also a good structural improvement. The feedback regarding outputListeners.ts has been retained as it does not contradict any existing rules.

@gemini-cli gemini-cli bot added priority/p1 Important and should be addressed in the near term. area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt labels Feb 28, 2026
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Mar 15, 2026

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

@gemini-cli gemini-cli bot closed this Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/platform Issues related to Build infra, Release mgmt, Testing, Eval infra, Capacity, Quota mgmt priority/p1 Important and should be addressed in the near term.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Critical Slowdown in Gemini CLI Startup Time (up to 60 seconds)

1 participant