diff --git a/native/gpui/src/hub.rs b/native/gpui/src/hub.rs index 859075a730..ceed783156 100644 --- a/native/gpui/src/hub.rs +++ b/native/gpui/src/hub.rs @@ -502,14 +502,22 @@ async fn session(config:&Config,commands:&mut mpsc::Receiver,queued:&mu if let Some(e)=client_cmd(command) {let _=events.send(e).await;continue;} if command["type"]=="send" { /* i18n-ok: connection errors, logged as GPUI_LOG disconnect */ ensure!(command["hub"].is_null()||command["hub"]==v.hub,"cross-hub command refused"); - let request:Value=serde_json::from_str(command["text"].as_str().context("missing command text")?)?; + let mut request:Value=serde_json::from_str(command["text"].as_str().context("missing command text")?)?; let rpc_id=request["id"].as_u64().context("missing RPC id")?; - let m=request["m"].as_str().unwrap_or("");ensure!(!m.starts_with("view.")&&rpc_id<(1<<47),"invalid product RPC");eprintln!("GPUI_LOG {{\"rpc_sent\":\"{m}\",\"thread\":{}}}",request["p"]["thread"]); + let m=request["m"].as_str().unwrap_or("").to_string();ensure!(!m.starts_with("view.")&&rpc_id<(1<<47),"invalid product RPC");eprintln!("GPUI_LOG {{\"rpc_sent\":\"{m}\",\"thread\":{}}}",request["p"]["thread"]); if m == "kicad.watch.set" { dock_watch = Some((rpc_id, dock_watch_request(&request, &r["shown"]["thread"]))); announce = true; - } else if m == "kicad.watch" { dock_watch = None; } - v.outgoing.insert(rpc_id,method=="view.action"&&is_post(m));v.rpc_names.insert(rpc_id,m.to_string()); + } else if m == "kicad.watch" { + dock_watch = None; + if request["p"]["force"] != true && request["p"].get("keptChunks").is_none() { + let hashes = crate::screens::plot::kept_chunk_hashes(); + if !hashes.is_empty() { + if let Some(obj) = request["p"].as_object_mut() { obj.insert("keptChunks".into(), json!(hashes)); } + } + } + } + v.outgoing.insert(rpc_id,method=="view.action"&&is_post(&m));v.rpc_names.insert(rpc_id,m.clone()); // The send action's canonical command is named by the hub; ACK all sends from this action. if method=="view.action" && m=="chat.send" {v.outgoing.insert(rpc_id,true);} sink.send(Message::Binary(wire::encode(&request).into())).await?; diff --git a/native/gpui/src/screens/plot.rs b/native/gpui/src/screens/plot.rs index 894a64e57c..28d3404b81 100644 --- a/native/gpui/src/screens/plot.rs +++ b/native/gpui/src/screens/plot.rs @@ -3,7 +3,41 @@ //! plot applied) or the chunk itself `{l layer, c colour, a alpha, m mode, w width, d varints}`; applying it is `Plot.apply`, so //! `held` mirrors what the hub thinks this connection holds. Nothing is decided here: which chunks, their order, colour and //! opacity come from the hub. Pure; decoded chunks are capsules (strokes) and fan triangles (fills), in micrometres. -use std::{collections::BTreeMap, sync::Arc}; +use std::{collections::{BTreeMap, HashMap, VecDeque}, sync::{Arc, Mutex, OnceLock}}; + +const MAX_CHUNK_CACHE: usize = 2048; + +#[derive(Default)] +struct ChunkCache { + map: HashMap>, + order: VecDeque, +} + +fn chunk_cache() -> &'static Mutex { + static CACHE: OnceLock> = OnceLock::new(); + CACHE.get_or_init(Default::default) +} + +fn cache_chunk(hash: &str, chunk: &Arc) { + if hash.len() != 16 { return; } + let mut g = chunk_cache().lock().unwrap(); + if !g.map.contains_key(hash) { + g.order.push_back(hash.to_string()); + while g.order.len() > MAX_CHUNK_CACHE { + if let Some(old) = g.order.pop_front() { g.map.remove(&old); } + } + } + g.map.insert(hash.to_string(), chunk.clone()); +} + +fn get_cached_chunk(hash: &str) -> Option> { + chunk_cache().lock().unwrap().map.get(hash).cloned() +} + +/// Persistent 16-hex chunk hashes held across board switches (`keptChunks` on `kicad.watch`). +pub fn kept_chunk_hashes() -> Vec { + chunk_cache().lock().unwrap().map.keys().cloned().collect() +} /// A CBOR value as far as plots use it (no tags: a frame's dictionary words are plain keys 1 and 31 for "t" and "key"). #[derive(Clone, Debug, PartialEq)] @@ -197,7 +231,15 @@ impl Store { for r in refs { match r { C::U(k) if !stale_base && *k >= 0 && (*k as usize) < held.len() => cs.push(held[*k as usize].clone()), - C::Map(_) => cs.push(Arc::new(Chunk::decode(r))), + C::Text(h) => match get_cached_chunk(h) { + Some(c) => cs.push(c), + /* i18n-ok: kept in English in the frame; viewer.rs draws it through trs */ None => { self.fail(key, fam, "Invalid geometry reference. Retry to reload this view.", Some(true)); return Err("missing cached chunk hash"); } + }, + C::Map(_) => { + let ch = Arc::new(Chunk::decode(r)); + if let Some(h) = r.get("h").and_then(C::text) { cache_chunk(h, &ch); } + cs.push(ch); + } /* i18n-ok: kept in English in the frame; viewer.rs draws it through trs */ _ => { self.fail(key, fam, "Invalid geometry reference. Retry to reload this view.", Some(true)); return Err("invalid geometry reference"); } } }