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:
mguinhosandClaude Opus 5.5 committed 2026-10-23 17:18:08 -03:00
1 parent 630a1686e5
commit b4a27f0180
10 files changed
+294 -40

No files matched your search

+20 -3
View File
@@ -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);