⚡ Bolt: [performance improvement] Optimize canvas buffer allocations - #6
Conversation
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>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
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.
| const targetW = direction === 'vertical' ? totalW : totalH; | ||
| const targetH = direction === 'vertical' ? totalH : baseW; |
💡 What: Modified
PreviewCanvas.tsxto conditionally update canvas width and height only when the dimensions have actually changed. Included an explicitclearRect()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