Skip to content

Avoid unnecessary clone in MatrixHtmlSpan event handler#700

Merged
kevinaboos merged 2 commits intomainfrom
perf-html-span-event-loop-4434835930463611105
Feb 6, 2026
Merged

Avoid unnecessary clone in MatrixHtmlSpan event handler#700
kevinaboos merged 2 commits intomainfrom
perf-html-span-event-loop-4434835930463611105

Conversation

@kevinaboos
Copy link
Member

This PR optimizes the MatrixHtmlSpan::handle_event method in src/shared/html_or_plaintext.rs.

Optimization:

  • Removed self.drawn_areas.clone().into_iter().
  • Implemented an index-based loop: for i in 0..self.drawn_areas.len().

Rationale:

  • The original code cloned the SmallVec to avoid borrow checker conflicts when mutating self inside the loop.
  • The new approach uses indexing to access Area (which is Copy). This accesses self only temporarily during indexing, releasing the borrow before the loop body mutates self.
  • This avoids unnecessary memory allocation/copying in a high-frequency event handler.

Verification:

  • Verified borrow checker safety using a reproduction script (repro_borrow.rs).
  • Verified performance improvement using a benchmark script (repro_perf.rs), showing ~3x improvement for small vectors.
  • Logic remains functionally equivalent.

PR created automatically by Jules for task 4434835930463611105 started by @kevinaboos

Replaced `self.drawn_areas.clone().into_iter()` with an index-based loop `0..self.drawn_areas.len()`.
This avoids allocating a temporary vector (heap or stack) during every event handling call, improving performance in the hot path.
The index-based loop satisfies the borrow checker by keeping the borrow of `self` (for indexing) separate from the mutation of `self` in the loop body.
Verified with isolated reproduction scripts to ensure correctness and performance gains (approx 3x faster for inline vectors).

Co-authored-by: kevinaboos <1139460+kevinaboos@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.

@kevinaboos kevinaboos changed the title ⚡ Optimize MatrixHtmlSpan event loop to avoid vector clone Avoid unnecessary clone in MatrixHtmlSpan event handler Feb 6, 2026
@kevinaboos kevinaboos merged commit e3cb7f1 into main Feb 6, 2026
10 checks passed
@kevinaboos kevinaboos deleted the perf-html-span-event-loop-4434835930463611105 branch February 6, 2026 00:53
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