Skip to content

perf: parse chat history off the main thread on tab open - #18

Merged
SergKam merged 1 commit into
mainfrom
perf/async-history-parse
Jun 11, 2026
Merged

perf: parse chat history off the main thread on tab open#18
SergKam merged 1 commit into
mainfrom
perf/async-history-parse

Conversation

@SergKam

@SergKam SergKam commented Jun 11, 2026

Copy link
Copy Markdown
Owner

Symptom

After the instant-switch fix (#17), the residual tab-open pause (~half a second) was the synchronous parse of the persisted chat history — a multi-MB JSON via serde — running inside Workspace::new on the GTK main thread.

Fix

  • History starts empty; it's parsed on a worker thread. A short (30 ms) poll delivers the parsed Vec<ChatMessage> back to the UI thread.
  • create_agent_panel no longer reads history at construction. It returns a new load_history callback; the parse-completion handler calls it to populate the shared buffer and render the first page in the existing idle-chunked batches.
  • The autosave dirty counter (last_saved_chat_len, now a shared Rc<Cell<usize>>) is seeded to the loaded length, so the first autosave doesn't redundantly rewrite the history we just read.

Net: building a tab no longer blocks on the JSON parse — the panel appears immediately and history streams in once parsed.

Testing

  • cargo clippy --lib --bins -- -D warnings — clean
  • cargo fmt --check — clean
  • cargo test — all pass

The remaining tab-open stall was the synchronous parse of the persisted chat
history (a multi-megabyte JSON via serde) inside `Workspace::new`, on the GTK
main thread.

Now the history starts empty and is parsed on a worker thread; a short poll
delivers the parsed messages back to the UI thread and hands them to the panel's
new `load_history` callback, which populates the shared buffer and renders the
first page in idle-chunked batches (as before). The autosave dirty counter is
seeded to the loaded length so the first autosave doesn't rewrite what we just
read.

`create_agent_panel` no longer reads history at construction; it returns a
`load_history` hydration callback instead.
@SergKam
SergKam merged commit 291f0b6 into main Jun 11, 2026
2 checks passed
@SergKam
SergKam deleted the perf/async-history-parse branch June 11, 2026 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant