Executor compila expressões puras; texto = TEXTO não lia além do fim
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>
This commit is contained in:
1 parent
146d58b7af
commit
630a1686e5
13 files changed
+694
-152
No files matched your search
@@ -15,6 +15,9 @@
|
||||
#include <tb_control.h>
|
||||
#include <tb_errors.h>
|
||||
#include <tb_operators.h>
|
||||
#include <tb_number.h>
|
||||
#include <tb_string.h>
|
||||
#include <tb_vector.h>
|
||||
#include <tb_variables.h>
|
||||
#include <tb_appearance.h>
|
||||
#include <tb_pen.h>
|
||||
@@ -287,6 +290,9 @@ int main(void) {
|
||||
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_number_get_library());
|
||||
tb_library_register(rt, tb_string_get_library());
|
||||
tb_library_register(rt, tb_vector_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());
|
||||
@@ -1391,6 +1397,35 @@ int main(void) {
|
||||
remove(bigp2);
|
||||
}
|
||||
|
||||
/* 30. Expressoes compiladas (tb_editor_jit.c): mesma saida que no por no */
|
||||
{
|
||||
bool err = false;
|
||||
char* t = mcp("new_tab", "{\"name\":\"Contas\"}", &err, NULL);
|
||||
free(t);
|
||||
t = mcp("add_scratchblocks", "{\"code\":\"when flag clicked\\n"
|
||||
"set [t v] to [abc]\\nset [n v] to (7)\\n"
|
||||
"delete all of [L v]\\nadd (1) to [L v]\\nadd (5) to [L v]\\n"
|
||||
"say (join [a=] ((n) / (2)))\\n"
|
||||
"say (join [b=] (((n) - (1)) * (0.5)))\\n"
|
||||
"say (join [c=] ((t) + (1)))\\n"
|
||||
"say (join [d=] ((item (2) of [L v]) * (10)))\\n"
|
||||
"if <<(t) = [ABC]> and <not <(n) < (5)>>> then\\nsay [e=sim]\\nend\\n"
|
||||
"if <[abd] > (t)> then\\nsay [f=sim]\\nend\\n"
|
||||
"say (join [g=] ((n) * (n)))\\n\",\"x\":40,\"y\":40}", &err, NULL);
|
||||
CHECK(!err, "contas: cria o script");
|
||||
free(t);
|
||||
flag();
|
||||
for (int i = 0; i < 3; i++) frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
CHECK(console_has("a=3.5"), "compilado: (7) / (2) = 3.5");
|
||||
CHECK(console_has("b=3"), "compilado: ((7) - (1)) * (0.5) = 3");
|
||||
CHECK(console_has("c=1"), "compilado: texto numa conta vale 0");
|
||||
CHECK(console_has("d=50"), "compilado: item de lista (não compilado) dentro da conta");
|
||||
CHECK(console_has("e=sim"), "compilado: = compara texto sem maiúsculas; não; e");
|
||||
CHECK(console_has("f=sim"), "compilado: > entre textos compara o texto");
|
||||
CHECK(console_has("g=49"), "compilado: inteiro * inteiro");
|
||||
stop();
|
||||
}
|
||||
|
||||
tinyblocks_deinit(&rt);
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
return fails != 0;
|
||||
|
||||
+3
-3
@@ -19,7 +19,7 @@ int main(void) {
|
||||
const TinyBlocks_BlockMeta* m = tb_meta_find("Math.Add");
|
||||
CHECK(m != NULL, "acha Math.Add");
|
||||
if (m) {
|
||||
CHECK(m->func_name && !strcmp(m->func_name, "eval_add"), "funcao = eval_add");
|
||||
CHECK(m->func_name && !strcmp(m->func_name, "f_add"), "funcao = f_add (a conta pura do bloco)");
|
||||
CHECK(m->doc && strstr(m->doc, "Soma"), "doc menciona 'Soma'");
|
||||
CHECK(m->file && strstr(m->file, "tb_operators.c"), "arquivo tb_operators.c");
|
||||
}
|
||||
@@ -35,11 +35,11 @@ int main(void) {
|
||||
char path[1200];
|
||||
CHECK(tb_meta_resolve_file(m->file, path, sizeof(path)), "resolve o arquivo-fonte");
|
||||
int line = tb_meta_line(m);
|
||||
CHECK(line > 0, "localiza a linha de eval_add");
|
||||
CHECK(line > 0, "localiza a linha de f_add");
|
||||
if (line > 0) {
|
||||
char buf[1024];
|
||||
int got = tb_meta_read_lines(m, line, 1, buf, sizeof(buf));
|
||||
CHECK(got == 1 && strstr(buf, "eval_add"), "le a linha da funcao");
|
||||
CHECK(got == 1 && strstr(buf, "f_add"), "le a linha da funcao");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -102,6 +102,8 @@ int main(void) {
|
||||
CHECK(is_real(run("Math.Add", "0.4", "1", NULL), 1.4), "(0.4) + (1) = 1.4");
|
||||
CHECK(is_real(run("Math.Sub", "-5", "0.25", NULL), -5.25), "(-5) - (0.25) = -5.25");
|
||||
CHECK(is_int(run("Math.Mul", "6", "7", NULL), 42), "inteiro * inteiro continua inteiro");
|
||||
CHECK(is_int(run("Operators.Eq", "abc", "ABC", NULL), 1), "= entre textos ignora maiúsculas (abc = ABC)");
|
||||
CHECK(is_int(run("Operators.Gt", "abd", "abc", NULL), 1), "> entre textos");
|
||||
CHECK(is_int(run("Math.Div", "7", "2", NULL), 3), "Math.Div de inteiros continua divisão inteira");
|
||||
CHECK(is_real(run("Math.Div", "7.5", "2", NULL), 3.75), "Math.Div com real é real");
|
||||
CHECK(is_real(run("Math.Max", "0.5", "0.2", NULL), 0.5) && is_real(run("Math.Abs", "-2.5", NULL, NULL), 2.5),
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
#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;
|
||||
}
|
||||
Reference in new issue
Block a user