diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-11-29 17:26:20 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-11-29 17:26:20 -0300 |
commit | 002beeebe79065e03dd9f531bee367e8459e3f64 (patch) | |
tree | 5b066d30e08950c923c253ce85f702b39fbe9c31 /testes | |
parent | 9329eeac3b7035c223d7a8b63dbde1f6db371bf5 (diff) | |
download | lua-002beeebe79065e03dd9f531bee367e8459e3f64.tar.gz lua-002beeebe79065e03dd9f531bee367e8459e3f64.tar.bz2 lua-002beeebe79065e03dd9f531bee367e8459e3f64.zip |
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.
Diffstat (limited to 'testes')
-rw-r--r-- | testes/nextvar.lua | 29 |
1 files changed, 14 insertions, 15 deletions
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 | |||
316 | local a = {} | 316 | local a = {} |
317 | for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end | 317 | for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end |
318 | 318 | ||
319 | |||
320 | -- Table length with limit smaller than maximum value at array | ||
321 | local a = {} | ||
322 | for i = 1,64 do a[i] = true end -- make its array size 64 | ||
323 | for i = 1,64 do a[i] = nil end -- erase all elements | ||
324 | assert(T.querytab(a) == 64) -- array part has 64 elements | ||
325 | a[32] = true; a[48] = true; -- binary search will find these ones | ||
326 | a[51] = true -- binary search will miss this one | ||
327 | assert(#a == 48) -- this will set the limit | ||
328 | assert(select(3, T.querytab(a)) == 48) -- this is the limit now | ||
329 | a[50] = true -- this will set a new limit | ||
330 | assert(select(3, T.querytab(a)) == 50) -- this is the limit now | ||
331 | -- but the size is larger (and still inside the array part) | ||
332 | assert(#a == 51) | ||
333 | |||
334 | end --] | 319 | end --] |
335 | 320 | ||
336 | 321 | ||
@@ -344,6 +329,20 @@ assert(#{1, 2, 3, nil, nil} == 3) | |||
344 | print'+' | 329 | print'+' |
345 | 330 | ||
346 | 331 | ||
332 | do | ||
333 | local s1, s2 = math.randomseed() | ||
334 | print(string.format( | ||
335 | "testing length for some random tables (seeds 0X%x:%x)", s1, s2)) | ||
336 | local N = 130 | ||
337 | for i = 1, 1e3 do -- create that many random tables | ||
338 | local a = table.create(math.random(N)) -- initiate with random size | ||
339 | for j = 1, math.random(N) do -- add random number of random entries | ||
340 | a[math.random(N)] = true | ||
341 | end | ||
342 | assert(#a == 0 or a[#a] and not a[#a + 1]) | ||
343 | end | ||
344 | end | ||
345 | |||
347 | local nofind = {} | 346 | local nofind = {} |
348 | 347 | ||
349 | a,b,c = 1,2,3 | 348 | a,b,c = 1,2,3 |