perf(chat): look up chat txids through a revision-keyed index
The History tab copies every chat txid into a fresh set each frame to badge and filter chat transactions: M string allocations for M chat messages. ChatStore gains hasChatTxid(), an O(1) lookup in a txid index that is rebuilt only after the store changed (append, delivery update, erase, clear) and never built for an empty store. clear() also drops the index, so no chat txids linger after a lock. The History tab moves to it in the next commit. The chat note-buffer pump, which runs every frame while chat is idle, built the whole conversation-id list just to see whether the store was empty; it now asks store().empty(), O(1) instead of O(M). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -4854,7 +4854,7 @@ bool App::shouldSplitChatBuffer() {
|
||||
// depleted, funded. NB: a wall-clock cooldown is deliberately NOT used — a split's outputs stay
|
||||
// invisible until mined (~1 block, far longer than any UI cooldown), so we gate on a single-split-
|
||||
// in-flight flag (cleared when the pipeline recovers or the watchdog fires) to prevent runaway splits.
|
||||
if (chat_service_.store().conversationIds().empty() && chat_send_queue_.empty()) return false;
|
||||
if (chat_service_.store().empty() && chat_send_queue_.empty()) return false; // == conversationIds().empty(), O(1)
|
||||
if (!state_.connected || state_.isLocked()) return false;
|
||||
// Channel busy = our own op inflight, OR a user send holds the shared channel. Lite user sends set
|
||||
// lite_send_callback_; full-node user sends don't touch inflight_op_, so they're detected via the
|
||||
|
||||
@@ -90,6 +90,7 @@ std::vector<ChatMessage> ChatStore::eraseConversation(const std::string& convers
|
||||
void ChatStore::clear() {
|
||||
messages_.clear();
|
||||
seen_.clear();
|
||||
txid_index_.clear(); // drop the History-badge txid copies too (revision_ below invalidates the memo)
|
||||
++revision_;
|
||||
}
|
||||
|
||||
|
||||
+22
-1
@@ -38,7 +38,7 @@ public:
|
||||
std::vector<std::string> conversationIds() const;
|
||||
|
||||
// The set of on-chain txids that carried a chat message (sent or received, messages + contact
|
||||
// requests) — used by the History tab to badge / filter chat transactions. O(messages), no copies.
|
||||
// requests). Builds a fresh COPY every call (O(messages) allocations) — prefer hasChatTxid() on hot paths.
|
||||
std::unordered_set<std::string> chatTxids() const {
|
||||
std::unordered_set<std::string> out;
|
||||
out.reserve(messages_.size());
|
||||
@@ -47,6 +47,15 @@ public:
|
||||
return out;
|
||||
}
|
||||
|
||||
// True if an on-chain txid carried a chat message (History tab "Message" badge / Chat filter). Backed by a
|
||||
// txid index rebuilt lazily when revision() moved (once per store mutation, O(messages)); O(1) on an empty
|
||||
// store and per lookup. One txid may back several messages; a from-scratch rebuild handles erase correctly.
|
||||
bool hasChatTxid(const std::string& txid) const {
|
||||
if (messages_.empty() || txid.empty()) return false;
|
||||
if (txid_index_rev_ != revision_) rebuildTxidIndex();
|
||||
return txid_index_.count(txid) != 0;
|
||||
}
|
||||
|
||||
// Remove every message in a conversation from the in-memory view and return the removed messages
|
||||
// (so the caller can delete/tombstone their persisted rows). Re-appends are prevented by the ingest
|
||||
// guard (blocked-cid predicate / DB tombstone), not here.
|
||||
@@ -70,9 +79,21 @@ public:
|
||||
private:
|
||||
static std::string dedupKey(const ChatMessage& message);
|
||||
|
||||
void rebuildTxidIndex() const {
|
||||
txid_index_.clear();
|
||||
txid_index_.reserve(messages_.size());
|
||||
for (const auto& m : messages_)
|
||||
if (!m.txid.empty()) txid_index_.insert(m.txid);
|
||||
txid_index_rev_ = revision_;
|
||||
}
|
||||
|
||||
std::vector<ChatMessage> messages_;
|
||||
std::unordered_set<std::string> seen_;
|
||||
std::uint64_t revision_ = 0;
|
||||
// Lazy memo for hasChatTxid(): valid iff txid_index_rev_ == revision_. revision_ is monotonic and never reset,
|
||||
// so append/updateDelivery/eraseConversation/clear all invalidate it (~0 = never built). Main thread only.
|
||||
mutable std::unordered_set<std::string> txid_index_;
|
||||
mutable std::uint64_t txid_index_rev_ = ~std::uint64_t{0};
|
||||
};
|
||||
|
||||
} // namespace dragonx::chat
|
||||
|
||||
Reference in New Issue
Block a user