lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


        if ({false, [1] = true})[1] then
              return 'LuaJIT'

I'm not sure that the evaluation of the table constructor is documented correctly. Here you assign two different boolean values to the same index [1], this creates a collision and the evaluation order matters.


As I understand, there is no collision.

{item1, ...} stores items in the array part
{[idx1] = item1, ...} _always_ stores items in the hash part (even if you use integer keys without holes)

From "Lua performance tips":

If you write something like{[1] = true, [2] = true, [3] = true}, however, Lua is not smart enough to detect that the given expressions (literal numbers, in this case) describe array indices, so it creates a table with four slots in its hash part, wasting memory and CPU time


$ luac -p -l - <<< 'local t = ({false, [1] = true})'

main <stdin:0,0> (7 instructions at 0x565552f68ae0)
0+ params, 2 slots, 1 upvalue, 1 local, 1 constant, 0 functions
	1	[1]	PREPARATORY	0
	2	[1]	NEWTABLE 	0 1 1	; 1
	3	[1]	EXTRAARG 	0
	4	[1]	LOADFALSE	1
	5	[1]	SETI     	0 1 0k	; true
	6	[1]	SETLIST  	0 1 0
	7	[1]	RETURN   	1 1 1	; 0 out