gpui: Reduce window.refresh to improve cache hit of the cached views (#25009)

Release Notes:

- Improved performance when using the scroll wheel and some other mouse
interactions.

Based on some cache details about GPUI `AnyView::cached` that I found in
the discussion of
https://github.com/zed-industries/zed/discussions/24260#discussioncomment-12135749,
and combined with the optimization points found in actual applications.

This change may have some scenarios that I have not considered, so I
just make a draft to put forward my ideas first for discussion.

From my analysis, `AnyView::cached` will always invalid by Div's mouse
events, because of it called `window.refresh`. I understand that (mouse
move event) this is because the interface changes related to hover and
mouse_move will be affected style, so `window.refresh` is required.
Since Div does not have the `entity_id` of View, it is impossible to
know which View should be refreshed, so the entire window can only be
refreshed.

With this change, we can reduce a lot of `render` method calls on
ScrollWheel or Mouse Event.
This commit is contained in:
Jason Lee 2025-03-19 05:52:20 +08:00 committed by GitHub
parent 89ae4ca9a3
commit 985ac4e5f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 22 deletions

View file

@ -9,8 +9,8 @@
use crate::{
point, px, size, AnyElement, App, AvailableSpace, Bounds, ContentMask, DispatchPhase, Edges,
Element, FocusHandle, GlobalElementId, Hitbox, IntoElement, Pixels, Point, ScrollWheelEvent,
Size, Style, StyleRefinement, Styled, Window,
Element, EntityId, FocusHandle, GlobalElementId, Hitbox, IntoElement, Pixels, Point,
ScrollWheelEvent, Size, Style, StyleRefinement, Styled, Window,
};
use collections::VecDeque;
use refineable::Refineable as _;
@ -371,6 +371,7 @@ impl StateInner {
scroll_top: &ListOffset,
height: Pixels,
delta: Point<Pixels>,
current_view: EntityId,
window: &mut Window,
cx: &mut App,
) {
@ -413,7 +414,7 @@ impl StateInner {
);
}
window.refresh();
cx.notify(current_view);
}
fn logical_scroll_top(&self) -> ListOffset {
@ -847,6 +848,7 @@ impl Element for List {
window: &mut Window,
cx: &mut App,
) {
let current_view = window.current_view();
window.with_content_mask(Some(ContentMask { bounds }), |window| {
for item in &mut prepaint.layout.item_layouts {
item.element.paint(window, cx);
@ -863,6 +865,7 @@ impl Element for List {
&scroll_top,
height,
event.delta.pixel_delta(px(20.)),
current_view,
window,
cx,
)
@ -967,7 +970,10 @@ mod test {
#[gpui::test]
fn test_reset_after_paint_before_scroll(cx: &mut TestAppContext) {
use crate::{div, list, point, px, size, Element, ListState, Styled};
use crate::{
div, list, point, px, size, AppContext, Context, Element, IntoElement, ListState,
Render, Styled, Window,
};
let cx = cx.add_empty_window();
@ -981,9 +987,16 @@ mod test {
offset_in_item: px(0.0),
});
struct TestView(ListState);
impl Render for TestView {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
list(self.0.clone()).w_full().h_full()
}
}
// Paint
cx.draw(point(px(0.), px(0.)), size(px(100.), px(20.)), |_, _| {
list(state.clone()).w_full().h_full()
cx.draw(point(px(0.), px(0.)), size(px(100.), px(20.)), |_, cx| {
cx.new(|_| TestView(state.clone()))
});
// Reset