main.cc · 69 linesall
// Headless per-frame CPU cost of large views in Clay (no renderer).
// Probe for docs/research/ui-virtualization/immediate-mode-large-views.md.
// Usage: clay-probe <all|all_scroll|windowed|windowed_scroll> <N> [frames]
#define CLAY_IMPLEMENTATION
#include "clay.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

static long measureCalls;
static Clay_Dimensions measure(Clay_StringSlice text, Clay_TextElementConfig *config, void *ud) {
    measureCalls++;
    return (Clay_Dimensions){ .width = text.length * 8.0f, .height = 16.0f };
}
static long errors; static void onError(Clay_ErrorData e) { if (!errors++) fprintf(stderr, "clay error (type %d): %.*s\n", e.errorType, e.errorText.length, e.errorText.chars); }
static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e6 + t.tv_nsec / 1e3; }
static int cmp(const void *a, const void *b) { double x = *(double*)a, y = *(double*)b; return x < y ? -1 : x > y; }

int main(int argc, char **argv) {
    const char *sc = argv[1];
    int n = atoi(argv[2]);
    int frames = argc > 3 ? atoi(argv[3]) : (2000000 / n < 8 ? 8 : 2000000 / n > 200 ? 200 : 2000000 / n);
    int windowed = strncmp(sc, "windowed", 8) == 0, scrolling = strstr(sc, "scroll") != NULL;
    Clay_String *labels = malloc(sizeof(Clay_String) * n);
    for (int i = 0; i < n; i++) {
        char *b = malloc(64);
        int len = snprintf(b, 64, "Row %d of %d: some_file_name.rs", i, n);
        labels[i] = (Clay_String){ .isStaticallyAllocated = false, .length = len, .chars = b };
    }
    Clay_SetMaxElementCount(windowed ? 8192 : n * 2 + 64);
    Clay_SetMaxMeasureTextCacheWordCount(windowed ? 16384 : n * 8 + 1024);
    uint32_t mem = Clay_MinMemorySize();
    Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory(mem, malloc(mem));
    Clay_Initialize(arena, (Clay_Dimensions){ 1280, 800 }, (Clay_ErrorHandler){ onError });
    Clay_SetMeasureTextFunction(measure, NULL);
    const float rowH = 16.0f;
    double *t = malloc(sizeof(double) * frames);
    long calls[3] = {0};
    int cmds = 0;
    for (int f = 0; f < frames; f++) {
        float scroll = scrolling ? f * 800.0f : 0.0f;
        if (scroll > n * rowH - 800) scroll = 0; // wrap around
        long before = measureCalls;
        double s = now();
        Clay_BeginLayout();
        CLAY(CLAY_ID("Scroll"), { .layout = { .layoutDirection = CLAY_TOP_TO_BOTTOM, .sizing = { CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0) } },
                                  .clip = { .vertical = true, .childOffset = (Clay_Vector2){ 0, -scroll } } }) {
            if (!windowed) {
                for (int i = 0; i < n; i++) CLAY_TEXT(labels[i], CLAY_TEXT_CONFIG({ .fontSize = 16, .textColor = {255, 255, 255, 255} }));
            } else {
                int first = (int)(scroll / rowH), last = first + (int)(800 / rowH) + 2;
                if (last > n) last = n;
                CLAY_AUTO_ID({ .layout = { .sizing = { CLAY_SIZING_FIXED(1), CLAY_SIZING_FIXED(first * rowH) } } }) {}
                for (int i = first; i < last; i++) CLAY_TEXT(labels[i], CLAY_TEXT_CONFIG({ .fontSize = 16, .textColor = {255, 255, 255, 255} }));
                CLAY_AUTO_ID({ .layout = { .sizing = { CLAY_SIZING_FIXED(1), CLAY_SIZING_FIXED((n - last) * rowH) } } }) {}
            }
        }
        Clay_RenderCommandArray r = Clay_EndLayout(1.0f / 60);
        t[f] = now() - s;
        cmds = r.length;
        if (f < 3) calls[f] = measureCalls - before;
    }
    double first = t[0];
    qsort(t + 2, frames - 2, sizeof(double), cmp);
    printf("%s\tN=%d\tframes=%d\tfirst_us=%.0f\tmedian_us=%.1f\trender_cmds=%d\tmeasure_calls_f0=%ld\tf1=%ld\tf2=%ld\terrors=%ld\n",
           sc, n, frames, first, t[2 + (frames - 2) / 2], cmds, calls[0], calls[1], calls[2], errors);
    return 0;
}