From 853311e5b1c1d9fe9d006e3a4f322e9916764933 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 28 Oct 2024 10:54:36 -0300 Subject: Table rehash can resize only the hash part If there are no integer keys outside the array part, there is no reason to resize it, saving the time to count its elements. Moreover, assignments to non-integer keys will not collapse a table created with 'table.create'. --- testes/nextvar.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'testes') diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 5d8796f7..cee77f76 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -39,13 +39,32 @@ do -- rehash moving elements from array to hash for i = 5, 95 do a[i] = nil end check(a, 128, 0) - a.x = 1 -- force a re-hash - check(a, 4, 8) + a[129] = 1 -- force a re-hash + check(a, 4, 8) -- keys larger than 4 go to the hash part for i = 1, 4 do assert(a[i] == i) end for i = 5, 95 do assert(a[i] == nil) end for i = 96, 100 do assert(a[i] == i) end - assert(a.x == 1) + assert(a[129] == 1) +end + + +do -- growing hash part keeping array part + local a = table.create(1000) + check(a, 1000, 0) + a.x = 10 + check(a, 1000, 1) -- array part keeps its elements +end + + +do -- "growing" length of a prebuilt table + local N = 100 + local a = table.create(N) + for i = 1, N do + a[#a + 1] = true + assert(#a == i) + end + check(a, N, 0) end -- cgit v1.2.3-55-g6feb