Traço novo no core: tb_type_block_set_pure (TB_PURE_NUM/LT/EQ/GT/AND/ OR/NOT/VAR). A library declara que o bloco é puro e qual a semântica; number e operators definem cada conta como uma função de doubles, que é ao mesmo tempo o eval do bloco e o que um executor compila. tb_editor_jit.c: ao montar a árvore do runtime, uma expressão de blocos puros vira um bloco só (Editor.Compiled), com nós nativos (double, variável resolvida para o slot), sem marcar/avaliar/copiar valor em cada nó. O que não é puro (item de lista, Meus Blocos...) entra como nó que avalia a árvore normal. TINYBLOCKS_NO_JIT=1 desliga. Render do Minecraft: 18 ms -> 12 ms, com a mesma imagem (checksum). O "=" e o "<" entre textos passavam do fim da string: o | 32 vinha antes do teste do fim, e "abc" = "ABC" dava falso. tests/vm_bench.c (make bench-vm BENCH=projeto.json) mede a VM sem janela; TINYBLOCKS_VM_MS troca a fatia de tempo da VM por quadro. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
5.5 KiB
C
128 lines
5.5 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)); 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));
|
|
tb_vars_set("yaw", tb_value_integer(k * 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;
|
|
}
|