perf(core): cache loadApiKey to reduce redundant keychain access#21520
perf(core): cache loadApiKey to reduce redundant keychain access#21520
Conversation
The `loadApiKey` function accesses the system keychain to retrieve the API key, which involves inter-process communication (IPC) overhead. Since this function can be called repeatedly during startup or context refreshes, this commit wraps it in a short-lived memory cache to improve startup performance and avoid unnecessary OS prompts.
|
Hi there! Thank you for your contribution to Gemini CLI. To improve our contribution process and better track changes, we now require all pull requests to be associated with an existing issue, as announced in our recent discussion and as detailed in our CONTRIBUTING.md. This pull request is being closed because it is not currently linked to an issue. Once you have updated the description of this PR to link an issue (e.g., by adding How to link an issue: Thank you for your understanding and for being a part of our community! |
|
Hi @sehoon38, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this. We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines. Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed. Thank you for your understanding and for being a part of our community! |
Summary of ChangesHello, 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 introduces a caching layer for API key retrieval, significantly enhancing the performance of the CLI. By storing the API key in a short-lived in-memory cache, the system avoids repeated, costly interactions with the underlying keychain, leading to faster startup times and more efficient context refreshes. The design ensures that while performance is boosted, the system remains responsive to real-time changes in the API key. Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces caching for loadApiKey to improve performance, which is a good optimization. However, the implementation of the cache reset mechanism for tests introduces shared mutable state at the module level, which can lead to flaky tests. I've suggested a safer alternative using the existing cache API, aligning with the rule for using existing cache dependencies. Additionally, the test refactoring has inadvertently removed coverage for important edge cases related to clearing the API key and error handling, which should be restored.
|
Thank you for linking an issue! This pull request has been automatically reopened. |
|
Size Change: +446 B (0%) Total Size: 26 MB ℹ️ View Unchanged
|
Summary
This PR caches the results of
loadApiKeyto avoid redundant keychain access during CLI startup and context refreshes. Accessing the system keychain involves IPC overhead, so caching this for a short duration (30 seconds) improves startup performance while still allowing for near-instant updates if the key is changed interactively.Details
loadApiKeyinpackages/core/src/core/apiKeyCredentialStorage.tsusing the corecreateCacheutility.resetApiKeyCacheForTestingto allow proper test isolation.saveApiKeyandclearApiKeyto explicitly delete the cached entry to ensure changes take effect instantly.apiKeyCredentialStorage.test.tsto assert thatfsand storage read methods are only called the expected number of times, validating the cache behavior.Related Issues
Related to #21519
How to Validate
apiKeyCredentialStorage:npm test -w @google/gemini-cli-core -- src/core/apiKeyCredentialStorage.test.tsPre-Merge Checklist