From f2a1898039775fba0181be92b6472c2d7d605ca1 Mon Sep 17 00:00:00 2001 From: mileswa1q22 Date: Fri, 21 Aug 2026 19:02:37 -0500 Subject: [PATCH] working to make it more stabel --- Cargo.lock | 19 +++++++++++++++---- Cargo.toml | 9 +++++++++ src/app.rs | 41 +++++++++++++++++++++++++++++++++++++++-- src/servo_host.rs | 6 ++++++ 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cbd4fda..83c3cfc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,16 +77,16 @@ dependencies = [ [[package]] name = "aes-gcm" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdf011db2e21ce0d575593d749db5554b47fed37aff429e4dc50bc91ac93a028" +checksum = "7f2b8006a0c83f52b62ba44a97b58bf76fe2f70a329e588f67f89691d93d498f" dependencies = [ "aead", "aes", "cipher", "ctr", + "ctutils", "ghash", - "subtle", "zeroize", ] @@ -2380,6 +2380,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5" dependencies = [ "polyval", + "zeroize", ] [[package]] @@ -5167,6 +5168,7 @@ dependencies = [ "cpubits", "cpufeatures 0.3.0", "universal-hash", + "zeroize", ] [[package]] @@ -5800,7 +5802,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -9845,6 +9847,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.60.2" diff --git a/Cargo.toml b/Cargo.toml index 212ba2d..f13ccfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,12 @@ servo = "0.5.0" url = "2" wgpu = "30.0.0" winit = "0.30.13" + +[profile.dev.package."*"] +# Optimize dependencies (Servo, WebRender, wgpu…) even in dev builds — +# unoptimized engines are unusably slow, while keeping our crate debuggable. +opt-level = 2 + +[profile.release] +lto = "thin" +codegen-units = 1 diff --git a/src/app.rs b/src/app.rs index 3b283c2..066176a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -30,12 +30,17 @@ const CHROME_BG_DARKER: egui::Color32 = egui::Color32::from_rgb(223, 223, 230); const TAB_ACTIVE_BG: egui::Color32 = egui::Color32::WHITE; const ACCENT: egui::Color32 = egui::Color32::from_rgb(0, 97, 224); +/// Minimum time between expensive GPU->CPU page readbacks (~one vsync). +const MIN_PAINT_INTERVAL: Duration = Duration::from_millis(15); + pub struct BrowserApp { proxy: EventLoopProxy, window: Option>, gpu: Option, - engine: Option>, + // NOTE: `tabs` must be dropped BEFORE `engine` — closing a WebView sends a + // message to Servo, which must still be alive when that happens. tabs: Vec, + engine: Option>, active: usize, url_edit: String, url_bar_focused: bool, @@ -63,8 +68,8 @@ impl BrowserApp { proxy, window: None, gpu: None, - engine: None, tabs: Vec::new(), + engine: None, active: 0, url_edit: START_PAGE.to_owned(), url_bar_focused: false, @@ -380,6 +385,12 @@ impl BrowserApp { if !tab.dirty { return; } + // Throttle GPU->CPU readbacks: a full-surface read is expensive, so skip + // frames arriving sooner than one vsync after the previous read. Keep the + // dirty flag so the next redraw picks the newer frame up instead. + if tab.last_paint.elapsed() < MIN_PAINT_INTERVAL { + return; + } tab.webview.paint(); let size = tab.webview.size(); @@ -391,8 +402,24 @@ impl BrowserApp { let rect = euclid::Box2D::from_origin_and_size(euclid::point2(0, 0), euclid::size2(w, h)); let rc = tab.webview.rendering_context(); if let Some(img) = rc.read_to_image(rect) { + tab.last_paint = Instant::now(); let dims = [img.width() as usize, img.height() as usize]; let pixels = img.into_raw(); + // Skip the (expensive) texture re-upload when nothing changed. + let hash = { + use std::hash::{Hash, Hasher}; + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + pixels.len().hash(&mut hasher); + for chunk in pixels.chunks(4096) { + chunk.hash(&mut hasher); + } + hasher.finish() + }; + if hash == tab.last_frame_hash && tab.texture.is_some() { + tab.dirty = false; + return; + } + tab.last_frame_hash = hash; let image = egui::ColorImage::from_rgba_unmultiplied(dims, &pixels); match (&tab.texture, &egui_ctx) { (Some(texture), _) => { @@ -835,6 +862,16 @@ impl ApplicationHandler for BrowserApp { event_loop.set_control_flow(ControlFlow::Wait); } + fn exiting(&mut self, _event_loop: &ActiveEventLoop) { + // Close all WebViews while the Servo instance is still alive, so their + // shutdown messages reach a running constellation instead of a dying one. + if let Some(engine) = self.engine.take() { + engine.spin(); + self.drain_engine_messages(); + } + self.tabs.clear(); + } + fn window_event( &mut self, event_loop: &ActiveEventLoop, diff --git a/src/servo_host.rs b/src/servo_host.rs index 5ac619c..d2e64fe 100644 --- a/src/servo_host.rs +++ b/src/servo_host.rs @@ -151,6 +151,10 @@ pub struct Tab { pub animating: bool, pub dirty: bool, pub texture: Option, + /// Hash of the last uploaded frame — skips redundant GPU texture uploads. + pub last_frame_hash: u64, + /// Time of the last GPU->CPU readback — throttles readback frequency. + pub last_paint: std::time::Instant, } impl Tab { @@ -165,6 +169,8 @@ impl Tab { animating: false, dirty: true, texture: None, + last_frame_hash: 0, + last_paint: std::time::Instant::now() - std::time::Duration::from_secs(1), } }