Skip to content

⚡ Bolt: Reuse Gemini client to save ~400ms per request#26

Open
suvadityamuk wants to merge 1 commit intomainfrom
bolt/genai-client-reuse-10245002133299409996
Open

⚡ Bolt: Reuse Gemini client to save ~400ms per request#26
suvadityamuk wants to merge 1 commit intomainfrom
bolt/genai-client-reuse-10245002133299409996

Conversation

@suvadityamuk
Copy link
Owner

💡 What: Refactored the generation pipeline to instantiate the google.genai.Client once and reuse it across multiple API calls, instead of creating a new client for each call.

🎯 Why: Benchmarking revealed that instantiating google.genai.Client takes approximately 80ms. The original implementation instantiated the client 5 times (1 for prompt refinement + 4 for view generation), adding ~400ms of avoidable latency to every generation request.

📊 Impact: Reduces the total pipeline execution time by ~400ms. This is a pure overhead reduction that makes the UI feel more responsive and reduces unnecessary object creation.

🔬 Measurement: Confirmed via benchmark_overhead.py which showed a reduction from ~447ms to ~83ms for client overhead in the simulated pipeline.


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

Instantiates `google.genai.Client` once in `operators.py` and passes it to `utils.py` functions, eliminating ~400ms of overhead (5 instantiations x ~80ms) per generation request.

- Updated `utils.refine_prompt` and `utils.generate_image` to accept a client instance.
- Updated `operators.py` to create the client once and pass it down.
- Added performance note in code.

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

🤖 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 significantly enhances the performance of the generation pipeline by optimizing the instantiation of the google.genai.Client. By creating the client only once and reusing it across multiple API calls, the system avoids unnecessary overhead, leading to a more responsive user interface and reduced latency for generation requests.

Highlights

  • Performance Improvement: Reduced pipeline execution time by approximately 400ms by instantiating the google.genai.Client only once per request instead of multiple times.
  • Code Refactoring: Implemented dependency injection for the google.genai.Client by passing the client instance to refine_prompt and generate_image functions.
Changelog
  • .jules/bolt.md
    • Added a new entry documenting the learning and action related to GenAI client instantiation overhead.
  • operators.py
    • Introduced a single google.genai.Client instantiation within _run_pipeline.
    • Updated calls to utils.refine_prompt and utils.generate_image to pass the newly created client instance.
  • utils.py
    • Modified refine_prompt and generate_image functions to accept a client object as an argument.
    • Removed the internal get_client calls from refine_prompt and generate_image.
    • Adjusted line wrapping in a comment within generate_3d_meshy.
Activity
  • No human activity (comments, reviews, etc.) has been recorded for this pull request yet.
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

The Pull Request successfully optimizes the Gemini generation pipeline by reusing the genai.Client instance across multiple API calls. This architectural change eliminates approximately 400ms of latency per request, directly improving the responsiveness of the 3D generation process.

🔍 General Feedback

  • Efficiency: The consolidation of client instantiation is a significant win for performance, especially in a Blender add-on where responsiveness is key.
  • Maintainability: Adopting dependency injection for the Gemini client makes the code more modular and easier to test in the future.
  • Documentation: While the changes are clear, minor enhancements to docstrings could help keep the documentation in sync with the new function signatures.



def refine_prompt(api_key, prompt):
def refine_prompt(client, prompt):
Copy link
Contributor

Choose a reason for hiding this comment

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

🟢 Updated `refine_prompt` signature to accept `client` instead of `api_key`. While the change is correct, consider updating the docstring to explicitly document the parameters, which would improve maintainability as the codebase grows.



def generate_image(api_key, prompt, output_path, input_image_path=None):
def generate_image(client, prompt, output_path, input_image_path=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

🟢 Updated `generate_image` signature to accept `client` instead of `api_key`. Similarly to `refine_prompt`, adding parameter descriptions to the docstring would be a beneficial minor improvement for documentation consistency.

def _run_pipeline(self, gemini_key, meshy_key, prompt, q):
try:
# ⚡ Bolt: Create client once and reuse to avoid instantiation overhead (~80ms per call)
client = utils.get_client(gemini_key)
Copy link
Contributor

Choose a reason for hiding this comment

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

🟢 Positive highlight: Consolidating the `genai.Client` instantiation here and passing it down via dependency injection is a great optimization. Avoiding repeated instantiation overhead (~400ms total) makes the generation pipeline significantly more responsive.

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 effectively addresses a performance bottleneck by reusing the google.genai.Client instance across multiple API calls, saving significant time on each request. The implementation correctly uses dependency injection by creating the client once and passing it to the helper functions. The changes are clear, well-motivated, and a great optimization. I have a couple of suggestions to improve the maintainability of the updated function signatures by adding type hints.



def refine_prompt(api_key, prompt):
def refine_prompt(client, prompt):

Choose a reason for hiding this comment

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

medium

For better code clarity and maintainability, please add type hints to this function signature. The type of the new client parameter is not obvious, and a hint would help developers understand what object is expected without tracing its origin. This follows the best practices outlined in PEP 484 for improving static analysis and code readability.

Suggested change
def refine_prompt(client, prompt):
def refine_prompt(client: "genai.Client", prompt: str) -> str:



def generate_image(api_key, prompt, output_path, input_image_path=None):
def generate_image(client, prompt, output_path, input_image_path=None):

Choose a reason for hiding this comment

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

medium

Similar to refine_prompt, please add type hints to this function's signature. This makes the function easier to use correctly, especially with the new client parameter, and improves overall code quality and maintainability as recommended by PEP 484.

Suggested change
def generate_image(client, prompt, output_path, input_image_path=None):
def generate_image(client: "genai.Client", prompt: str, output_path: str, input_image_path: str | None = None) -> str:

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