Skip to content

⚡ Bolt: Reuse Gemini client to reduce overhead#23

Open
suvadityamuk wants to merge 1 commit intomainfrom
bolt/reuse-gemini-client-4077313459110273685
Open

⚡ Bolt: Reuse Gemini client to reduce overhead#23
suvadityamuk wants to merge 1 commit intomainfrom
bolt/reuse-gemini-client-4077313459110273685

Conversation

@suvadityamuk
Copy link
Owner

💡 What: Modified the pipeline to instantiate the Google Gemini API client once and pass it to all utility functions, instead of creating a new client for every request.

🎯 Why: Measuring revealed that google.genai.Client instantiation takes ~80ms-220ms. The previous implementation instantiated it 5-6 times per generation cycle (once for prompt refinement, and once for each of the 4-5 image views), adding unnecessary latency and preventing connection reuse.

📊 Impact:

  • Reduces "wall clock" CPU time by eliminating ~400ms of redundant initialization work.
  • Enables the underlying HTTP client to potentially reuse connections (keep-alive) for the parallel image generation requests, further reducing latency.

🔬 Measurement:

  • Verified with a test script tests/test_optimization.py that mocks the google.genai library and confirms the client is instantiated exactly once during the full pipeline execution.
  • Baseline reproduction showed 5 instantiations; fix shows 1.

PR created automatically by Jules for task 4077313459110273685 started by @suvadityamuk

- Instantiated `google.genai.Client` once in `operators.py` and passed it to `utils` functions.
- Updated `utils.py` to accept optional `client` argument in `refine_prompt` and `generate_image`.
- Eliminates ~5 redundant client instantiations per generation cycle (~80ms each).
- Enables potential connection pooling for parallel requests.

Co-authored-by: suvadityamuk <70141886+suvadityamuk@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2026

🤖 Hi @suvadityamuk, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@gemini-code-assist
Copy link

Summary of Changes

Hello @suvadityamuk, 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 implements a crucial performance optimization by refactoring the Google Gemini API client instantiation. Instead of repeatedly creating new client instances within the image generation pipeline, a single client is now initialized and reused across all relevant utility functions. This change aims to reduce processing time and improve network efficiency by leveraging connection reuse, ultimately leading to a faster and more resource-efficient generation process.

Highlights

  • Performance Optimization: The Google Gemini API client is now instantiated only once per pipeline execution, significantly reducing overhead associated with repeated client creation.
  • Dependency Injection: The Gemini client instance is passed as an argument to utility functions like refine_prompt and generate_image, promoting reuse and cleaner code architecture.
  • Latency Reduction: This change eliminates approximately 400ms of redundant client initialization time and enables the underlying HTTP client to potentially reuse connections, further reducing overall latency.
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
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

📋 Review Summary

This pull request effectively addresses a performance bottleneck by instantiating the Gemini client once and reusing it across multiple function calls. The changes are well-implemented, improving the efficiency of the image generation pipeline.

🔍 General Feedback

  • The use of dependency injection to pass the client is a good practice and improves the code's modularity and testability.
  • Documenting the performance learning in .jules/bolt.md is a great way to share knowledge within the project.
  • The changes are clean and focused on the performance improvement.

Copy link

@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 a valuable performance optimization by instantiating the Gemini client once and reusing it across multiple function calls, which effectively reduces latency. The implementation is sound and the goal is clearly achieved. My review includes a few suggestions for operators.py to further simplify the code by leveraging Python's closure feature, which will make the implementation even cleaner.


# Helper to generate a single view
def generate_view(view_name, view_prompt, input_ref=None):
def generate_view(view_name, view_prompt, input_ref=None, client=None):

Choose a reason for hiding this comment

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

medium

Since generate_view is a nested function, it can directly access the client variable from its parent _run_pipeline scope (this is called a closure). You don't need to pass client as an argument, which simplifies the function signature. The corresponding call sites will also need to be updated.

Suggested change
def generate_view(view_name, view_prompt, input_ref=None, client=None):
def generate_view(view_name, view_prompt, input_ref=None):

Comment on lines +116 to +118
res_path = utils.generate_image(
gemini_key, view_prompt, out, input_ref, client=client
)

Choose a reason for hiding this comment

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

medium

With the client parameter removed from generate_view's signature, this call to utils.generate_image will now correctly use the client variable captured from the _run_pipeline scope. No change is needed here, but this comment is to confirm the logic holds after the suggested change to the function signature.


# Generate front view first
front_path = generate_view("Front", front_prompt, None)
front_path = generate_view("Front", front_prompt, None, client=client)

Choose a reason for hiding this comment

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

medium

Following the simplification of generate_view's signature, the client argument is no longer needed in this call.

Suggested change
front_path = generate_view("Front", front_prompt, None, client=client)
front_path = generate_view("Front", front_prompt, None)

Comment on lines +153 to +155
executor.submit(
generate_view, v_name, v_prompt, front_path, client
)

Choose a reason for hiding this comment

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

medium

As generate_view no longer accepts a client parameter, this argument should be removed from the executor.submit call.

                        executor.submit(generate_view, v_name, v_prompt, front_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant