From 66e8f944f6732fe8bf961e3fa54f51c865bd0c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Fri, 27 Nov 2026 17:03:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E7=95=AB=E9=9D=A2=E7=9A=84?= =?UTF-8?q?=E9=A0=81=E9=9D=A2=E5=8D=A1=E7=89=87=E5=8F=AA=E7=9C=8B=E8=87=AA?= =?UTF-8?q?=E5=B7=B1=E9=82=A3=E6=AE=B5=E5=9B=9E=E8=A6=86=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=AF=8F=E7=95=AB=E4=B8=80=E5=89=87=E5=B0=B1=E6=8A=8A?= =?UTF-8?q?=E6=95=B4=E6=AE=B5=E5=B0=8D=E8=A9=B1=E9=87=8D=E7=AE=97=E4=B8=80?= =?UTF-8?q?=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jenny-Host 換上 cdb61e6 後視窗執行緒還是 92%。sudo perf(使用者同意)取樣 20 秒: pages::ids_in 44%、pages::cards_by_message 15%,加上解析 write_page 參數的 JSON。 每一格畫面都把整段對話排過一次,每則寫過頁面的訊息都呼叫 cards_by_message(&整段對話), 也就是每格把全部 write_page 的參數解析「寫過頁面的訊息數」次(她的對話約 239×239)。 現在用 pages::cards_at:只看這則訊息到下一則使用者訊息之間,跟 cards_by_message 給它的一樣(測試逐則比對)。 perf_long_chat 加上 write_page,也印出兩種算法各自的時間。 已驗證: cargo test 全綠(932 通過);a_reply_shows_each_saved_page_once_under_its_last_save 逐則比對 cards_at 和 cards_by_message。 未驗證: jenny-Host 實機(之後跟重構一起部署)。另外一格畫面還是會把整段對話全部排過一次, 每則訊息做的事都跟對話長度成正比;根本的解法是讓對話不要無限長大(下一步)。 Co-Authored-By: Claude Opus 5.5 --- src/app/chat_ui.rs | 2 +- src/app/layout_audit.rs | 18 ++++++++++++++++-- src/pages.rs | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/app/chat_ui.rs b/src/app/chat_ui.rs index afd5853..c7e979e 100644 --- a/src/app/chat_ui.rs +++ b/src/app/chat_ui.rs @@ -847,7 +847,7 @@ impl App { // 頁面 saved in this reply: the card (the page as it is now) — once a reply, under the step // that saved it last. (Worked out only for a message that saved one: few do.) if !crate::pages::ids_in(&m.tool_calls).is_empty() { - for id in crate::pages::cards_by_message(&bot.messages).get(idx).cloned().unwrap_or_default() { + for id in crate::pages::cards_at(&bot.messages, idx) { ui.add_space(8.0); self.page_card(ui, id); } diff --git a/src/app/layout_audit.rs b/src/app/layout_audit.rs index 762f49f..b809535 100644 --- a/src/app/layout_audit.rs +++ b/src/app/layout_audit.rs @@ -1002,6 +1002,11 @@ fn perf_long_chat() { a.thinking = think.clone(); a.at = 1_700_000_000_000 + i * 2_000; a.tool_calls = vec![crate::model::ToolCall { id: id.clone(), name: "browser_open".into(), args, status: crate::model::ToolStatus::Done }]; + // (jenny-Host's bot keeps a page of what it sent: a write_page every so often.) + if i % 70 == 0 { + let page = serde_json::json!({"id": 16, "content": think.repeat(10), "note": "更新"}).to_string(); + a.tool_calls = vec![crate::model::ToolCall { id: id.clone(), name: "write_page".into(), args: page, status: crate::model::ToolStatus::Done }]; + } let mut r = Message::tool_result(&id, "browser_open", &long); r.at = a.at + 1_000; r.folded = i < 9_000; @@ -1014,7 +1019,8 @@ fn perf_long_chat() { let size = egui::vec2(1280.0, 800.0); let mut frame = eframe::Frame::_new_kittest(); let mut times = vec![]; - for f in 0..12 { + let frames = std::env::var("PERF_FRAMES").ok().and_then(|f| f.parse().ok()).unwrap_or(12); + for f in 0..frames { if f >= 6 { app.state.bot_mut(sel).unwrap().messages.push(Message::tool_result("x", "browser_open", "新的一步")); } @@ -1023,12 +1029,20 @@ fn perf_long_chat() { let mut out = ctx.run_ui(input, |ui| app.ui(ui, &mut frame)); times.push(t.elapsed().as_millis()); let texts = out.shapes.iter().filter(|c| matches!(c.shape, egui::epaint::Shape::Text(_))).count(); - if times.len() == 12 { + if times.len() == frames { println!("PERF last frame shapes {} texts {texts}", out.shapes.len()); } out.textures_delta.clear(); } println!("PERF frames ms: {times:?}"); + let all = app.state.bot(sel).unwrap().messages.clone(); + let t = std::time::Instant::now(); + let _ = crate::pages::cards_by_message(&all); + println!("PERF cards_by_message (the old way, once per page-writing message drawn) ms: {}", t.elapsed().as_millis()); + let t = std::time::Instant::now(); + let last = all.iter().rposition(|m| m.tool_calls.iter().any(|c| c.name == "write_page")).unwrap(); + let _ = crate::pages::cards_at(&all, last); + println!("PERF cards_at µs: {}", t.elapsed().as_micros()); let dir = std::env::temp_dir().join(format!("anybot-perf-{}", std::process::id())); let p = dir.join("state.json"); for k in 0..3 { diff --git a/src/pages.rs b/src/pages.rs index 278927d..0905284 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -477,6 +477,29 @@ fn written_id(c: &crate::model::ToolCall) -> Option { serde_json::from_str::(&c.args).ok().and_then(|v| v["id"].as_u64()) } +/// The pages whose card shows under message `idx` (what cards_by_message gives it), reading only +/// its own reply. The chat drew each message that saved a page by working out the whole chat: +/// on jenny-Host (36 000 messages, a page written a few hundred times) that was most of the +/// window's time every frame, parsing every write_page in the chat once per such message shown. +pub fn cards_at(messages: &[crate::model::Message], idx: usize) -> Vec { + let Some(m) = messages.get(idx) else { return vec![] }; + if m.role == crate::model::Role::User { + return vec![]; + } + let mine = ids_in(&m.tool_calls); + if mine.is_empty() { + return mine; + } + let mut later: Vec = vec![]; + for n in &messages[idx + 1..] { + if n.role == crate::model::Role::User { + break; + } + later.extend(ids_in(&n.tool_calls)); + } + mine.into_iter().filter(|id| !later.contains(id)).collect() +} + /// For each message, the pages whose card shows under it: each page a reply saved, once, under the /// step that saved it last (a reply that wrote its page three times — sent back, fixed, updated — /// showed three cards of the same 第 3 版). A reply runs from one message of yours to the next. @@ -896,6 +919,8 @@ mod tests { ]; let cards = cards_by_message(&msgs); assert_eq!(cards, vec![vec![], vec![], vec![], vec![], vec![7], vec![], vec![], vec![7]]); + // One message at a time (what the chat draws) says the same, reading only its reply. + assert_eq!((0..msgs.len() + 1).map(|i| cards_at(&msgs, i)).collect::>(), [cards, vec![vec![]]].concat()); } #[test]