From 002beeebe79065e03dd9f531bee367e8459e3f64 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 29 Nov 2024 17:26:20 -0300 Subject: New way to keep hints for table length Instead of using 'alimit' for keeping the size of the array and at the same time being a hint for '#t', a table now keeps these two values separate. The Table structure has a field 'asize' with the size of the array, while the length hint is kept in the array itself. That way, tables with no array part waste no space with that field. Moreover, the space for the hint may have zero cost for small arrays, if the array of tags plus the hint still fits in a single word. --- testes/nextvar.lua | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'testes') diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 00e509f8..d1da3cee 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -316,21 +316,6 @@ end local a = {} for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end - --- Table length with limit smaller than maximum value at array -local a = {} -for i = 1,64 do a[i] = true end -- make its array size 64 -for i = 1,64 do a[i] = nil end -- erase all elements -assert(T.querytab(a) == 64) -- array part has 64 elements -a[32] = true; a[48] = true; -- binary search will find these ones -a[51] = true -- binary search will miss this one -assert(#a == 48) -- this will set the limit -assert(select(3, T.querytab(a)) == 48) -- this is the limit now -a[50] = true -- this will set a new limit -assert(select(3, T.querytab(a)) == 50) -- this is the limit now --- but the size is larger (and still inside the array part) -assert(#a == 51) - end --] @@ -344,6 +329,20 @@ assert(#{1, 2, 3, nil, nil} == 3) print'+' +do + local s1, s2 = math.randomseed() + print(string.format( + "testing length for some random tables (seeds 0X%x:%x)", s1, s2)) + local N = 130 + for i = 1, 1e3 do -- create that many random tables + local a = table.create(math.random(N)) -- initiate with random size + for j = 1, math.random(N) do -- add random number of random entries + a[math.random(N)] = true + end + assert(#a == 0 or a[#a] and not a[#a + 1]) + end +end + local nofind = {} a,b,c = 1,2,3 -- cgit v1.2.3-55-g6feb