Skip to content

⚡ Bolt: [performance improvement] Optimize canvas buffer allocations - #6

Merged
sunalan2025 merged 1 commit into
mainfrom
bolt/canvas-buffer-optimization-2516103214454487876
Aug 3, 2026
Merged

⚡ Bolt: [performance improvement] Optimize canvas buffer allocations#6
sunalan2025 merged 1 commit into
mainfrom
bolt/canvas-buffer-optimization-2516103214454487876

Conversation

@sunalan2025

Copy link
Copy Markdown
Owner

💡 What: Modified PreviewCanvas.tsx to conditionally update canvas width and height only when the dimensions have actually changed. Included an explicit clearRect() when avoiding a resize, as the dimension reassignment typically handles the canvas clear implicitly.

🎯 Why: In HTML5 Canvas, re-assigning the width or height property (even to the same value) forces the browser to drop its underlying graphics buffer, re-allocate memory, and clear the pixels. This was happening on every re-render of the renderPipeline, leading to performance overhead (buffer reallocation) and potential layout thrashing when dragging or drawing annotations.

📊 Impact: Reduces expensive DOM operations and graphical memory re-allocations on every single render cycle by reusing the existing buffer when dimensions don't change.

🔬 Measurement: Verify performance by recording a CPU trace while drawing annotations or adjusting crops in the canvas. The rendering frame rate should be smoother without repeated allocations.


PR created automatically by Jules for task 2516103214454487876 started by @sunalan2025

    This commit updates `PreviewCanvas.tsx` to conditionally assign `canvas.width` and `canvas.height` only when they have changed. Re-assigning canvas dimensions clears the canvas and forces a slow reallocation of the underlying graphics buffer, which causes performance penalties and potential layout thrashing during mouse interactions and re-renders. A `clearRect` is included in cases where a resize and implicit clear don't occur, preventing visual ghosting artifacts.

Co-authored-by: sunalan2025 <255776802+sunalan2025@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.

Copilot AI review requested due to automatic review settings August 2, 2026 02:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This pull request optimizes PreviewCanvas.tsx canvas rendering by avoiding unnecessary canvas resizes (which force buffer reallocation and implicit clears), reducing per-frame overhead during interactive operations like drawing annotations and adjusting crops.

Changes:

  • Conditionally updates the stitched/base canvas dimensions only when the computed size changes.
  • Conditionally updates the display canvas dimensions only when needed, and adds an explicit clearRect() when reusing the existing buffer.
  • Adds a Bolt learning note documenting the canvas resize performance behavior and the intended mitigation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/components/PreviewCanvas.tsx Avoids redundant canvas resizes and explicitly clears when reusing the same canvas buffer to reduce per-render overhead.
.jules/bolt.md Documents the canvas resize performance pitfall and the optimization approach used in PreviewCanvas.tsx.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +327 to +328
const targetW = direction === 'vertical' ? totalW : totalH;
const targetH = direction === 'vertical' ? totalH : baseW;
@sunalan2025
sunalan2025 merged commit 593923f into main Aug 3, 2026
1 check passed
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.

2 participants