Files
tinyblocks/tests/vm_bench.c
T
mguinhosandClaude Opus 5.5 b4a27f0180 Executor compila "mude para" e "mude por"; listas e variáveis sem procurar nome
- TB_PURE_SET_VAR / TB_PURE_CHANGE_VAR: os comandos de variável rodam
  direto do programa compilado (alvo resolvido para o slot), sem montar,
  marcar e avaliar a árvore do bloco.
- Condição compilada de "se"/"repita até" é lida sem set_output/cópia.
- get_rt: o bloco guarda a própria árvore (rt_hot), sem busca no hash.
- Listas com nome: o bloco guarda o índice da lista; antes copiava o nome
  com snprintf e procurava entre todas a cada acesso.
- TbVarsEpoch: o código compilado guarda o ponteiro do valor da variável
  e só confere geração e escopo (sem chamada). Blocos de variável com o
  nome no próprio campo pulam o strcmp de conferência.
- jcmp compara números sem passar por TinyBlocks_Value.

Minecraft, vista de perto: pior 19 ms -> 10 ms; 748 M -> 565 M instruções
em 3 renders. Mesma imagem (checksum) com e sem compilador.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 17:18:08 -03:00

146 lines
6.4 KiB
C

#define _POSIX_C_SOURCE 200809L
/* vm_bench.c - mede a VM do editor rodando um projeto sem janela.
* Uso: build/vm_bench projeto.json [quadros]
* Clica a bandeira e, a cada quadro do editor, liga a variavel "mudou"
* (o Minecraft so redesenha quando a camera mexe) e mede o tempo do quadro. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_renderable_block.h>
#include <tinyblocks/tb_vars.h>
#include <tinyblocks/tb_image.h>
#include <tinyblocks/tb_value_class.h>
#include <tb_editor.h>
#include <tb_editor_internal.h>
#include <tb_graphics.h>
#include <tb_events.h>
#include <tb_control.h>
#include <tb_errors.h>
#include <tb_operators.h>
#include <tb_variables.h>
#include <tb_appearance.h>
#include <tb_pen.h>
#include <tb_input_lib.h>
#include <tb_imglib.h>
#include <tb_number.h>
#include <tb_string.h>
#include <tb_vector.h>
static TinyBlocks* rt;
static TinyBlocks_Block *ev, *scene;
static double now(void);
static double t_eval, t_draw;
static void frame(void) {
tb_block_set_output(ev, GFX_IN_WIN_W, tb_value_integer(1280));
tb_block_set_output(ev, GFX_IN_WIN_H, tb_value_integer(720));
tb_block_set_output(ev, GFX_IN_TEXT, tb_value_text(""));
tb_block_set_output(ev, GFX_IN_DROP_FILE, tb_value_text(""));
double a = now();
tb_block_eval(scene, rt);
double b = now();
TinyBlocks_RenderableBlock* rb = NULL;
scene->type->on_draw(scene, rt, &rb);
t_eval += b - a; t_draw += now() - b;
tb_renderable_release_all();
}
static double now(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec + t.tv_nsec / 1e9;
}
int main(int argc, char** argv) {
if (argc < 2) { fprintf(stderr, "uso: %s projeto.json [quadros]\n", argv[0]); return 2; }
int frames = argc > 2 ? atoi(argv[2]) : 20;
setenv("XDG_CONFIG_HOME", "/tmp/tb_bench_cfg", 1);
setenv("TINYBLOCKS_MCP_PORT", "0", 1);
tinyblocks_init(&rt, NULL);
tb_graphics_set_backend(tb_graphics_null_backend());
tb_library_register(rt, tb_events_get_library());
tb_library_register(rt, tb_control_get_library());
tb_library_register(rt, tb_errors_get_library());
tb_library_register(rt, tb_operators_get_library());
tb_library_register(rt, tb_variables_get_library());
tb_library_register(rt, tb_appearance_get_library());
tb_library_register(rt, tb_pen_get_library());
tb_library_register(rt, tb_input_get_library());
tb_library_register(rt, tb_imglib_get_library());
tb_library_register(rt, tb_number_get_library());
tb_library_register(rt, tb_string_get_library());
tb_library_register(rt, tb_vector_get_library());
tb_library_register(rt, tb_graphics_get_library());
tb_library_register(rt, tb_editor_get_library());
ev = tb_block_create(rt, tb_type_block_find(rt, "Graphics.Input"));
scene = tb_block_create(rt, tb_type_block_find(rt, "Editor.Scene"));
tb_block_set_default(scene, 0, tb_value_block(ev));
frame();
Ed* ed = *(Ed**)scene->userdata;
if (!project_load(ed, argv[1])) { fprintf(stderr, "nao abriu: %s\n", ed->hot_status); return 1; }
tab_select(ed, ed->tab_n - 1); /* a ultima aba (o programa) */
frame();
run_flag(ed);
/* quadros do editor ate o primeiro desenho completo (setup + 1o render) */
if (getenv("BENCH_PROFILE")) { /* so render: "mudou" sempre ligado */
for (int k = 0; k < frames; k++) {
tb_vars_set("mudou", tb_value_integer(1));
const char* cam = getenv("BENCH_CAM");
if (cam) {
double c[4] = {0};
sscanf(cam, "%lf,%lf,%lf,%lf", &c[0], &c[1], &c[2], &c[3]);
const char* nm[4] = { "cx", "cy", "cz", "pit" };
for (int q = 0; q < 4; q++) tb_vars_set(nm[q], tb_value_real(c[q]));
tb_vars_set("yaw", tb_value_integer(270));
}
frame();
}
return 0;
}
double t0 = now();
int n = 0;
while (ed->vm_running && n < 100000) { frame(); n++; const TinyBlocks_Value* m = tb_vars_get("nf"); if (m && n > 2 && tb_vars_get("mudou") && tb_vars_get("mudou")->as.integer == 0 && tb_vars_get("f") && tb_value_as_number(*tb_vars_get("f"), 0) >= tb_value_as_number(*m, 0)) break; }
printf("setup + 1o quadro: %.3f s (%d quadros do editor)\n", now() - t0, n);
double best = 1e9, sum = 0;
for (int k = 0; k < frames; k++) {
tb_vars_set("mudou", tb_value_integer(1));
const char* cam = getenv("BENCH_CAM"); /* "cx,cy,cz,pit": camera fixa */
if (cam) {
double c[4] = {0};
sscanf(cam, "%lf,%lf,%lf,%lf", &c[0], &c[1], &c[2], &c[3]);
const char* nm[4] = { "cx", "cy", "cz", "pit" };
for (int q = 0; q < 4; q++) tb_vars_set(nm[q], tb_value_real(c[q]));
}
tb_vars_set("yaw", tb_value_integer(k * (getenv("BENCH_STEP") ? atoi(getenv("BENCH_STEP")) : 3)));
double a = now();
t_eval = t_draw = 0;
int m = 0;
do { frame(); m++; } while (tb_vars_get("mudou")->as.integer == 1 || tb_value_as_number(*tb_vars_get("f"), 0) < tb_value_as_number(*tb_vars_get("nf"), 0));
/* o render termina quando f chega em nf de novo */
double dt = now() - a;
sum += dt; if (dt < best) best = dt;
printf("render %2d: %.1f ms (%d quadros do editor; VM+entrada %.1f ms, desenho %.1f ms = %.1f ms/quadro)\n",
k, dt * 1000, m, t_eval * 1000, t_draw * 1000, t_draw * 1000 / m);
}
const char* show[] = { "nf", "f", "tela", "x0", "pyy", "ok", "vis" };
for (int i = 0; i < 7; i++) {
const TinyBlocks_Value* v = tb_vars_get(show[i]);
char buf[64] = "(nao existe)";
if (v) tb_value_to_text(*v, buf, sizeof(buf));
printf(" %s = %s\n", show[i], buf);
}
const TinyBlocks_Value* tv = tb_vars_get("tela");
if (tv && tv->kind == TB_VALUE_IMAGE) { /* checksum da tela (FNV-1a) */
const TinyBlocks_Image* im = tv->as.image;
const uint8_t* d = tb_image_data_const(im);
size_t n = (size_t)tb_image_width(im) * tb_image_height(im) * tb_image_channels(im);
uint32_t h = 2166136261u;
for (size_t i = 0; i < n; i++) { h ^= d[i]; h *= 16777619u; }
printf(" checksum da tela: %08x\n", h);
}
printf("media %.1f ms, melhor %.1f ms\n", sum / frames * 1000, best * 1000);
return 0;
}