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>
This commit is contained in:
1 parent
630a1686e5
commit
b4a27f0180
10 files changed
+294
-40
No files matched your search
@@ -514,7 +514,7 @@ static int copy_tree(Tab* S, int si, Tab* D, bool with_next) {
|
||||
*n = *o;
|
||||
n->parent = -1; n->prel = REL_NONE; n->next = -1;
|
||||
n->sub[0] = n->sub[1] = n->sub[2] = -1;
|
||||
n->live_rt = NULL; n->dragging = false;
|
||||
n->live_rt = NULL; n->rt_hot = NULL; n->dragging = false;
|
||||
for (int s = 0; s < MAX_INPUTS; s++) n->args[s] = -1;
|
||||
for (int s = 0; s < MAX_INPUTS; s++) {
|
||||
if (o->args[s] < 0) continue;
|
||||
@@ -1254,6 +1254,8 @@ static void run_reset(Ed* ed) {
|
||||
ed->vm_n = 0;
|
||||
ed->vm_running = false;
|
||||
ed->rt_cache_n = 0;
|
||||
ed->rt_gen++;
|
||||
jit_reset(ed);
|
||||
memset(ed->rt_cache, 0, sizeof(ed->rt_cache));
|
||||
for (int i = ed->run_n - 1; i >= 0; i--) tb_block_destroy(ed->run_blocks[i], ed->rt);
|
||||
ed->run_n = 0;
|
||||
@@ -1282,7 +1284,7 @@ TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
if (!t) return NULL;
|
||||
if (t->pure && t->pure != TB_PURE_VAR) { /* expressao pura: compila */
|
||||
if (t->pure >= TB_PURE_NUM && t->pure <= TB_PURE_NOT) { /* expressao pura: compila */
|
||||
TinyBlocks_Block* c = jit_build(ed, T, bi);
|
||||
if (c) return c;
|
||||
}
|
||||
@@ -1313,8 +1315,10 @@ TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
Chamado quando o usuario edita algo, para a edicao valer no programa
|
||||
que esta rodando. */
|
||||
static void rt_cache_clear(Ed* ed) {
|
||||
if (!ed->rt_cache_n) return;
|
||||
if (!ed->rt_cache_n && !ed->jit_n) return;
|
||||
ed->rt_cache_n = 0;
|
||||
ed->rt_gen++;
|
||||
jit_reset(ed);
|
||||
memset(ed->rt_cache, 0, sizeof(ed->rt_cache));
|
||||
}
|
||||
|
||||
@@ -1331,6 +1335,12 @@ void rt_mark_dirty(TinyBlocks_Block* r, int depth) {
|
||||
uma recursao nao reusar uma arvore que ainda esta sendo avaliada). */
|
||||
static TinyBlocks_Block* get_rt(Ed* ed, Tab* T, int bi) {
|
||||
int depth = ed->frame_n;
|
||||
EdBlock* b = &T->ws[bi];
|
||||
if (b->rt_hot && b->rt_hot_gen == ed->rt_gen && b->rt_hot_T == T && b->rt_hot_bi == bi &&
|
||||
b->rt_hot_depth == depth) {
|
||||
rt_mark_dirty(b->rt_hot, 0);
|
||||
return b->rt_hot;
|
||||
}
|
||||
uintptr_t h = ((uintptr_t)T >> 4) * 31u + (uintptr_t)bi * 131u + (uintptr_t)depth * 7u;
|
||||
for (int k = 0; k < RT_CACHE_CAP; k++) {
|
||||
RtCacheEnt* e = &ed->rt_cache[(h + (uintptr_t)k) % RT_CACHE_CAP];
|
||||
@@ -1340,11 +1350,14 @@ static TinyBlocks_Block* get_rt(Ed* ed, Tab* T, int bi) {
|
||||
if (r && ed->rt_cache_n < RT_CACHE_CAP * 3 / 4) {
|
||||
e->T = T; e->bi = bi; e->depth = depth; e->r = r;
|
||||
ed->rt_cache_n++;
|
||||
b = &T->ws[bi]; /* montar pode ter crescido o pool */
|
||||
b->rt_hot = r; b->rt_hot_T = T; b->rt_hot_bi = bi; b->rt_hot_depth = depth; b->rt_hot_gen = ed->rt_gen;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
if (e->T == T && e->bi == bi && e->depth == depth) {
|
||||
rt_mark_dirty(e->r, 0);
|
||||
b->rt_hot = e->r; b->rt_hot_T = T; b->rt_hot_bi = bi; b->rt_hot_depth = depth; b->rt_hot_gen = ed->rt_gen;
|
||||
return e->r;
|
||||
}
|
||||
}
|
||||
@@ -1355,6 +1368,7 @@ static TinyBlocks_Block* get_rt(Ed* ed, Tab* T, int bi) {
|
||||
static TinyBlocks_Value eval_reporter(Ed* ed, Tab* T, int bi) {
|
||||
TinyBlocks_Block* r = get_rt(ed, T, bi);
|
||||
if (!r) return tb_value_none();
|
||||
if (jit_is_compiled(r)) return jit_block_value(r); /* numero: sem avaliar/copiar */
|
||||
tb_block_eval(r, ed->rt);
|
||||
return r->output_count > 0 ? tb_value_clone(r->outputs[0]) : tb_value_none();
|
||||
}
|
||||
@@ -1482,6 +1496,7 @@ static bool vm_step(Ed* ed, VmThread* th, bool sync) {
|
||||
Shape sh = shape_of(t);
|
||||
if (sh == SH_HAT || shape_is_value(sh)) continue;
|
||||
if (t->flow) { vm_push(ed, th, T, bi, VF_FLOW); continue; }
|
||||
if ((t->pure == TB_PURE_SET_VAR || t->pure == TB_PURE_CHANGE_VAR) && jit_stmt(ed, T, bi, t)) continue;
|
||||
TinyBlocks_Block* r = get_rt(ed, T, bi);
|
||||
if (r) tb_block_eval(r, ed->rt);
|
||||
if (t->flags & TB_TYPE_REDRAW) ed->redraw_req = true; /* caneta: mostra este quadro */
|
||||
@@ -7679,6 +7694,8 @@ static void editor_instance_deinit(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
free(ed->fdlg.ents);
|
||||
free(ed->sbp_text);
|
||||
free(ed->run_blocks);
|
||||
jit_reset(ed);
|
||||
free(ed->jit_list);
|
||||
free(ed->grp_mem);
|
||||
free(ed->grp_mem_pos);
|
||||
for (int u = 0; u < ed->user_block_n; u++) free(ed->user_blocks[u].snap);
|
||||
|
||||
@@ -36,11 +36,15 @@ typedef struct {
|
||||
TinyBlocks_Value cval; /* J_CONST: valor original (owned) */
|
||||
TinyBlocks_Block* gen; /* J_GEN (dono: run_blocks do editor) */
|
||||
TbVarRef ref; /* J_VAR */
|
||||
const TinyBlocks_Value* vp; /* J_VAR: valor achado, valido com vgen/vscope */
|
||||
uint32_t vgen;
|
||||
const void* vscope;
|
||||
char name[64]; /* J_VAR */
|
||||
} JNode;
|
||||
|
||||
typedef struct {
|
||||
TinyBlocks* rt;
|
||||
const TbVarsEpoch* ep;
|
||||
int n, root;
|
||||
JNode node[];
|
||||
} JProg;
|
||||
@@ -137,6 +141,15 @@ static int compile_block(JCtx* c, int bi) {
|
||||
|
||||
static double jnum(JProg* P, int i);
|
||||
|
||||
/* Valor da variavel: o ponteiro guardado vale enquanto ninguem criar/apagar
|
||||
* variavel nem trocar de escopo (dois inteiros comparados, sem chamada) */
|
||||
static inline const TinyBlocks_Value* jvar(JProg* P, JNode* x) {
|
||||
if (x->vp && x->vgen == P->ep->gen && x->vscope == P->ep->scope) return x->vp;
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
x->vp = v; x->vgen = P->ep->gen; x->vscope = P->ep->scope;
|
||||
return v;
|
||||
}
|
||||
|
||||
/* Bloco nao compilado: reavalia a arvore dele (como o executor faria) */
|
||||
static TinyBlocks_Value gen_eval(JProg* P, JNode* x) {
|
||||
rt_mark_dirty(x->gen, 0);
|
||||
@@ -150,7 +163,7 @@ static TinyBlocks_Value jval(JProg* P, int i, TinyBlocks_Value* tmp) {
|
||||
switch (x->op) {
|
||||
case J_CONST: return x->cval;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
const TinyBlocks_Value* v = jvar(P, x);
|
||||
return v ? *v : tb_value_integer(0);
|
||||
}
|
||||
case J_GEN: return gen_eval(P, x);
|
||||
@@ -170,19 +183,44 @@ static int icmp(const char* a, const char* b) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Numero do no, se for numero; senao o texto dele em txt (sem reavaliar) */
|
||||
static bool jnum_try(JProg* P, int i, double* d, char* txt, size_t cap) {
|
||||
JNode* x = &P->node[i];
|
||||
TinyBlocks_Value v;
|
||||
switch (x->op) {
|
||||
case J_CONST:
|
||||
if (x->k_num) { *d = x->k; return true; }
|
||||
v = x->cval;
|
||||
break;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* p = jvar(P, x);
|
||||
if (!p) { *d = 0; return true; } /* sem variavel = 0 */
|
||||
if (p->kind == TB_VALUE_INTEGER) { *d = (double)p->as.integer; return true; }
|
||||
if (p->kind == TB_VALUE_REAL) { *d = p->as.real; return true; }
|
||||
v = *p;
|
||||
break;
|
||||
}
|
||||
case J_GEN: v = gen_eval(P, x); break;
|
||||
default: *d = jnum(P, i); return true;
|
||||
}
|
||||
if (tb_value_to_number(v, d)) return true;
|
||||
tb_value_to_text(v, txt, cap);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void num_text(double d, char* txt, size_t cap) {
|
||||
TinyBlocks_Value v = (d == floor(d) && fabs(d) < 9e15) ? tb_value_integer((int64_t)d) : tb_value_real(d);
|
||||
tb_value_to_text(v, txt, cap);
|
||||
}
|
||||
|
||||
static int jcmp(JProg* P, JNode* x) {
|
||||
TinyBlocks_Value ta, tb2;
|
||||
TinyBlocks_Value a = jval(P, x->arg[0], &ta);
|
||||
double p, q;
|
||||
bool an = tb_value_to_number(a, &p);
|
||||
char sa[256] = "";
|
||||
if (!an) tb_value_to_text(a, sa, sizeof(sa)); /* antes de avaliar b (pode reusar a saida) */
|
||||
TinyBlocks_Value b = jval(P, x->arg[1], &tb2);
|
||||
bool bn = tb_value_to_number(b, &q);
|
||||
char sa[256], sb[256];
|
||||
bool an = jnum_try(P, x->arg[0], &p, sa, sizeof(sa));
|
||||
bool bn = jnum_try(P, x->arg[1], &q, sb, sizeof(sb));
|
||||
if (an && bn) return (p > q) - (p < q);
|
||||
char sb[256];
|
||||
if (an) tb_value_to_text(a, sa, sizeof(sa));
|
||||
tb_value_to_text(b, sb, sizeof(sb));
|
||||
if (an) num_text(p, sa, sizeof(sa));
|
||||
if (bn) num_text(q, sb, sizeof(sb));
|
||||
return icmp(sa, sb);
|
||||
}
|
||||
|
||||
@@ -200,7 +238,7 @@ static double jnum(JProg* P, int i) {
|
||||
switch (x->op) {
|
||||
case J_CONST: return x->k;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
const TinyBlocks_Value* v = jvar(P, x);
|
||||
if (!v) return 0.0;
|
||||
if (v->kind == TB_VALUE_INTEGER) return (double)v->as.integer;
|
||||
if (v->kind == TB_VALUE_REAL) return v->as.real;
|
||||
@@ -223,14 +261,18 @@ static double jnum(JProg* P, int i) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/* Numero como valor: inteiro se der exato, senao real */
|
||||
static TinyBlocks_Value jit_num_value(double d) {
|
||||
if (isfinite(d) && d == floor(d) && fabs(d) < 9e15) return tb_value_integer((int64_t)d);
|
||||
return tb_value_real(isfinite(d) ? d : 0.0);
|
||||
}
|
||||
|
||||
/* -- Bloco "Editor.Compiled" --- */
|
||||
|
||||
static void eval_compiled(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
JProg* P = self->userdata;
|
||||
double d = P ? jnum(P, P->root) : 0.0;
|
||||
if (isfinite(d) && d == floor(d) && fabs(d) < 9e15) tb_block_set_output(self, 0, tb_value_integer((int64_t)d));
|
||||
else tb_block_set_output(self, 0, tb_value_real(isfinite(d) ? d : 0.0));
|
||||
tb_block_set_output(self, 0, jit_num_value(P ? jnum(P, P->root) : 0.0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
@@ -268,6 +310,7 @@ TinyBlocks_Block* jit_build(Ed* ed, Tab* T, int bi) {
|
||||
JProg* P = r ? tb_alloc(ed->rt, sizeof(JProg) + (size_t)c->n * sizeof(JNode)) : NULL;
|
||||
if (P) {
|
||||
P->rt = ed->rt;
|
||||
P->ep = tb_vars_epoch();
|
||||
P->n = c->n;
|
||||
P->root = root;
|
||||
memcpy(P->node, c->node, (size_t)c->n * sizeof(JNode));
|
||||
@@ -283,3 +326,121 @@ TinyBlocks_Block* jit_build(Ed* ed, Tab* T, int bi) {
|
||||
free(c);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* -- Valor de um bloco compilado, sem set_output/copia (condicoes) --- */
|
||||
|
||||
bool jit_is_compiled(const TinyBlocks_Block* r) {
|
||||
return r && r->type && r->type->eval_fn == eval_compiled;
|
||||
}
|
||||
|
||||
TinyBlocks_Value jit_block_value(TinyBlocks_Block* r) {
|
||||
JProg* P = r->userdata;
|
||||
return jit_num_value(P ? jnum(P, P->root) : 0.0);
|
||||
}
|
||||
|
||||
/* -- Comandos compilados (TB_PURE_SET_VAR / TB_PURE_CHANGE_VAR) ---
|
||||
* "mude [x] para (expr)" e "mude [x] por (expr)" rodam direto: o alvo e
|
||||
* resolvido uma vez para o slot (TbVarRef com nome fixo) e o valor sai do
|
||||
* programa compilado, sem montar/marcar/avaliar a arvore do bloco. */
|
||||
|
||||
typedef struct {
|
||||
int pure;
|
||||
char name[64];
|
||||
TbVarRef ref;
|
||||
JProg* val; /* malloc; nos GEN sao da run_blocks */
|
||||
} JStmt;
|
||||
|
||||
static char s_jit_none; /* "nao compila": nao tenta de novo */
|
||||
#define JIT_NONE ((void*)&s_jit_none)
|
||||
|
||||
static void prog_free_consts(JProg* P) {
|
||||
for (int i = 0; i < P->n; i++)
|
||||
if (P->node[i].op == J_CONST) tb_value_free(&P->node[i].cval);
|
||||
}
|
||||
|
||||
/* Esquece os comandos compilados (o cache do runtime foi esvaziado) */
|
||||
void jit_reset(Ed* ed) {
|
||||
for (int i = 0; i < ed->jit_n; i++) {
|
||||
JStmt* S = ed->jit_list[i];
|
||||
prog_free_consts(S->val);
|
||||
free(S->val);
|
||||
free(S);
|
||||
}
|
||||
ed->jit_n = 0;
|
||||
}
|
||||
|
||||
static JStmt* stmt_compile(Ed* ed, Tab* T, int bi, TinyBlocks_TypeBlock* t) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
if (t->input_desc_count < 2 || b->args[0] >= 0) return NULL; /* nome calculado */
|
||||
const char* nm = slot_src(b, 0);
|
||||
if (!nm[0] || strlen(nm) >= sizeof(((JStmt*)0)->name)) return NULL;
|
||||
if (ed->jit_n == ed->jit_cap) {
|
||||
int nc = ed->jit_cap ? ed->jit_cap * 2 : 64;
|
||||
void** nl = realloc(ed->jit_list, (size_t)nc * sizeof(void*));
|
||||
if (!nl) return NULL;
|
||||
ed->jit_list = nl; ed->jit_cap = nc;
|
||||
}
|
||||
JCtx* c = calloc(1, sizeof(JCtx));
|
||||
if (!c) return NULL;
|
||||
c->ed = ed; c->T = T;
|
||||
int root = compile_slot(c, bi, 1);
|
||||
JStmt* S = NULL;
|
||||
if (!c->fail && root >= 0) {
|
||||
S = calloc(1, sizeof(JStmt));
|
||||
JProg* P = S ? malloc(sizeof(JProg) + (size_t)c->n * sizeof(JNode)) : NULL;
|
||||
if (P) {
|
||||
P->rt = ed->rt;
|
||||
P->ep = tb_vars_epoch();
|
||||
P->n = c->n;
|
||||
P->root = root;
|
||||
memcpy(P->node, c->node, (size_t)c->n * sizeof(JNode));
|
||||
c->n = 0;
|
||||
S->val = P;
|
||||
S->pure = t->pure;
|
||||
strcpy(S->name, nm);
|
||||
S->ref.fixed = true;
|
||||
ed->jit_list[ed->jit_n++] = S;
|
||||
} else {
|
||||
free(S);
|
||||
S = NULL;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < c->n; i++) if (c->node[i].op == J_CONST) tb_value_free(&c->node[i].cval);
|
||||
free(c);
|
||||
return S;
|
||||
}
|
||||
|
||||
static void stmt_run(JStmt* S) {
|
||||
JProg* P = S->val;
|
||||
JNode* x = &P->node[P->root];
|
||||
if (S->pure == TB_PURE_SET_VAR) {
|
||||
TinyBlocks_Value tmp;
|
||||
TinyBlocks_Value v = x->op == J_CONST || x->op == J_VAR || x->op == J_GEN
|
||||
? jval(P, P->root, &tmp)
|
||||
: (tmp = jit_num_value(jnum(P, P->root)));
|
||||
tb_vars_set_ref(S->name, v, &S->ref);
|
||||
return;
|
||||
}
|
||||
double d = x->op == J_CONST ? x->k : jnum(P, P->root);
|
||||
const TinyBlocks_Value* cur = tb_vars_get_ref(S->name, &S->ref);
|
||||
double a = cur ? tb_value_as_number(*cur, 0) : 0;
|
||||
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
|
||||
? tb_value_integer((long long)llround(a + d))
|
||||
: tb_value_real(a + d);
|
||||
tb_vars_set_ref(S->name, nv, &S->ref);
|
||||
}
|
||||
|
||||
bool jit_stmt(Ed* ed, Tab* T, int bi, TinyBlocks_TypeBlock* t) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
int depth = ed->frame_n;
|
||||
if (!(b->jst && b->jst_gen == ed->rt_gen && b->jst_T == T && b->jst_bi == bi && b->jst_depth == depth)) {
|
||||
const char* off = getenv("TINYBLOCKS_NO_JIT");
|
||||
JStmt* S = (off && off[0] && off[0] != '0') ? NULL : stmt_compile(ed, T, bi, t);
|
||||
b = &T->ws[bi];
|
||||
b->jst = S ? (void*)S : JIT_NONE;
|
||||
b->jst_gen = ed->rt_gen; b->jst_T = T; b->jst_bi = bi; b->jst_depth = depth;
|
||||
}
|
||||
if (b->jst == JIT_NONE) return false;
|
||||
stmt_run(b->jst);
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user