Visual Studio Code, rebuilt in Rust
Rust
perf(workbench): redraw only the view whose labels or rows asked
rokarturea74d1d699af
diff --git a/native/crates/app/src/workbench.rs b/native/crates/app/src/workbench.rsindex d769e3f952e..6fb61d88712 100644--- a/native/crates/app/src/workbench.rs+++ b/native/crates/app/src/workbench.rs@@ -686,6 +686,8 @@ impl Render for Workbench {let accessible_view = self.accessible_view_widget(window, cx);self.schedule_menubar_update(window, cx);self.with_group(main_group, |workbench| workbench.sync_window_title(window));+ // The labels drawn above asked for the decorations they lack.+ self.send_decoration_requests(window.current_view(), cx);div().relative().size_full()diff --git a/native/crates/app/src/workbench/auxiliary_windows.rs b/native/crates/app/src/workbench/auxiliary_windows.rsindex a7956ad608b..6b75eb87b4b 100644--- a/native/crates/app/src/workbench/auxiliary_windows.rs+++ b/native/crates/app/src/workbench/auxiliary_windows.rs@@ -455,6 +455,8 @@ impl Workbench {true => self.window_overlays(window, cx),false => Vec::new(),};+ // The labels drawn above asked for the decorations they lack.+ self.send_decoration_requests(window.current_view(), cx);div().relative().size_full()diff --git a/native/crates/app/src/workbench/extensions_list_view.rs b/native/crates/app/src/workbench/extensions_list_view.rsindex 96ded694e8d..3ee5ea7310e 100644--- a/native/crates/app/src/workbench/extensions_list_view.rs+++ b/native/crates/app/src/workbench/extensions_list_view.rs@@ -215,7 +215,8 @@ impl Workbench {count,cx.processor(move |workbench, range: std::ops::Range<usize>, window, cx| {let end = range.end;- let rows: Vec<AnyElement> = range.map(|row| workbench.extension_row(index, row, list_focused, cx)).collect();+ let view = window.current_view();+ let rows: Vec<AnyElement> = range.map(|row| workbench.extension_row(index, row, list_focused, view, cx)).collect();if end + 5 >= workbench.extensions_viewlet.panes[index].extensions.len() {workbench.load_next_extensions_page(index, window, cx);}@@ -228,11 +229,11 @@ impl Workbench {}/// extensionsList.ts `Renderer`: the row of one extension.- fn extension_row(&mut self, pane_index: usize, row: usize, list_focused: bool, cx: &mut Context<Self>) -> AnyElement {+ fn extension_row(&mut self, pane_index: usize, row: usize, list_focused: bool, view: gpui::EntityId, cx: &mut Context<Self>) -> AnyElement {let extension = self.extensions_viewlet.panes[pane_index].extensions[row].clone();let icon_url = extension.gallery.as_ref().and_then(|gallery| gallery.icon.as_ref()).map(|icon| icon.uri.clone());if let (None, Some(url)) = (extension_local_icon(&extension), &icon_url) {- self.request_extension_icon(url, cx);+ self.request_extension_icon(url, view, cx);}let key = (pane_index, row);let viewlet = &self.extensions_viewlet;diff --git a/native/crates/app/src/workbench/extensions_view.rs b/native/crates/app/src/workbench/extensions_view.rsindex dbbe01911d1..8c3f5be07d9 100644--- a/native/crates/app/src/workbench/extensions_view.rs+++ b/native/crates/app/src/workbench/extensions_view.rs@@ -10,7 +10,7 @@ use std::time::Duration;use exthost::extension_management::{Job, UserExtensions, add_user_extensions};use exthost::gallery::{ExtensionInfo, Gallery, GalleryExtension, QueryOptions};use exthost::scan::ScannedExtension;-use gpui::{Context, FocusHandle, Image, ImageFormat, Task, UniformListScrollHandle, Window};+use gpui::{App, Context, FocusHandle, Image, ImageFormat, Task, UniformListScrollHandle, Window};use serde_json::{Value, json};use super::Workbench;@@ -1217,8 +1217,8 @@ impl Workbench {Some((count, message))}- /// Downloads a gallery icon once; the row shows it when it arrives.- pub(super) fn request_extension_icon(&mut self, url: &str, cx: &mut Context<Self>) {+ /// Downloads a gallery icon once; the row shows it when it arrives, drawn again by `view`.+ pub(super) fn request_extension_icon(&mut self, url: &str, view: gpui::EntityId, cx: &mut Context<Self>) {if self.extensions_viewlet.icons.contains_key(url) {return;}@@ -1237,7 +1237,7 @@ impl Workbench {let _ = workbench.update(cx, |workbench, cx| {let image = bytes.ok().and_then(|bytes| image_format(&bytes).map(|format| Arc::new(Image::from_bytes(format, bytes))));workbench.extensions_viewlet.icons.insert(url, image);- cx.notify();+ App::notify(cx, view);});}).detach();diff --git a/native/crates/app/src/workbench/extensions_view/tests.rs b/native/crates/app/src/workbench/extensions_view/tests.rsindex ca254897da7..dba0dc293fd 100644--- a/native/crates/app/src/workbench/extensions_view/tests.rs+++ b/native/crates/app/src/workbench/extensions_view/tests.rs@@ -77,3 +77,40 @@ fn the_views_are_panes_and_an_empty_outdated_view_collapses(cx: &mut gpui::TestAlet state: serde_json::Value = serde_json::from_str(&state.expect("the click stores the views' state")).unwrap();assert_eq!(state["workbench.views.extensions.searchOutdated"]["collapsed"], serde_json::json!(false));}++/// A gallery icon that arrives redraws the view its row drew in (the side bar's), not the Workbench and with it+/// every part.+#[gpui::test]+fn an_arriving_icon_redraws_the_rows_view_alone(cx: &mut gpui::TestAppContext) {+ use std::io::{Read, Write};+ let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();+ let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();+ let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();+ let (workbench, cx) = cx.add_window_view(|_, cx| {+ let mut workbench = Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx);+ workbench.set_part_hidden(crate::workbench::layout::Part::SideBar, false, cx);+ workbench+ });+ cx.update(|window, cx| drop(window.draw(cx)));+ cx.run_until_parked();+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ let url = format!("http://{}/icon.png", listener.local_addr().unwrap());+ let server = std::thread::spawn(move || {+ let (mut stream, _) = listener.accept().unwrap();+ let _ = stream.read(&mut [0; 1024]).unwrap();+ stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").unwrap();+ });+ let side_bar = workbench.read_with(cx, |workbench, _| workbench.part_view_entity(crate::workbench::part_view::CachedPart::Grid(crate::workbench::layout::Part::SideBar)).unwrap().clone());+ let (workbench_notified, side_bar_notified) = (std::rc::Rc::new(std::cell::Cell::new(0)), std::rc::Rc::new(std::cell::Cell::new(0)));+ let _observers = cx.update(|_, cx| {+ let (workbench_seen, side_bar_seen) = (workbench_notified.clone(), side_bar_notified.clone());+ (cx.observe(&workbench, move |_, _| workbench_seen.set(workbench_seen.get() + 1)), cx.observe(&side_bar, move |_, _| side_bar_seen.set(side_bar_seen.get() + 1)))+ });++ workbench.update(cx, |workbench, cx| workbench.request_extension_icon(&url, side_bar.entity_id(), cx));+ cx.run_until_parked();+ server.join().unwrap();+ assert!(workbench.read_with(cx, |workbench, _| workbench.extensions_viewlet.icons.contains_key(&url)));+ assert_eq!(side_bar_notified.get(), 1, "the row's view draws the icon");+ assert_eq!(workbench_notified.get(), 0, "the workbench stays unnotified");+}diff --git a/native/crates/app/src/workbench/file_decorations.rs b/native/crates/app/src/workbench/file_decorations.rsindex 3e61631aa85..61e8c56c3d1 100644--- a/native/crates/app/src/workbench/file_decorations.rs+++ b/native/crates/app/src/workbench/file_decorations.rs@@ -3,7 +3,7 @@use std::path::Path;-use gpui::{Context, Div, ParentElement, Rgba, Styled, div, px};+use gpui::{App, Context, Div, EntityId, ParentElement, Rgba, Styled, div, px};use serde_json::{Value, json};use super::Workbench;@@ -28,14 +28,14 @@ impl Workbench {_ => {}}drop(decorations);- self.send_decoration_requests(cx);+ self.send_decoration_requests(cx.entity_id(), cx);cx.notify();Outcome::done()}/// `DecorationRequestsQueue`: the resources labels asked for since the last frame, one `$provideDecorations`- /// per provider.- pub(super) fn send_decoration_requests(&mut self, cx: &mut Context<Self>) {+ /// per provider. The answer redraws `view`, the one whose labels asked.+ pub(super) fn send_decoration_requests(&mut self, view: EntityId, cx: &mut Context<Self>) {let requests = self.decorations.borrow_mut().take_requests();for (handle, requests) in requests {let dtos: Vec<Value> = requests.iter().map(|(id, path)| json!({ "id": id, "uri": { "$mid": 1, "scheme": "file", "path": path } })).collect();@@ -44,7 +44,7 @@ impl Workbench {let Ok(Ok(reply)) = reply.await else { return };let _ = workbench.update(cx, |workbench, cx| {workbench.decorations.borrow_mut().answered(handle, &requests, &reply);- cx.notify();+ App::notify(cx, view);});}).detach();diff --git a/native/crates/app/src/workbench/file_decorations/tests.rs b/native/crates/app/src/workbench/file_decorations/tests.rsindex 4d6c96b8d1d..8e0d1293fe0 100644--- a/native/crates/app/src/workbench/file_decorations/tests.rs+++ b/native/crates/app/src/workbench/file_decorations/tests.rs@@ -1,4 +1,6 @@+use std::cell::Cell;use std::path::Path;+use std::rc::Rc;use config::workspace::Workspace;use exthost::scan::scan_extension;@@ -10,6 +12,8 @@ use crate::host_sync::HostSync;use crate::languages::Languages;use crate::theme::load_theme;use crate::workbench::Workbench;+use crate::workbench::layout::Part;+use crate::workbench::part_view::CachedPart;#[gpui::test]fn explorer_rows_ask_each_provider_once_and_show_the_answer(cx: &mut TestAppContext) {@@ -55,6 +59,49 @@ fn explorer_rows_ask_each_provider_once_and_show_the_answer(cx: &mut TestAppContassert_eq!(letter.as_deref(), Some("F"));}+/// A decoration answer redraws the labels that asked (decorationsService.ts `onDidChangeDecorations`): the side+/// bar's view, not the Workbench and with it every part.+#[gpui::test]+fn an_answer_redraws_the_side_bar_alone(cx: &mut TestAppContext) {+ let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();+ let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();+ let dir = std::env::temp_dir().join(format!("vsrustium-decorations-redraw-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(&dir).unwrap();+ std::fs::write(dir.join("a.ts"), "").unwrap();+ let dir = dir.canonicalize().unwrap();+ let (sender, receiver) = std::sync::mpsc::channel();+ let folder = Workspace::folder(dir.clone());+ let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root, Some(folder), Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx));+ let draw = |cx: &mut gpui::VisualTestContext| {+ cx.update(|window, cx| drop(window.draw(cx)));+ cx.run_until_parked();+ };+ drop(workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("workbench.view.explorer", Vec::new(), window, cx)));+ draw(cx);+ let register = ForwardedRequest { req: 1, proxy: "MainThreadDecorations", method: "$registerDecorationProvider".into(), args: vec![json!(7), json!("Git")] };+ drop(workbench.update_in(cx, |workbench, window, cx| workbench.handle_host_request(register, window, cx)));+ draw(cx);+ let side_bar_renders = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, cx| workbench.part_view_entity(CachedPart::Grid(Part::SideBar)).unwrap().read(cx).renders);+ let before = side_bar_renders(cx);+ let notified = Rc::new(Cell::new(0));+ let _observer = {+ let notified = notified.clone();+ cx.update(|_, cx| cx.observe(&workbench, move |_, _| notified.set(notified.get() + 1)))+ };++ let request = receiver.try_iter().find(|message| message.method == "$provideDecorations").expect("the row asked");+ let id = request.args[1][0]["id"].to_string();+ let reply = serde_json::Map::from_iter([(id, json!([false, "Modified", "M", null]))]);+ let _ = request.reply.unwrap().send(Ok(Value::Object(reply)));+ draw(cx);+ assert_eq!(notified.get(), 0, "the workbench stays unnotified");+ assert!(side_bar_renders(cx) > before, "the side bar draws the answer");+ let letter = workbench.read_with(cx, |workbench, _| workbench.file_decoration(&dir.join("a.ts"), false).and_then(|decoration| decoration.letter));+ assert_eq!(letter.as_deref(), Some("M"));+ std::fs::remove_dir_all(dir).unwrap();+}+#[gpui::test]fn problems_decorate_the_file_before_other_providers_and_bubble_to_its_folder(cx: &mut TestAppContext) {let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();diff --git a/native/crates/app/src/workbench/part_grid.rs b/native/crates/app/src/workbench/part_grid.rsindex d9d8c15e5ee..8e6f9ec0841 100644--- a/native/crates/app/src/workbench/part_grid.rs+++ b/native/crates/app/src/workbench/part_grid.rs@@ -528,8 +528,6 @@ impl Workbench {.children(sashes.into_iter().map(|(sash, state, bounds)| self.part_sash(sash, state, bounds, cx))).when(dragging, |section| section.child(self.part_sash_drag(cx))).child(self.gripper_activity(cx));- // The labels drawn above asked for the decorations they lack.- self.send_decoration_requests(cx);section}diff --git a/native/crates/app/src/workbench/part_view.rs b/native/crates/app/src/workbench/part_view.rsindex 5b53bb691f2..a3d52b4c0f5 100644--- a/native/crates/app/src/workbench/part_view.rs+++ b/native/crates/app/src/workbench/part_view.rs@@ -106,7 +106,7 @@ impl Workbench {_ => unreachable!("{part:?} is drawn by the workbench"),};// The labels drawn above asked for the decorations they lack.- self.send_decoration_requests(cx);+ self.send_decoration_requests(window.current_view(), cx);content}}