聊天畫面的頁面卡片只看自己那段回覆,不再每畫一則就把整段對話重算一次
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 <noreply@anthropic.com>
This commit is contained in:
parent
cdb61e6690
commit
66e8f944f6
|
|
@ -847,7 +847,7 @@ impl App {
|
||||||
// 頁面 saved in this reply: the card (the page as it is now) — once a reply, under the step
|
// 頁面 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.)
|
// 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() {
|
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);
|
ui.add_space(8.0);
|
||||||
self.page_card(ui, id);
|
self.page_card(ui, id);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1002,6 +1002,11 @@ fn perf_long_chat() {
|
||||||
a.thinking = think.clone();
|
a.thinking = think.clone();
|
||||||
a.at = 1_700_000_000_000 + i * 2_000;
|
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 }];
|
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);
|
let mut r = Message::tool_result(&id, "browser_open", &long);
|
||||||
r.at = a.at + 1_000;
|
r.at = a.at + 1_000;
|
||||||
r.folded = i < 9_000;
|
r.folded = i < 9_000;
|
||||||
|
|
@ -1014,7 +1019,8 @@ fn perf_long_chat() {
|
||||||
let size = egui::vec2(1280.0, 800.0);
|
let size = egui::vec2(1280.0, 800.0);
|
||||||
let mut frame = eframe::Frame::_new_kittest();
|
let mut frame = eframe::Frame::_new_kittest();
|
||||||
let mut times = vec![];
|
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 {
|
if f >= 6 {
|
||||||
app.state.bot_mut(sel).unwrap().messages.push(Message::tool_result("x", "browser_open", "新的一步"));
|
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));
|
let mut out = ctx.run_ui(input, |ui| app.ui(ui, &mut frame));
|
||||||
times.push(t.elapsed().as_millis());
|
times.push(t.elapsed().as_millis());
|
||||||
let texts = out.shapes.iter().filter(|c| matches!(c.shape, egui::epaint::Shape::Text(_))).count();
|
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());
|
println!("PERF last frame shapes {} texts {texts}", out.shapes.len());
|
||||||
}
|
}
|
||||||
out.textures_delta.clear();
|
out.textures_delta.clear();
|
||||||
}
|
}
|
||||||
println!("PERF frames ms: {times:?}");
|
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 dir = std::env::temp_dir().join(format!("anybot-perf-{}", std::process::id()));
|
||||||
let p = dir.join("state.json");
|
let p = dir.join("state.json");
|
||||||
for k in 0..3 {
|
for k in 0..3 {
|
||||||
|
|
|
||||||
25
src/pages.rs
25
src/pages.rs
|
|
@ -477,6 +477,29 @@ fn written_id(c: &crate::model::ToolCall) -> Option<u64> {
|
||||||
serde_json::from_str::<Value>(&c.args).ok().and_then(|v| v["id"].as_u64())
|
serde_json::from_str::<Value>(&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<u64> {
|
||||||
|
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<u64> = 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
|
/// 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 —
|
/// 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.
|
/// 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);
|
let cards = cards_by_message(&msgs);
|
||||||
assert_eq!(cards, vec![vec![], vec![], vec![], vec![], vec![7], vec![], vec![], vec![7]]);
|
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::<Vec<_>>(), [cards, vec![vec![]]].concat());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue