Variáveis: índice hash e referência resolvida por bloco

Ler uma variável comparava o nome com todas as outras (strcmp linear),
e era o que a VM mais fazia: 60% do tempo num render 3D. Agora cada
tabela tem um índice hash (a ordem do painel continua a mesma).

Além disso, TbVarRef: o bloco resolve o nome uma vez para o slot da
variável, preso ao escopo onde foi resolvido; só resolve de novo se uma
variável for criada/apagada ou o bloco rodar em outro escopo. "valor
de", "mude para" e "mude por" usam a referência.

Render do Minecraft em blocos: 86 ms -> 18 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 17:08:05 -03:00
1 parent bf6c59f80e
commit 146d58b7af
3 files changed
+123 -13

No files matched your search

+94 -9
View File
@@ -4,14 +4,21 @@
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "tinyblocks/tb_vars.h"
typedef struct { char* name; TinyBlocks_Value val; } Var;
typedef struct { char* name; uint32_t hash; TinyBlocks_Value val; } Var;
/* Vetor na ordem de criacao (o painel de variaveis mostra nessa ordem) +
* indice hash (enderecamento aberto, guarda posicao+1; 0 = vazio).
* Ler uma variavel e o que a VM mais faz: sem o indice, cada leitura
* comparava o nome com todas as outras. */
typedef struct {
Var* v;
int n, cap;
int* idx;
int idx_cap; /* potencia de 2, >= 2 * cap */
} VarTable;
struct TbVarScope {
@@ -21,36 +28,72 @@ struct TbVarScope {
};
static VarTable s_global;
static uint32_t s_gen = 1; /* muda quando uma variavel e criada/apagada ou um escopo morre (TbVarRef) */
static TbVarScope* s_cur = NULL;
static TbVarScope* s_live = NULL;
/* -- Tabela --- */
static int table_find(const VarTable* t, const char* name) {
for (int i = 0; i < t->n; i++) if (!strcmp(t->v[i].name, name)) return i;
static uint32_t name_hash(const char* s) {
uint32_t h = 2166136261u; /* FNV-1a */
for (; *s; s++) { h ^= (unsigned char)*s; h *= 16777619u; }
return h;
}
static void index_rebuild(VarTable* t) {
if (!t->idx) return;
memset(t->idx, 0, (size_t)t->idx_cap * sizeof(int));
uint32_t mask = (uint32_t)t->idx_cap - 1;
for (int i = 0; i < t->n; i++) {
uint32_t k = t->v[i].hash & mask;
while (t->idx[k]) k = (k + 1) & mask;
t->idx[k] = i + 1;
}
}
static int table_find_h(const VarTable* t, const char* name, uint32_t h) {
if (!t->idx) return -1;
uint32_t mask = (uint32_t)t->idx_cap - 1;
for (uint32_t k = h & mask; t->idx[k]; k = (k + 1) & mask) {
const Var* x = &t->v[t->idx[k] - 1];
if (x->hash == h && !strcmp(x->name, name)) return t->idx[k] - 1;
}
return -1;
}
static Var* table_put(VarTable* t, const char* name, TinyBlocks_Value v) {
int i = table_find(t, name);
uint32_t h = name_hash(name);
int i = table_find_h(t, name, h);
if (i >= 0) {
tb_value_free(&t->v[i].val);
t->v[i].val = tb_value_clone(v);
TinyBlocks_Value old = t->v[i].val;
t->v[i].val = tb_value_clone(v); /* clona antes: v pode ser o proprio valor */
tb_value_free(&old);
return &t->v[i];
}
if (t->n == t->cap) {
int nc = t->cap ? t->cap * 2 : 16;
Var* nv = realloc(t->v, (size_t)nc * sizeof(Var));
if (!nv) return NULL;
int* ni = calloc((size_t)nc * 2, sizeof(int));
if (!ni) { t->v = nv; t->cap = nc; return NULL; }
free(t->idx);
t->v = nv; t->cap = nc;
t->idx = ni; t->idx_cap = nc * 2;
index_rebuild(t);
}
size_t len = strlen(name);
char* nm = malloc(len + 1);
if (!nm) return NULL;
memcpy(nm, name, len + 1);
s_gen++;
Var* x = &t->v[t->n++];
x->name = nm;
x->hash = h;
x->val = tb_value_clone(v);
uint32_t mask = (uint32_t)t->idx_cap - 1, k = h & mask;
while (t->idx[k]) k = (k + 1) & mask;
t->idx[k] = t->n;
return x;
}
@@ -59,21 +102,28 @@ static void table_remove(VarTable* t, int i) {
tb_value_free(&t->v[i].val);
memmove(&t->v[i], &t->v[i + 1], (size_t)(t->n - i - 1) * sizeof(Var));
t->n--;
index_rebuild(t); /* posicoes mudaram (apagar e raro) */
s_gen++;
}
static void table_clear(VarTable* t) {
while (t->n > 0) table_remove(t, t->n - 1);
s_gen++;
for (int i = 0; i < t->n; i++) { free(t->v[i].name); tb_value_free(&t->v[i].val); }
t->n = 0;
free(t->v);
free(t->idx);
t->v = NULL; t->cap = 0;
t->idx = NULL; t->idx_cap = 0;
}
/* Tabela onde `name` existe, do escopo atual para fora; NULL se nenhuma */
static VarTable* where(const char* name, int* idx) {
uint32_t h = name_hash(name);
for (TbVarScope* s = s_cur; s; s = s->parent) {
int i = table_find(&s->t, name);
int i = table_find_h(&s->t, name, h);
if (i >= 0) { *idx = i; return &s->t; }
}
int i = table_find(&s_global, name);
int i = table_find_h(&s_global, name, h);
if (i >= 0) { *idx = i; return &s_global; }
return NULL;
}
@@ -96,6 +146,7 @@ void tb_vars_scope_free(TbVarScope* s) {
for (TbVarScope* o = s_live; o; o = o->next_live) /* filhos perdem o pai */
if (o->parent == s) o->parent = NULL;
if (s_cur == s) s_cur = NULL;
s_gen++;
table_clear(&s->t);
free(s);
}
@@ -112,6 +163,40 @@ const TinyBlocks_Value* tb_vars_get(const char* name) {
return t ? &t->v[i].val : NULL;
}
/* Cache valido: mesma geracao e o nome confere (o nome do bloco pode mudar) */
static Var* ref_hit(const char* name, const TbVarRef* ref) {
if (!ref || ref->gen != s_gen || ref->scope != s_cur || !ref->table) return NULL;
Var* x = &((VarTable*)ref->table)->v[ref->idx];
if (ref->fixed && ref->name == name) return x; /* nome fixo (codigo compilado) */
return strcmp(x->name, name) == 0 ? x : NULL;
}
const TinyBlocks_Value* tb_vars_get_ref(const char* name, TbVarRef* ref) {
if (!name || !name[0]) return NULL;
Var* x = ref_hit(name, ref);
if (x) return &x->val;
int i;
VarTable* t = where(name, &i);
if (!t) return NULL;
if (ref) { ref->gen = s_gen; ref->scope = s_cur; ref->table = t; ref->idx = i; ref->name = name; }
return &t->v[i].val;
}
void tb_vars_set_ref(const char* name, TinyBlocks_Value v, TbVarRef* ref) {
if (!name || !name[0]) return;
Var* x = ref_hit(name, ref);
if (!x) {
tb_vars_set(name, v);
int i;
VarTable* t = where(name, &i);
if (t && ref) { ref->gen = s_gen; ref->scope = s_cur; ref->table = t; ref->idx = i; ref->name = name; }
return;
}
TinyBlocks_Value old = x->val;
x->val = tb_value_clone(v);
tb_value_free(&old);
}
bool tb_vars_exists(const char* name) { return tb_vars_get(name) != NULL; }
void tb_vars_set(const char* name, TinyBlocks_Value v) {