Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Add hot reloading using Subsecond (#65738)

Anthony Eid670a779a5ed9
diff --git a/Cargo.lock b/Cargo.lock
index 00bb83e506d..fc1be194bae 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5022,6 +5022,51 @@ dependencies = [
"subtle",
]
+[[package]]
+name = "dioxus-cli-config"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "322d10f47effebd85ee0662a2cb083ff920d40dd6295a4afd8cfa4f9bc2e05c6"
+
+[[package]]
+name = "dioxus-core"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b232228ada232c0adc151e41ebd9ef70cc04683db097fa0dc6015979fa01722"
+dependencies = [
+ "anyhow",
+ "const_format",
+ "dioxus-core-types",
+ "futures-channel",
+ "futures-util",
+ "generational-box",
+ "longest-increasing-subsequence",
+ "rustc-hash 2.1.1",
+ "rustversion",
+ "serde",
+ "slab",
+ "slotmap",
+ "subsecond",
+ "tracing",
+]
+
+[[package]]
+name = "dioxus-core-types"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96007ed6cbad951dfed82d83c581aabd95bc591f351f5666d454ddbe7845b324"
+
+[[package]]
+name = "dioxus-devtools-types"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dfd6f3475e38c93be245a664a9ce11679a8f2b99832620705cf7837f42aa39ce"
+dependencies = [
+ "dioxus-core",
+ "serde",
+ "subsecond-types",
+]
+
[[package]]
name = "dirs"
version = "6.0.0"
@@ -6861,6 +6906,16 @@ dependencies = [
"triomphe",
]
+[[package]]
+name = "generational-box"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8b2dc9b873cd1a8adb80aa5dcef9a8205f781a8a313d5ffb867248cb3bcb764"
+dependencies = [
+ "parking_lot",
+ "tracing",
+]
+
[[package]]
name = "generator"
version = "0.8.9"
@@ -7364,6 +7419,7 @@ dependencies = [
"anyhow",
"async-channel 2.5.0",
"async-task",
+ "async-tungstenite",
"backtrace",
"bench_metrics",
"bitflags 2.13.1",
@@ -7372,6 +7428,8 @@ dependencies = [
"criterion",
"ctor",
"derive_more",
+ "dioxus-cli-config",
+ "dioxus-devtools-types",
"embed-resource",
"env_logger 0.11.8",
"etagere",
@@ -7416,9 +7474,11 @@ dependencies = [
"serde_json",
"slotmap",
"smallvec",
+ "smol",
"spin 0.10.0",
"stacksafe",
"strum 0.28.0",
+ "subsecond",
"sum_tree",
"taffy",
"thiserror 2.0.17",
@@ -10296,6 +10356,12 @@ dependencies = [
"thiserror 2.0.17",
]
+[[package]]
+name = "longest-increasing-subsequence"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86"
+
[[package]]
name = "loom"
version = "0.7.2"
@@ -16818,6 +16884,7 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbff4acf519f630b3a3ddcfaea6c06b42174d9a44bc70c620e9ed1649d58b82a"
dependencies = [
+ "serde",
"version_check",
]
@@ -17402,6 +17469,34 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "subsecond"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d350d5788fa94d560d92269266a50efc77b036bfca6675e420b64df7b3211f37"
+dependencies = [
+ "js-sys",
+ "libc",
+ "libloading",
+ "memfd",
+ "memmap2",
+ "serde",
+ "subsecond-types",
+ "thiserror 2.0.17",
+ "wasm-bindgen",
+ "wasm-bindgen-futures",
+ "web-sys",
+]
+
+[[package]]
+name = "subsecond-types"
+version = "0.7.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dcf32d66269b5fbb8558334e8d22b657b2f7af0dd2ef56c231541099a862e464"
+dependencies = [
+ "serde",
+]
+
[[package]]
name = "subtle"
version = "2.6.1"
diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml
index f6616c8c743..a46c580fc1d 100644
--- a/crates/gpui/Cargo.toml
+++ b/crates/gpui/Cargo.toml
@@ -47,6 +47,13 @@ screen-capture = [
windows-manifest = ["dep:embed-resource"]
profiler = ["dep:hdrhistogram"]
stacker = ["dep:stacksafe"]
+hot-reload = [
+ "dep:async-tungstenite",
+ "dep:dioxus-cli-config",
+ "dep:dioxus-devtools-types",
+ "dep:smol",
+ "dep:subsecond",
+]
[lib]
path = "src/gpui.rs"
@@ -127,6 +134,13 @@ async-channel = { workspace = true, features = ["std"] }
[target.'cfg(not(target_family = "wasm"))'.dependencies]
pollster.workspace = true
+[target.'cfg(any(target_os = "macos", target_os = "linux", target_os = "windows", target_os = "ios"))'.dependencies]
+async-tungstenite = { workspace = true, optional = true }
+dioxus-cli-config = { version = "=0.7.10", optional = true }
+dioxus-devtools-types = { version = "=0.7.10", optional = true }
+smol = { workspace = true, optional = true }
+subsecond = { version = "=0.7.10", optional = true }
+
[target.'cfg(target_family = "wasm")'.dependencies]
getrandom = { version = "0.3.4", features = ["wasm_js"] }
uuid = { workspace = true, features = ["js"] }
diff --git a/crates/gpui/build.rs b/crates/gpui/build.rs
index a55b3b1cb32..3fdbb0f06e6 100644
--- a/crates/gpui/build.rs
+++ b/crates/gpui/build.rs
@@ -18,6 +18,16 @@ fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
+ // Hot reloading is limited to debug builds so the feature can't ship in release binaries,
+ // and to the platforms whose patch transport is implemented.
+ println!("cargo::rustc-check-cfg=cfg(gpui_hot_reload)");
+ if std::env::var_os("CARGO_FEATURE_HOT_RELOAD").is_some()
+ && std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some()
+ && matches!(target_os.as_str(), "macos" | "linux" | "windows" | "ios")
+ {
+ println!("cargo::rustc-cfg=gpui_hot_reload");
+ }
+
if target_os == "windows" {
#[cfg(feature = "windows-manifest")]
embed_resource();
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs
index d26e92f4e3d..722a44b667d 100644
--- a/crates/gpui/src/app.rs
+++ b/crates/gpui/src/app.rs
@@ -256,6 +256,8 @@ impl Application {
let cx = &mut *this.borrow_mut();
// Some platforms only connect to their display server in `run`.
cx.displays_changed();
+ #[cfg(gpui_hot_reload)]
+ crate::hot_reload::init(cx);
on_finish_launching(cx);
}));
@@ -282,6 +284,8 @@ impl Application {
let cx = &mut *this.borrow_mut();
// Some platforms only connect to their display server in `run`.
cx.displays_changed();
+ #[cfg(gpui_hot_reload)]
+ crate::hot_reload::init(cx);
on_finish_launching(cx);
}));
ApplicationHandle { app: self.0 }
diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs
index d4da5aa34c0..2328e954df3 100644
--- a/crates/gpui/src/gpui.rs
+++ b/crates/gpui/src/gpui.rs
@@ -26,6 +26,8 @@ pub(crate) use platform_scheduler::PlatformScheduler;
mod geometry;
mod gestures;
mod global;
+#[cfg(gpui_hot_reload)]
+mod hot_reload;
mod input;
mod inspector;
mod interactive;
@@ -203,6 +205,17 @@ use std::{any::Any, future::Future};
pub use style::*;
pub use styled::*;
pub use subscription::*;
+/// Subsecond entry points for hot-patching callbacks outside GPUI's render boundaries.
+#[cfg(all(
+ feature = "hot-reload",
+ any(
+ target_os = "macos",
+ target_os = "linux",
+ target_os = "windows",
+ target_os = "ios"
+ )
+))]
+pub use subsecond;
pub use svg_renderer::*;
pub(crate) use tab_stop::*;
use taffy::TaffyLayoutEngine;
diff --git a/crates/gpui/src/hot_reload.rs b/crates/gpui/src/hot_reload.rs
new file mode 100644
index 00000000000..e92c6254bbf
--- /dev/null
+++ b/crates/gpui/src/hot_reload.rs
@@ -0,0 +1,248 @@
+use crate::{App, AppContext as _, Global, Task};
+use anyhow::{Context as _, Result};
+use async_tungstenite::tungstenite::Message;
+use dioxus_devtools_types::DevserverMsg;
+use futures::StreamExt;
+use std::net::SocketAddr;
+use subsecond::JumpTable;
+
+struct HotReload {
+ _task: Task<()>,
+}
+
+impl Global for HotReload {}
+
+pub(crate) fn init(cx: &mut App) {
+ if cx.has_global::<HotReload>() {
+ return;
+ }
+ let (Some(address), Some(endpoint)) = (
+ dioxus_cli_config::devserver_raw_addr(),
+ dioxus_cli_config::devserver_ws_endpoint(),
+ ) else {
+ log::warn!(
+ "GPUI hot reload is enabled, but no dx development server was found. \
+ Launch the app with `dx serve --hot-patch` to enable hot reloading."
+ );
+ return;
+ };
+
+ let build_id = dioxus_cli_config::build_id();
+ let process_id = std::process::id();
+ // Resolve the executable anchor on the foreground thread before any patch can load.
+ let reference = subsecond::aslr_reference();
+ let endpoint =
+ format!("{endpoint}?aslr_reference={reference}&build_id={build_id}&pid={process_id}");
+ let (sender, receiver) = async_channel::bounded(16);
+ let connection = cx.background_spawn(async move {
+ if let Err(error) = receive_patches(address, endpoint, build_id, process_id, sender).await {
+ log::error!("GPUI hot reload disconnected: {error:#}. Restart dx serve to reconnect.");
+ }
+ });
+ let task = cx.spawn(async move |cx| {
+ let _connection = connection;
+ while let Ok(table) = receiver.recv().await {
+ cx.update(|cx| {
+ // Patches are executable code from the developer's trusted dx server. Applying
+ // them between foreground updates avoids replacing code during a render.
+ match unsafe { subsecond::apply_patch(table) } {
+ Ok(()) => {
+ cx.refresh_windows();
+ log::info!("GPUI hot reload applied");
+ }
+ Err(error) => log::error!("GPUI hot reload failed: {error}"),
+ }
+ });
+ }
+ });
+ cx.set_global(HotReload { _task: task });
+}
+
+async fn receive_patches(
+ address: SocketAddr,
+ endpoint: String,
+ build_id: u64,
+ process_id: u32,
+ sender: async_channel::Sender<JumpTable>,
+) -> Result<()> {
+ let stream = smol::net::TcpStream::connect(address)
+ .await
+ .context("connecting to the dx development server")?;
+ let (mut websocket, _) = async_tungstenite::client_async(endpoint, stream)
+ .await
+ .context("opening the dx development websocket")?;
+ log::info!("GPUI hot reload connected");
+
+ while let Some(message) = websocket.next().await {
+ match message.context("reading the dx development websocket")? {
+ Message::Text(text) => {
+ let message = serde_json::from_str::<DevserverMsg>(&text)
+ .context("decoding a dx development message")?;
+ if let Some(table) = patch_for_process(message, build_id, process_id) {
+ if sender.send(table).await.is_err() {
+ return Ok(());
+ }
+ }
+ }
+ Message::Close(_) => break,
+ _ => {}
+ }
+ }
+ log::info!("GPUI hot reload disconnected");
+ Ok(())
+}
+
+fn patch_for_process(message: DevserverMsg, build_id: u64, process_id: u32) -> Option<JumpTable> {
+ match message {
+ DevserverMsg::HotReload(message)
+ if message.for_build_id == Some(build_id) && message.for_pid == Some(process_id) =>
+ {
+ message.jump_table
+ }
+ DevserverMsg::FullReloadCommand => {
+ log::warn!("GPUI hot reload requires a full application restart");
+ None
+ }
+ _ => None,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{
+ AnyView, Context, Entity, IntoElement, Render, RenderOnce, StyleRefinement, TestAppContext,
+ ViewElement, Window, div, prelude::*,
+ };
+ use dioxus_devtools_types::HotReloadMsg;
+ use std::{cell::Cell, rc::Rc};
+
+ fn message(build_id: Option<u64>, process_id: Option<u32>) -> DevserverMsg {
+ DevserverMsg::HotReload(HotReloadMsg {
+ for_build_id: build_id,
+ for_pid: process_id,
+ jump_table: Some(JumpTable {
+ lib: "unused-test-patch".into(),
+ map: Default::default(),
+ aslr_reference: 0,
+ new_base_address: 0,
+ ifunc_count: 0,
+ }),
+ ..Default::default()
+ })
+ }
+
+ #[test]
+ fn receives_only_matching_patches_over_the_websocket() -> Result<()> {
+ smol::block_on(async {
+ let listener = smol::net::TcpListener::bind("127.0.0.1:0").await?;
+ let address = listener.local_addr()?;
+ let (sender, receiver) = async_channel::bounded(16);
+ let server = async {
+ let (stream, _) = listener.accept().await?;
+ let mut websocket = async_tungstenite::accept_async(stream).await?;
+ for message in [
+ DevserverMsg::HotPatchStart,
+ DevserverMsg::HotReload(HotReloadMsg::default()),
+ message(Some(3), Some(2)),
+ message(Some(1), Some(3)),
+ message(None, Some(2)),
+ message(Some(1), None),
+ message(Some(1), Some(2)),
+ ] {
+ websocket
+ .send(Message::Text(serde_json::to_string(&message)?.into()))
+ .await?;
+ }
+ websocket.close(None).await?;
+ anyhow::Ok(())
+ };
+ futures::try_join!(
+ server,
+ receive_patches(address, format!("ws://{address}/_dioxus"), 1, 2, sender)
+ )?;
+ assert_eq!(
+ receiver.recv().await?.lib,
+ std::path::Path::new("unused-test-patch")
+ );
+ assert!(receiver.recv().await.is_err());
+ Ok(())
+ })
+ }
+
+ struct Child {
+ count: usize,
+ renders: Rc<Cell<usize>>,
+ }
+
+ impl Render for Child {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ self.renders.set(self.renders.get() + 1);
+ div()
+ }
+ }
+
+ struct Component(Rc<Cell<usize>>);
+
+ impl RenderOnce for Component {
+ fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
+ self.0.set(self.0.get() + 1);
+ div()
+ }
+ }
+
+ impl IntoElement for Component {
+ type Element = ViewElement<Self>;
+
+ fn into_element(self) -> Self::Element {
+ ViewElement::new(self)
+ }
+ }
+
+ struct Root {
+ child: Entity<Child>,
+ erased: bool,
+ component_renders: Rc<Cell<usize>>,
+ }
+
+ impl Render for Root {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ let element = div().child(Component(self.component_renders.clone()));
+ if self.erased {
+ element.child(AnyView::from(self.child.clone()).cached(StyleRefinement::default()))
+ } else {
+ element.child(self.child.clone().cached(StyleRefinement::default()))
+ }
+ }
+ }
+
+ #[gpui::test]
+ fn refresh_renders_cached_views_without_recreating_state(cx: &mut TestAppContext) {
+ for erased in [false, true] {
+ let renders = Rc::new(Cell::new(0));
+ let component_renders = Rc::new(Cell::new(0));
+ let child = cx.new(|_| Child {
+ count: 7,
+ renders: renders.clone(),
+ });
+ let window = cx.add_window(|_, _| Root {
+ child: child.clone(),
+ erased,
+ component_renders: component_renders.clone(),
+ });
+ cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx))
+ .unwrap();
+ let initial_renders = renders.get();
+ let initial_component_renders = component_renders.get();
+ assert!(initial_renders > 0);
+ assert!(initial_component_renders > 0);
+
+ cx.update(|cx| cx.refresh_windows());
+ cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx))
+ .unwrap();
+ assert!(renders.get() > initial_renders);
+ assert!(component_renders.get() > initial_component_renders);
+ assert_eq!(child.read_with(cx, |child, _| child.count), 7);
+ }
+ }
+}
diff --git a/crates/gpui/src/view.rs b/crates/gpui/src/view.rs
index 081edd9a06b..76688b74326 100644
--- a/crates/gpui/src/view.rs
+++ b/crates/gpui/src/view.rs
@@ -96,9 +96,17 @@ impl View for AnyView {
Some(self.entity.entity_id())
}
+ #[cfg(not(gpui_hot_reload))]
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
(self.render)(&self, window, cx)
}
+
+ // The stored function pointer was captured from the original binary, so it must be looked up
+ // in the jump table to reach the patched render.
+ #[cfg(gpui_hot_reload)]
+ fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
+ subsecond::HotFn::current(self.render).call((&self, window, cx))
+ }
}
impl<V: 'static + Render> IntoElement for Entity<V> {
@@ -325,6 +333,24 @@ struct ViewElementCacheKey {
text_style: TextStyle,
}
+#[cfg(not(gpui_hot_reload))]
+#[inline]
+fn render_view<V: View>(view: V, window: &mut Window, cx: &mut App) -> AnyElement {
+ view.render(window, cx).into_any_element()
+}
+
+/// Every view and component render passes through this hot-patchable boundary, so the
+/// patched code for that render and everything it calls runs on the next frame.
+#[cfg(gpui_hot_reload)]
+fn render_view<V: View>(view: V, window: &mut Window, cx: &mut App) -> AnyElement {
+ #[inline(never)]
+ fn render_view_impl<V: View>(view: V, window: &mut Window, cx: &mut App) -> AnyElement {
+ view.render(window, cx).into_any_element()
+ }
+
+ subsecond::HotFn::current(render_view_impl::<V>).call((view, window, cx))
+}
+
impl<V: View> Element for ViewElement<V> {
type RequestLayoutState = Option<AnyElement>;
type PrepaintState = Option<AnyElement>;
@@ -358,7 +384,7 @@ impl<V: View> Element for ViewElement<V> {
self.cached_style.as_ref(),
window,
cx,
- &mut |window, cx| view.take().unwrap().render(window, cx).into_any_element(),
+ &mut |window, cx| render_view(view.take().unwrap(), window, cx),
);
#[cfg(feature = "profiler")]
window.window_profiler.end_view(
@@ -370,11 +396,7 @@ impl<V: View> Element for ViewElement<V> {
} else {
// Stateless path: isolate subtree via type name (no entity identity).
request_layout_component(type_name::<V>(), window, cx, &mut |window, cx| {
- self.view
- .take()
- .unwrap()
- .render(window, cx)
- .into_any_element()
+ render_view(self.view.take().unwrap(), window, cx)
})
}
}
@@ -399,13 +421,7 @@ impl<V: View> Element for ViewElement<V> {
element,
window,
cx,
- &mut |window, cx| {
- self.view
- .take()
- .unwrap()
- .render(window, cx)
- .into_any_element()
- },
+ &mut |window, cx| render_view(self.view.take().unwrap(), window, cx),
);
#[cfg(feature = "profiler")]
window.window_profiler.end_view(