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.
This commit is contained in:
Roberto Ierusalimschy
2024-11-29 17:26:20 -03:00
parent 9329eeac3b
commit 002beeebe7
7 changed files with 146 additions and 249 deletions

View File

@@ -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