perf(chat): single-pass watermark and move-append on chat load
Loading chat history did more work than it needed to. Seeding the unread watermark walked every conversation and copied each one's full message list out of the store just to read the last timestamp, and the database reload copied every message into the store one at a time with no reservation. - The watermark now comes from one pass over the stored messages that returns the latest timestamp per conversation, with no per- conversation copy or sort. - The store gained a move-append and a reserve, so the reload pre-sizes its vector and dedup set once and moves each row in instead of copying it. The dedup key is computed before the move. Behaviour is unchanged: the watermark values and the blocked-conversation filter on reload are the same as before. Tests cover the per-conversation latest-timestamp pass and move-append. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -119,11 +119,13 @@ int ChatService::ingest(const std::vector<HushChatTransactionMetadata>& metadata
|
||||
|
||||
void ChatService::loadFromDatabase() {
|
||||
if (!db_) return;
|
||||
for (const auto& message : db_->load()) {
|
||||
auto rows = db_->load();
|
||||
store_.reserve(store_.size() + rows.size());
|
||||
for (auto& message : rows) {
|
||||
// Never surface a blocked conversation, even if a prior "delete & block" failed to remove its
|
||||
// rows (defense-in-depth): the ingest guard already drops live scans, this covers the reload path.
|
||||
if (blocked_pred_ && blocked_pred_(message.conversation_id)) continue;
|
||||
store_.append(message);
|
||||
if (blocked_pred_ && blocked_pred_(message.conversation_id)) continue; // evaluate before the move
|
||||
store_.append(std::move(message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user