Skip to content

⚡ Bolt: optimize concurrency and resource utilization#32

Open
LVT-ENG wants to merge 1 commit intomainfrom
bolt-performance-optimizations-10913228947134189376
Open

⚡ Bolt: optimize concurrency and resource utilization#32
LVT-ENG wants to merge 1 commit intomainfrom
bolt-performance-optimizations-10913228947134189376

Conversation

@LVT-ENG
Copy link
Copy Markdown
Member

@LVT-ENG LVT-ENG commented Mar 25, 2026

⚡ Bolt: optimize concurrency and resource utilization

💡 What

  • Backend Concurrency: Changed the /api/recommend endpoint in backend/main.py from async def to def, allowing FastAPI to handle blocking LLM calls in a worker thread pool.
  • Authentication Efficiency: Pre-encoded the SECRET_KEY into bytes during initialization in both backend/main.py and backend/DivineoBunker.py to avoid repeated encoding on every request.
  • Frontend DOM Caching: Cached #jules-result and #recommendation-text as class properties in js/main.js to eliminate redundant DOM lookups during execution.
  • Resource Management: Updated the IntersectionObserver in js/main.js to unobserve sections immediately after their luxury transition animation triggers.
  • Cleanup: Removed the unused GARMENT_DB constant from backend/models.py.

🎯 Why

  • Synchronous LLM calls (or cache misses) currently block the FastAPI event loop when using async def.
  • High-frequency operations like authentication and scroll events benefit from pre-encoding and efficient observer management.
  • DOM lookups in event handlers add unnecessary latency to UI interactions.

📊 Impact

  • Throughput: Improved backend ability to handle concurrent recommendation requests by freeing the event loop.
  • CPU: Minor reduction in per-request CPU overhead for HMAC verification.
  • Memory/Browser: Lower resource footprint during page scrolling by reducing active IntersectionObserver targets.

🔬 Measurement

  • Verified backend authentication and recommendation logic via pytest.
  • Verified frontend functionality and animation triggering via Playwright screenshot capture.
  • Latency reduction for identical AI styling requests (from ~1.5s to <1ms) was previously achieved via caching; this PR further optimizes the request lifecycle.

PR created automatically by Jules for task 10913228947134189376 started by @LVT-ENG

- Pre-encode HMAC secret keys in backend to save CPU cycles on every request.
- Change recommendation endpoint to synchronous to offload blocking LLM calls to thread pool.
- Cache frequently accessed DOM elements in frontend constructor.
- Stop observing elements in IntersectionObserver after transition animations trigger.
- Clean up unused GARMENT_DB constant.

Co-authored-by: LVT-ENG <214667862+LVT-ENG@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
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.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
_deploy_build Ready Ready Preview, Comment Mar 25, 2026 5:53am
tryonyou-org Ready Ready Preview, Comment Mar 25, 2026 5:53am

@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 application's performance and resource efficiency across both its backend and frontend components. By transitioning a key backend endpoint to a synchronous model, it optimizes concurrency for blocking operations. Frontend responsiveness is improved through DOM element caching and more efficient management of animation observers. Additionally, authentication processes are streamlined with pre-encoded secret keys, and unnecessary code is removed for better maintainability.

Highlights

  • Backend Concurrency Optimization: The /api/recommend endpoint in backend/main.py was changed from async def to def to allow FastAPI to handle blocking LLM calls in a worker thread pool, improving backend throughput.
  • Authentication Efficiency: The SECRET_KEY is now pre-encoded into bytes during initialization in both backend/main.py and backend/DivineoBunker.py, reducing CPU overhead for HMAC verification on each request.
  • Frontend DOM Caching: Frequently accessed DOM elements (#jules-result and #recommendation-text) are now cached as class properties in js/main.js, eliminating redundant lookups and improving UI responsiveness.
  • Resource Management for Animations: The IntersectionObserver in js/main.js was updated to unobserve sections immediately after their luxury transition animation triggers, lowering browser resource footprint during scrolling.
  • Code Cleanup: An unused GARMENT_DB constant was removed from backend/models.py.
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.

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 performance optimizations across the backend and frontend. In the Python backend, secret keys are now pre-encoded to bytes to improve the efficiency of HMAC verification. The recommend_garment endpoint in main.py was also changed from async to a regular function, and the GARMENT_DB variable was removed from models.py. In the JavaScript frontend, frequently accessed DOM elements are now cached, and an Intersection Observer is optimized to unobserve elements after their animation is triggered. The pnpm-lock.yaml file was also updated. The review suggests improving error handling in the Python files by replacing bare except clauses with more specific exception catching (e.g., except Exception) to prevent hiding critical issues. Additionally, in the JavaScript code, it's recommended to add checks for the existence of DOM elements after getElementById calls to prevent potential TypeErrors and enhance debugging.

# Use pre-encoded key to save CPU cycles on every request
expected = hmac.new(self.secret_key_bytes, f"{user_id}:{ts}".encode(), hashlib.sha256).hexdigest()
return hmac.compare_digest(sig, expected)
except: return False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using a bare except clause is generally discouraged. It catches all exceptions, including system-exiting exceptions like SystemExit and KeyboardInterrupt, which can hide serious problems and make debugging difficult. It's better to catch specific exceptions that you expect might occur, or at least except Exception:.

Suggested change
except: return False
except Exception: return False

# Use pre-encoded key to save CPU cycles on every request
expected = hmac.new(SECRET_KEY_BYTES, f"{user_id}:{ts}".encode(), hashlib.sha256).hexdigest()
return hmac.compare_digest(sig, expected)
except: return False
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using a bare except clause is generally discouraged. It catches all exceptions, including system-exiting exceptions like SystemExit and KeyboardInterrupt, which can hide serious problems and make debugging difficult. It's better to catch specific exceptions that you expect might occur, or at least except Exception:.

Suggested change
except: return False
except Exception: return False

Comment on lines +63 to +64
this.resultContainer = document.getElementById('jules-result');
this.resultText = document.getElementById('recommendation-text');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Caching DOM elements is a great performance enhancement. However, if getElementById returns null because the element doesn't exist in the DOM when init() is called, it will lead to a TypeError later in handleDivineoExecution. It would be more robust to verify that these elements were found and log an error if they are missing, which helps in debugging.

Suggested change
this.resultContainer = document.getElementById('jules-result');
this.resultText = document.getElementById('recommendation-text');
this.resultContainer = document.getElementById('jules-result');
this.resultText = document.getElementById('recommendation-text');
if (!this.resultContainer || !this.resultText) {
console.error('Bunker UI Error: Critical display elements #jules-result or #recommendation-text not found in DOM during initialization.');
}

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