Skip to content

Conversation

@tamuratak
Copy link
Contributor

Optimize item height updates to reduce reflows and improve performance. Related to #246523.

This is one of several PRs aimed at improving smooth scrolling in the chat view, even when it contains many code blocks. updateItemHeight and updateItemHeightOnRender are one source of reflow. In this PR, reflow is reduced by deferring and batching the actual method calls to the next animation frame. There have been many complaints about scrolling in the chat view, so I think optimizations like this are well justified.

When this PR not merged:

itemheight_not_merged

When all PRs merged:

allthree_merged

Copilot AI review requested due to automatic review settings January 5, 2026 10:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes chat list rendering performance by batching height update operations to reduce reflows. Instead of immediately firing height change events or measuring heights synchronously, the changes are queued and processed in batched animation frames, which should improve scrolling performance when the chat view contains many code blocks.

Key changes:

  • Introduced deferred batching for height change events via pendingItemHeightChanges Map and animation frame scheduling
  • Introduced deferred batching for height measurements via pendingHeightUpdates Map and animation frame scheduling
  • Modified updateItemHeight and renderElement call sites to use the new queuing mechanisms instead of direct/immediate updates

const queued = Array.from(this.pendingHeightUpdates.entries());
this.pendingHeightUpdates.clear();
for (const [template, queuedElement] of queued) {
this.updateItemHeightOnRender(queuedElement, template);
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

There's a potential issue where templates stored in pendingHeightUpdates may be disposed or reused for different elements before the animation frame callback executes. When updateItemHeightOnRender is called with these stale templates at line 982, it may measure the wrong element's height or access a template that's been recycled for a different element. Consider checking if template.currentElement === queuedElement before calling updateItemHeightOnRender to ensure the template still corresponds to the queued element.

Suggested change
this.updateItemHeightOnRender(queuedElement, template);
// Templates may have been recycled or disposed since they were queued.
// Ensure the template still corresponds to the element we queued it for.
if (template.currentElement === queuedElement && template.rowContainer.isConnected) {
this.updateItemHeightOnRender(queuedElement, template);
}

Copilot uses AI. Check for mistakes.
Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I think this is basically good but we are trying to cut a release this week and I'm not brave enough to merge this for this release. I'm worried about the impact of delaying the layouts by a frame. I see this

Image

if I close the session, reload the window, and reopen the chat session. I can debug it and merge it later.

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