Listas com nome mutáveis no lugar (sem cópia); vazamento no "cor da imagem"

"lista [nome]" copiava a lista inteira a cada leitura e adicione / insira /
apague / substitua reconstruíam a lista a cada operação: um Z-buffer ou
qualquer lista grande ficava O(n) por acesso. Agora a lista com nome é um
objeto mutável, como no Scratch ("mude a lista para" continua copiando).
O JSON do core ganha tb_json_array_set / insert / remove / clear.
20 mil leituras + escritas numa lista de 10 mil: 13 ms (test-stdlib).

"cor da imagem" (Image e PPM) vazava os números de cada pixel lido.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 16:05:38 -03:00
1 parent bfa17eecea
commit 63c8fb0c1c
6 files changed
+127 -39

No files matched your search

+19 -32
View File
@@ -453,21 +453,21 @@ static long long in_pos(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, uint
return (k >= 1 && k <= hi) ? k : 0;
}
/* "lista [nome]": copia do conteudo atual */
/* Listas com nome sao objetos mutaveis, como no Scratch: "lista [nome]" e a
* propria lista (sem copia) e adicionar / inserir / apagar / substituir mudam
* a lista no lugar, em tempo constante (inserir/apagar: linear no que vem
* depois). Ler um item de uma lista de 10 mil nao copia 10 mil itens. */
static void eval_var(TinyBlocks_Block* self, TinyBlocks* rt) {
char nm[64];
NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm)));
out_list(self, L ? tb_json_clone(L->arr) : tb_json_array());
if (L) tb_json_retain(L->arr);
out_list(self, L ? L->arr : tb_json_array());
}
static void eval_push(TinyBlocks_Block* self, TinyBlocks* rt) {
char nm[64];
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
if (L) {
TinyBlocks_JsonValue* arr = list_copy(L->arr, 0, UINT32_MAX);
if (arr) push_owned(arr, in_item(self, rt, 0));
named_set(L, arr);
}
if (L) push_owned(L->arr, in_item(self, rt, 0));
self->dirty = false;
}
@@ -475,21 +475,16 @@ static void eval_delete_at(TinyBlocks_Block* self, TinyBlocks* rt) {
char nm[64];
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
if (L) {
uint32_t n = tb_json_array_length(L->arr);
long long k = in_pos(self, rt, 0, n, false);
if (k > 0) {
TinyBlocks_JsonValue* arr = tb_json_array();
for (uint32_t i = 0; arr && i < n; i++)
if ((long long)i + 1 != k) push_owned(arr, tb_json_array_get(L->arr, i));
named_set(L, arr);
}
long long k = in_pos(self, rt, 0, tb_json_array_length(L->arr), false);
if (k > 0) tb_json_array_remove(L->arr, (uint32_t)(k - 1));
}
self->dirty = false;
}
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
char nm[64];
named_set(named_get(in_name(self, rt, 0, nm, sizeof(nm))), tb_json_array());
NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm)));
if (L) tb_json_array_clear(L->arr);
self->dirty = false;
}
@@ -497,15 +492,11 @@ static void eval_insert_at(TinyBlocks_Block* self, TinyBlocks* rt) {
char nm[64];
NamedList* L = named_get(in_name(self, rt, 2, nm, sizeof(nm)));
if (L) {
uint32_t n = tb_json_array_length(L->arr);
long long k = in_pos(self, rt, 1, n, true);
long long k = in_pos(self, rt, 1, tb_json_array_length(L->arr), true);
if (k > 0) {
TinyBlocks_JsonValue* arr = list_copy(L->arr, 0, (uint32_t)(k - 1));
if (arr) {
push_owned(arr, in_item(self, rt, 0));
for (uint32_t i = (uint32_t)(k - 1); i < n; i++) push_owned(arr, tb_json_array_get(L->arr, i));
}
named_set(L, arr);
TinyBlocks_JsonValue* it = in_item(self, rt, 0);
tb_json_array_insert(L->arr, (uint32_t)(k - 1), it);
tb_json_release(it);
}
}
self->dirty = false;
@@ -515,15 +506,11 @@ static void eval_set_at(TinyBlocks_Block* self, TinyBlocks* rt) {
char nm[64];
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
if (L) {
uint32_t n = tb_json_array_length(L->arr);
long long k = in_pos(self, rt, 0, n, false);
long long k = in_pos(self, rt, 0, tb_json_array_length(L->arr), false);
if (k > 0) {
TinyBlocks_JsonValue* arr = tb_json_array();
for (uint32_t i = 0; arr && i < n; i++) {
if ((long long)i + 1 == k) push_owned(arr, in_item(self, rt, 2));
else push_owned(arr, tb_json_array_get(L->arr, i));
}
named_set(L, arr);
TinyBlocks_JsonValue* it = in_item(self, rt, 2);
tb_json_array_set(L->arr, (uint32_t)(k - 1), it);
tb_json_release(it);
}
}
self->dirty = false;