From 63c8fb0c1ce11a7c9052c09524e04940348a86c2 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Fri, 23 Oct 2026 16:05:38 -0300 Subject: [PATCH] =?UTF-8?q?Listas=20com=20nome=20mut=C3=A1veis=20no=20luga?= =?UTF-8?q?r=20(sem=20c=C3=B3pia);=20vazamento=20no=20"cor=20da=20imagem"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "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 --- include/tinyblocks/tb_json_block.h | 5 ++ libraries/tinyblocks-image/src/tb_imglib.c | 10 ++-- libraries/tinyblocks-ppm/src/tb_ppm.c | 9 ++-- libraries/tinyblocks-vector/src/tb_vector.c | 51 +++++++----------- src/tb_json_block.c | 33 ++++++++++++ tests/stdlib_test.c | 58 +++++++++++++++++++++ 6 files changed, 127 insertions(+), 39 deletions(-) diff --git a/include/tinyblocks/tb_json_block.h b/include/tinyblocks/tb_json_block.h index dba0c97..320c21a 100644 --- a/include/tinyblocks/tb_json_block.h +++ b/include/tinyblocks/tb_json_block.h @@ -52,6 +52,11 @@ TB_API const char* tb_json_as_string(const TinyBlocks_JsonValue* j); /* NULL se TB_API bool tb_json_array_push (TinyBlocks_JsonValue* arr, TinyBlocks_JsonValue* item); TB_API uint32_t tb_json_array_length(const TinyBlocks_JsonValue* arr); TB_API TinyBlocks_JsonValue* tb_json_array_get(const TinyBlocks_JsonValue* arr, uint32_t idx); /* ref nova, ou NULL */ +/* Troca / insere (idx == length acrescenta) / remove o item idx; retem item */ +TB_API bool tb_json_array_set (TinyBlocks_JsonValue* arr, uint32_t idx, TinyBlocks_JsonValue* item); +TB_API bool tb_json_array_insert(TinyBlocks_JsonValue* arr, uint32_t idx, TinyBlocks_JsonValue* item); +TB_API bool tb_json_array_remove(TinyBlocks_JsonValue* arr, uint32_t idx); +TB_API void tb_json_array_clear (TinyBlocks_JsonValue* arr); /* -- Objetos (mutação in-place; use em árvores não compartilhadas) --- */ TB_API bool tb_json_object_set (TinyBlocks_JsonValue* obj, const char* key, TinyBlocks_JsonValue* val); diff --git a/libraries/tinyblocks-image/src/tb_imglib.c b/libraries/tinyblocks-image/src/tb_imglib.c index 92d3897..533ae28 100644 --- a/libraries/tinyblocks-image/src/tb_imglib.c +++ b/libraries/tinyblocks-image/src/tb_imglib.c @@ -109,10 +109,12 @@ static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) { uint8_t r=0,g=0,b=0,a=0; tb_image_get_pixel(in_img(self,rt,0), (int)tb_block_input_number(self,rt,1, 0), (int)tb_block_input_number(self,rt,2, 0), &r,&g,&b,&a); TinyBlocks_JsonValue* arr = tb_json_array(); - tb_json_array_push(arr, tb_json_number(r)); - tb_json_array_push(arr, tb_json_number(g)); - tb_json_array_push(arr, tb_json_number(b)); - tb_json_array_push(arr, tb_json_number(a)); + const uint8_t ch[4] = { r, g, b, a }; + for (int i = 0; i < 4; i++) { /* push retem: solta a nossa referencia */ + TinyBlocks_JsonValue* n = tb_json_number(ch[i]); + tb_json_array_push(arr, n); + tb_json_release(n); + } tb_block_set_output(self, 0, tb_value_json(arr)); tb_json_release(arr); self->dirty = false; diff --git a/libraries/tinyblocks-ppm/src/tb_ppm.c b/libraries/tinyblocks-ppm/src/tb_ppm.c index 2b77f40..eb1f45f 100644 --- a/libraries/tinyblocks-ppm/src/tb_ppm.c +++ b/libraries/tinyblocks-ppm/src/tb_ppm.c @@ -178,9 +178,12 @@ static void eval_getpixel(TinyBlocks_Block* self, TinyBlocks* rt) { uint8_t r = 0, g = 0, b = 0, a = 0; tb_image_get_pixel(img, (int)tb_block_input_number(self, rt, 1, 0), (int)tb_block_input_number(self, rt, 2, 0), &r, &g, &b, &a); TinyBlocks_JsonValue* arr = tb_json_array(); - tb_json_array_push(arr, tb_json_number(r)); - tb_json_array_push(arr, tb_json_number(g)); - tb_json_array_push(arr, tb_json_number(b)); + const uint8_t ch[3] = { r, g, b }; + for (int i = 0; i < 3; i++) { /* push retem: solta a nossa referencia */ + TinyBlocks_JsonValue* n = tb_json_number(ch[i]); + tb_json_array_push(arr, n); + tb_json_release(n); + } tb_block_set_output(self, 0, tb_value_json(arr)); tb_json_release(arr); self->dirty = false; diff --git a/libraries/tinyblocks-vector/src/tb_vector.c b/libraries/tinyblocks-vector/src/tb_vector.c index d3c4bda..cbf6858 100644 --- a/libraries/tinyblocks-vector/src/tb_vector.c +++ b/libraries/tinyblocks-vector/src/tb_vector.c @@ -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; diff --git a/src/tb_json_block.c b/src/tb_json_block.c index b7e719f..4d8acb8 100644 --- a/src/tb_json_block.c +++ b/src/tb_json_block.c @@ -178,6 +178,39 @@ bool tb_json_array_push(TinyBlocks_JsonValue* arr, TinyBlocks_JsonValue* item) { return true; } +bool tb_json_array_set(TinyBlocks_JsonValue* arr, uint32_t idx, TinyBlocks_JsonValue* item) { + if (!arr || arr->kind != TB_JSON_ARRAY || !item || idx >= arr->as.array.count) return false; + tb_json_retain(item); /* antes de soltar: pode ser o mesmo */ + tb_json_release(arr->as.array.items[idx]); + arr->as.array.items[idx] = item; + return true; +} + +bool tb_json_array_insert(TinyBlocks_JsonValue* arr, uint32_t idx, TinyBlocks_JsonValue* item) { + if (!arr || arr->kind != TB_JSON_ARRAY || !item || idx > arr->as.array.count) return false; + if (!tb_json_array_push(arr, item)) return false; /* cresce e retem no fim */ + TinyBlocks_JsonValue** it = arr->as.array.items; + uint32_t n = arr->as.array.count; + memmove(&it[idx + 1], &it[idx], (size_t)(n - 1 - idx) * sizeof(*it)); + it[idx] = item; + return true; +} + +bool tb_json_array_remove(TinyBlocks_JsonValue* arr, uint32_t idx) { + if (!arr || arr->kind != TB_JSON_ARRAY || idx >= arr->as.array.count) return false; + TinyBlocks_JsonValue** it = arr->as.array.items; + tb_json_release(it[idx]); + memmove(&it[idx], &it[idx + 1], (size_t)(arr->as.array.count - idx - 1) * sizeof(*it)); + arr->as.array.count--; + return true; +} + +void tb_json_array_clear(TinyBlocks_JsonValue* arr) { + if (!arr || arr->kind != TB_JSON_ARRAY) return; + for (uint32_t i = 0; i < arr->as.array.count; i++) tb_json_release(arr->as.array.items[i]); + arr->as.array.count = 0; +} + uint32_t tb_json_array_length(const TinyBlocks_JsonValue* arr) { return (arr && arr->kind == TB_JSON_ARRAY) ? arr->as.array.count : 0; } diff --git a/tests/stdlib_test.c b/tests/stdlib_test.c index 7b7bfda..9389fb7 100644 --- a/tests/stdlib_test.c +++ b/tests/stdlib_test.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -253,6 +254,63 @@ int main(void) { tb_block_destroy(li, rt); tb_block_destroy(st, rt); tb_block_destroy(lo, rt); } + /* listas com nome: objetos mutaveis (como no Scratch), sem copia */ + { + run("List.Clear", "nl", NULL, NULL); + run("List.Push", "a", "nl", NULL); + run("List.Push", "c", "nl", NULL); + run("List.InsertAt", "b", "2", "nl"); + run("List.InsertAt", "d", "último", "nl"); + CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[\"a\",\"b\",\"c\",\"d\"]"), "lista com nome: adicione e insira"); + run("List.SetAt", "1", "nl", "A"); + run("List.DeleteAt", "último", "nl", NULL); + CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[\"A\",\"b\",\"c\"]"), "lista com nome: substitua e apague"); + run("List.Assign", "copia", "[1,2]", NULL); + run("List.Push", "3", "copia", NULL); + CHECK(is_list(run("List.Var", "copia", NULL, NULL), "[1,2,3]"), "mude a lista para: guarda uma cópia"); + run("List.Clear", "nl", NULL, NULL); + CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[]"), "apague todos os itens"); + + /* 10 mil itens: ler e trocar um item nao pode custar a lista inteira */ + run("List.Clear", "big", NULL, NULL); + TinyBlocks_Block* push = tb_block_create(rt, tb_rt_find_type(rt, "List.Push")); + TinyBlocks_Value nm = tb_value_text("big"); + tb_block_set_default(push, 1, nm); + for (int i = 0; i < 10000; i++) { + TinyBlocks_Value v = tb_value_integer(i); + tb_block_set_default(push, 0, v); + push->dirty = true; + tb_block_eval(push, rt); + } + TinyBlocks_Block* var = tb_block_create(rt, tb_rt_find_type(rt, "List.Var")); + TinyBlocks_Block* item = tb_block_create(rt, tb_rt_find_type(rt, "List.Item")); + TinyBlocks_Block* set = tb_block_create(rt, tb_rt_find_type(rt, "List.SetAt")); + tb_block_set_default(var, 0, nm); + tb_block_connect(item, 0, var, 0); + tb_block_set_default(set, 1, nm); + clock_t t0 = clock(); + for (int i = 1; i <= 20000; i++) { + TinyBlocks_Value k = tb_value_integer(i % 10000 + 1), v = tb_value_integer(-i); + tb_block_set_default(item, 1, k); + item->dirty = var->dirty = true; + tb_block_eval(item, rt); + tb_block_set_default(set, 0, k); + tb_block_set_default(set, 2, v); + set->dirty = true; + tb_block_eval(set, rt); + } + double secs = (double)(clock() - t0) / CLOCKS_PER_SEC; + TinyBlocks_Value k1 = tb_value_integer(10000); + tb_block_set_default(item, 1, k1); + item->dirty = var->dirty = true; + tb_block_eval(item, rt); + printf(" 20 mil leituras + escritas numa lista de 10 mil: %.3f s\n", secs); + CHECK(is_int(&item->outputs[0], -19999), "lista grande: a escrita vale"); + CHECK(secs < 1.0, "lista grande: ler e trocar um item não copia a lista"); + tb_value_free(&nm); + tb_block_destroy(set, rt); tb_block_destroy(item, rt); tb_block_destroy(var, rt); tb_block_destroy(push, rt); + } + if (last) tb_block_destroy(last, rt); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails);