A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.
C++
perf(grid): chunked row render so 3k+ result sets don't freeze the UI
DangHuynhf9d900e2f845
diff --git a/windows/Gridex/DataGridView.cpp b/windows/Gridex/DataGridView.cppindex 97aa671..449adae 100644--- a/windows/Gridex/DataGridView.cpp+++ b/windows/Gridex/DataGridView.cpp@@ -5,7 +5,10 @@#include <winrt/Windows.UI.Core.h>#include <winrt/Windows.System.h>#include <winrt/Microsoft.UI.Input.h>+#include <winrt/Microsoft.UI.Dispatching.h>#include <winrt/Windows.ApplicationModel.DataTransfer.h>+#include <functional>+#include <memory>#include <algorithm>#include <cmath>#if __has_include("DataGridView.g.cpp")@@ -615,15 +618,68 @@ namespace winrt::Gridex::implementationrows.Children().Clear();const int totalRows = static_cast<int>(data_.rows.size());- for (int i = 0; i < totalRows; i++)++ // Chunked render to keep the UI thread responsive on large+ // result sets. Building 3k+ row visuals in one synchronous+ // pass froze the app for 5+ seconds — measurable in the+ // wild on `SELECT * FROM big_table`. Tradeoff: the first+ // chunk (kFirstChunk) is rendered synchronously so the user+ // sees the top of the table immediately; remaining rows are+ // posted back to the dispatcher queue in batches of kChunk+ // so input + scroll get a chance to run between batches.+ constexpr int kFirstChunk = 200;+ constexpr int kChunk = 200;++ int firstEnd = (std::min)(totalRows, kFirstChunk);+ for (int i = 0; i < firstEnd; ++i)rows.Children().Append(BuildRowElement(i));rows.Visibility(mux::Visibility::Visible);// Re-apply selection highlight (selectedRow_ may carry over from// a previous load if the host hasn't reset it).- if (selectedRow_ >= 0 && selectedRow_ < totalRows)+ if (selectedRow_ >= 0 && selectedRow_ < firstEnd)HighlightRow(selectedRow_);++ if (firstEnd >= totalRows) return;++ // New generation token. If another SetData fires before this+ // chain finishes, the captured `gen` will mismatch and the+ // continuation drops out without appending stale rows.+ const uint64_t gen = ++buildRowsGeneration_;+ auto self = get_strong();+ auto dq = winrt::Microsoft::UI::Dispatching::DispatcherQueue::GetForCurrentThread();+ if (!dq)+ {+ // No dispatcher (unlikely on the UI thread, but be safe) —+ // fall through to synchronous render of the remainder.+ for (int i = firstEnd; i < totalRows; ++i)+ rows.Children().Append(BuildRowElement(i));+ return;+ }++ // shared_ptr<function> so the lambda can re-post itself+ // without the dangling-reference traps of capturing a+ // std::function by reference.+ auto cursor = std::make_shared<int>(firstEnd);+ auto step = std::make_shared<std::function<void()>>();+ *step = [self, dq, cursor, step, gen, totalRows]() mutable+ {+ if (self->buildRowsGeneration_ != gen) return; // superseded+ int start = *cursor;+ int end = (std::min)(totalRows, start + kChunk);+ auto rows = self->DataRows();+ for (int i = start; i < end; ++i)+ rows.Children().Append(self->BuildRowElement(i));+ // Re-apply selection if it lands in the chunk we just+ // realized.+ if (self->selectedRow_ >= start && self->selectedRow_ < end)+ self->HighlightRow(self->selectedRow_);+ *cursor = end;+ if (end < totalRows)+ dq.TryEnqueue([step]() { (*step)(); });+ };+ dq.TryEnqueue([step]() { (*step)(); });}// Build a single row's UI element. Layout: a horizontal StackPanel thatdiff --git a/windows/Gridex/DataGridView.h b/windows/Gridex/DataGridView.hindex a038531..3fc18a8 100644--- a/windows/Gridex/DataGridView.h+++ b/windows/Gridex/DataGridView.h@@ -130,6 +130,13 @@ namespace winrt::Gridex::implementationwinrt::Microsoft::UI::Xaml::Style cellStyle_{ nullptr };winrt::Microsoft::UI::Xaml::Style rowNumStyle_{ nullptr };+ // Bumped on every BuildRows kickoff. The chunked scheduler+ // captures its value and bails out as soon as it sees a newer+ // generation — so rapid SetData calls (table switch, refresh,+ // re-query) don't keep appending stale rows from the previous+ // load.+ uint64_t buildRowsGeneration_ = 0;+void EnsureCellStyles();void ComputeColumnWidths();