perf(console): extend the filtered view incrementally

With any console filter on, each batch of new output rescanned all N
lines to rebuild the visible set. A text filter lowercase-copied the
needle and every line to test it: O(total characters) and 2N
allocations per batch.

When the filter is unchanged and the model only appended or evicted
lines since the last pass, the visible indices drop the evicted lines
and shift down, and only the new lines are tested. The result equals
a full rescan, since line text and channel never change and the
filtered view has no folds. A filter change, Clear, or a model window
that doesn't follow on from the last one still rescans.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 09:50:03 -05:00
co-authored by Claude Opus 5.5
parent ee0a486676
commit 1ced49d622
5 changed files with 82 additions and 2 deletions
+13
View File
@@ -32,5 +32,18 @@ bool consoleLinePassesFilter(const std::string& lineText,
return true;
}
bool ExtendFlatVisibleIndices(std::vector<int>& visible, std::uint64_t oldFirst, std::uint64_t oldEnd,
std::uint64_t newFirst, int n, const std::function<bool(int)>& passes)
{
const std::uint64_t newEnd = newFirst + static_cast<std::uint64_t>(n < 0 ? 0 : n);
if (newFirst < oldFirst || newFirst > oldEnd || newEnd < oldEnd) return false;
const int shift = static_cast<int>(newFirst - oldFirst);
visible.erase(visible.begin(), std::lower_bound(visible.begin(), visible.end(), shift));
for (int& idx : visible) idx -= shift;
for (int i = static_cast<int>(oldEnd - newFirst); i < n; ++i)
if (passes(i)) visible.push_back(i);
return true;
}
} // namespace ui
} // namespace dragonx
+11
View File
@@ -2,7 +2,10 @@
#include "console_channel.h"
#include <cstdint>
#include <functional>
#include <string>
#include <vector>
namespace dragonx {
namespace ui {
@@ -20,5 +23,13 @@ bool consoleLinePassesFilter(const std::string& lineText,
ConsoleChannel channel,
const ConsoleOutputFilter& filter);
// Incrementally maintain a FLAT (filtered, fold-free) visible set across model appends/evictions. `visible` holds
// ascending raw indices computed for the absolute window [oldFirst, oldEnd); the model now spans
// [newFirst, newFirst + n). Drops evicted indices, rebases the survivors and appends every new raw index in
// [oldEnd - newFirst, n) for which passes(i) is true. Returns false (visible untouched) when the windows do not
// chain; the caller then rescans. Only valid while the filter is unchanged (line text/channel are immutable).
bool ExtendFlatVisibleIndices(std::vector<int>& visible, std::uint64_t oldFirst, std::uint64_t oldEnd,
std::uint64_t newFirst, int n, const std::function<bool(int)>& passes);
} // namespace ui
} // namespace dragonx
+21 -2
View File
@@ -948,15 +948,28 @@ void ConsoleTab::computeVisibleLines(bool& hasTextFilter, std::string& filterLow
// fold triangles aren't drawn/clickable while filtering (else clicks would silently mutate the
// collapsed flag with no visible effect and the glyph would disagree with the rendered block).
folding_active_ = !has_filter;
visible_indices_.clear();
const int n = static_cast<int>(model_.size());
if (has_filter) {
const std::uint64_t seqFirst = model_.firstSeq();
const int flatFlags = (outputFilter.daemonMessagesEnabled ? 1 : 0) | (outputFilter.errorsOnly ? 2 : 0) |
(outputFilter.rpcTraceEnabled ? 4 : 0) | (outputFilter.appMessagesEnabled ? 8 : 0);
// Filtered (flat) view with the exact same filter as the last pass and a model that only appended/evicted
// since: drop evicted indices, rebase, test only the new lines. Identical to the full scan below (line text +
// channel are immutable; the flat view ignores folds) without its per-drain O(total chars) lowercase pass.
const bool extended = has_filter && vis_flat_valid_ && vis_flat_flags_ == flatFlags &&
vis_flat_filter_ == outputFilter.text &&
ExtendFlatVisibleIndices(visible_indices_, vis_flat_first_, vis_flat_end_, seqFirst, n,
[&](int i) { return consoleLinePassesFilter(model_[i].text, model_[i].channel, outputFilter); });
if (extended) {
// visible_indices_ updated in place
} else if (has_filter) {
visible_indices_.clear();
// Filtered view is flat — folding is bypassed so every match is reachable.
for (int i = 0; i < n; i++) {
if (!consoleLinePassesFilter(model_[i].text, model_[i].channel, outputFilter)) continue;
visible_indices_.push_back(i);
}
} else {
visible_indices_.clear();
// No filter — show all lines, but hide the interior of any collapsed JSON block
// (its opener stays visible; the opener + foldSpan lines through the closer are skipped).
for (int i = 0; i < n; ) {
@@ -966,6 +979,11 @@ void ConsoleTab::computeVisibleLines(bool& hasTextFilter, std::string& filterLow
else i++;
}
}
vis_flat_valid_ = has_filter;
vis_flat_flags_ = flatFlags;
vis_flat_filter_ = outputFilter.text;
vis_flat_first_ = seqFirst;
vis_flat_end_ = seqFirst + static_cast<std::uint64_t>(n);
filter_match_count_ = hasTextFilter ? static_cast<int>(visible_indices_.size()) : 0;
}
@@ -2129,6 +2147,7 @@ void ConsoleTab::clear()
++vis_generation_;
layout_ = ConsoleLayout{}; // drop the refs into wrap_cache_ BEFORE resetting it (no dangling segments)
wrap_cache_.reset();
vis_flat_valid_ = false;
stop_confirm_pending_ = false; // a pending 'stop' confirmation is cancelled by clearing
addLine(TR("console_cleared"), ConsoleChannel::Info);
}
+7
View File
@@ -188,6 +188,13 @@ private:
bool folding_active_ = true; // fold UI shown only in the unfiltered (foldable) view
bool has_text_filter_ = false; // computed once per frame (before the toolbar draws it)
std::string filter_lower_; // lowercased filter needle for match highlighting
// Flat (filtered) view bookkeeping for the incremental computeVisibleLines path: the exact filter and the
// absolute model window [vis_flat_first_, vis_flat_end_) the current visible_indices_ were computed for.
bool vis_flat_valid_ = false;
int vis_flat_flags_ = 0;
std::string vis_flat_filter_;
std::uint64_t vis_flat_first_ = 0;
std::uint64_t vis_flat_end_ = 0;
// Memoization keys so the two expensive per-frame passes rebuild only on change (not every frame):
// computeVisibleLines (filter scan over the whole model) is keyed on the model revision + filter