// Headless per-frame CPU cost of large views in Dear ImGui (null backend).
// Probe for docs/research/ui-virtualization/immediate-mode-large-views.md.
// Usage: imgui-probe <scenario> <N> [frames]
#include "imgui.h"
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
static const int FANOUT = 16;
static int N;
static std::vector<std::string> labels, treeLabels;
static bool hasKids(int i) { return (long)i * FANOUT + 1 < N; }
static void nested(int i) {
if (hasKids(i)) {
if (ImGui::TreeNodeEx((void*)(intptr_t)i, ImGuiTreeNodeFlags_DefaultOpen, "%s", treeLabels[i].c_str())) {
int end = std::min(N, i * FANOUT + FANOUT + 1);
for (int k = i * FANOUT + 1; k < end; k++) nested(k);
ImGui::TreePop();
}
} else {
ImGui::TreeNodeEx((void*)(intptr_t)i, ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen, "%s", treeLabels[i].c_str());
}
}
struct Row { int node; int depth; };
static void flatten(std::vector<Row>& out) {
out.clear();
std::vector<Row> stack{{0, 0}};
while (!stack.empty()) {
Row r = stack.back(); stack.pop_back();
out.push_back(r);
if (hasKids(r.node)) {
int end = std::min(N, r.node * FANOUT + FANOUT + 1);
for (int k = end - 1; k >= r.node * FANOUT + 1; k--) stack.push_back({k, r.depth + 1});
}
}
}
// The zero-caching "seek" clipper of imgui_demo.cpp's Property Editor (issue #3823):
// walk the visible order every frame, submitting widgets only inside the clipper's range.
static int seekDepth;
static int seekAdvance(ImGuiListClipper& c, int node) {
bool open;
ImGuiTreeNodeFlags fl = ImGuiTreeNodeFlags_DefaultOpen;
if (c.UserIndex >= c.DisplayStart && c.UserIndex < c.DisplayEnd) {
ImGui::SetCursorPosX(8 + seekDepth * ImGui::GetStyle().IndentSpacing);
ImGui::SetNextItemStorageID((ImGuiID)(node + 1));
open = ImGui::TreeNodeEx((void*)(intptr_t)node, (hasKids(node) ? fl : ImGuiTreeNodeFlags_Leaf) | ImGuiTreeNodeFlags_NoTreePushOnOpen, "%s", treeLabels[node].c_str()) && hasKids(node);
} else
open = hasKids(node) && ImGui::TreeNodeGetOpen((ImGuiID)(node + 1), fl);
c.UserIndex++;
if (open) { seekDepth++; return node * FANOUT + 1; }
while (node != 0 && (node % FANOUT == 0 || node + 1 >= N)) { node = (node - 1) / FANOUT; seekDepth--; }
return node == 0 ? -1 : node + 1;
}
static void textures() {
for (ImTextureData* t : ImGui::GetPlatformIO().Textures) {
if (t->Status == ImTextureStatus_WantCreate || t->Status == ImTextureStatus_WantUpdates) { t->SetTexID((ImTextureID)1); t->SetStatus(ImTextureStatus_OK); }
else if (t->Status == ImTextureStatus_WantDestroy) { t->SetTexID(ImTextureID_Invalid); t->SetStatus(ImTextureStatus_Destroyed); }
}
}
int main(int argc, char** argv) {
const char* sc = argv[1];
N = atoi(argv[2]);
int frames = argc > 3 ? atoi(argv[3]) : std::clamp(2000000 / N, 8, 200);
for (int i = 0; i < N; i++) {
labels.push_back("Row " + std::to_string(i) + " of " + std::to_string(N) + ": some_file_name.rs");
treeLabels.push_back(hasKids(i) ? "dir_" + std::to_string(i) : "file_" + std::to_string(i) + ".rs");
}
std::vector<Row> flat;
auto f0 = std::chrono::steady_clock::now();
flatten(flat);
double flatten_us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - f0).count();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(1280, 800);
io.DeltaTime = 1.0f / 60.0f;
io.IniFilename = nullptr;
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures;
std::vector<double> t;
int vtx = 0, cmds = 0;
for (int f = 0; f < frames; f++) {
auto s = std::chrono::steady_clock::now();
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::Begin("w", nullptr, ImGuiWindowFlags_NoDecoration);
if (!strcmp(sc, "scroll_page")) ImGui::SetScrollY(f * 800.0f);
if (!strcmp(sc, "flat_all")) { for (int i = 0; i < N; i++) ImGui::TextUnformatted(labels[i].c_str()); }
else if (!strcmp(sc, "flat_clipper") || !strcmp(sc, "scroll_page")) {
ImGuiListClipper c; c.Begin(N);
while (c.Step()) for (int i = c.DisplayStart; i < c.DisplayEnd; i++) ImGui::TextUnformatted(labels[i].c_str());
}
else if (!strcmp(sc, "tree_nested_open")) nested(0);
else if (!strcmp(sc, "tree_seek")) {
ImGuiListClipper c; c.Begin(INT_MAX); int node = 0; seekDepth = 0;
while (c.Step()) while (c.UserIndex < c.DisplayEnd && node != -1) node = seekAdvance(c, node);
while (node != -1) node = seekAdvance(c, node);
c.SeekCursorForItem(c.UserIndex);
if (f == frames - 1) fprintf(stderr, "seek rows=%d\n", c.UserIndex);
}
else if (!strcmp(sc, "tree_clipper")) {
ImGuiListClipper c; c.Begin((int)flat.size());
while (c.Step()) for (int i = c.DisplayStart; i < c.DisplayEnd; i++) {
const Row& r = flat[i];
ImGui::SetCursorPosX(8 + r.depth * ImGui::GetStyle().IndentSpacing);
ImGui::TreeNodeEx((void*)(intptr_t)r.node, (hasKids(r.node) ? ImGuiTreeNodeFlags_DefaultOpen : ImGuiTreeNodeFlags_Leaf) | ImGuiTreeNodeFlags_NoTreePushOnOpen, "%s", treeLabels[r.node].c_str());
}
}
else if (!strcmp(sc, "table_all") || !strcmp(sc, "table_clipper")) {
if (ImGui::BeginTable("t", 4, ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable)) {
auto row = [&](int i) { ImGui::TableNextRow();
ImGui::TableNextColumn(); ImGui::TextUnformatted(treeLabels[i].c_str());
ImGui::TableNextColumn(); ImGui::Text("%d", i);
ImGui::TableNextColumn(); ImGui::TextUnformatted("regular");
ImGui::TableNextColumn(); ImGui::TextUnformatted(labels[i].c_str()); };
if (sc[6] == 'a') for (int i = 0; i < N; i++) row(i);
else { ImGuiListClipper c; c.Begin(N); while (c.Step()) for (int i = c.DisplayStart; i < c.DisplayEnd; i++) row(i); }
ImGui::EndTable();
}
}
else { fprintf(stderr, "scenario?\n"); return 1; }
ImGui::End();
ImGui::Render();
ImDrawData* dd = ImGui::GetDrawData();
vtx = dd->TotalVtxCount; cmds = 0;
for (ImDrawList* l : dd->CmdLists) cmds += l->CmdBuffer.Size;
textures();
t.push_back(std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - s).count());
}
std::vector<double> w(t.begin() + 2, t.end()); std::sort(w.begin(), w.end());
printf("%s\tN=%d\tframes=%d\tfirst_us=%.0f\tthird_us=%.0f\tmedian_us=%.1f\tvtx=%d\tcmds=%d\tflatten_us=%.0f\n", sc, N, frames, t[0], t[2], w[w.size()/2], vtx, cmds, flatten_us);
ImGui::DestroyContext();
}