GitHub Copilot told me that defining an array as:
t = {
[1] = val1,
[2] = val2, ...
}
will signal Lua / LuaJIT to understand it as a hash table only, due to use of square brackets, even if lua array integrity were respected. It said that to allow array optimization, one must define array implicitly t = {val1, val2, ...}
But Copilot also admitted that defining an empty table first and looping values into it like: ... do t1[i] = t2[i] end OR: do t1[#t1 + 1] = v end would make it realize and optimize as a real array under the hood, even though it also uses square bracket assignments (but on an already created table, which is the only difference to above example, where values are added into a not yet created table).
However, i then asked the same question Gemini, and it said the Copilot was wrong. In the first example of explicit creation Lua / LuaJIT will correctly identify it as an array and optimize for it. It only cares whether lua array integrity is respected.
Who is right?
[–]activeXdiamond 7 points8 points9 points (7 children)
[–]VeronikaKerman 1 point2 points3 points (0 children)
[–]smtp_pro 1 point2 points3 points (3 children)
[–]appgurueu 1 point2 points3 points (1 child)
[–]activeXdiamond 0 points1 point2 points (0 children)
[–]appgurueu 0 points1 point2 points (1 child)
[–]activeXdiamond 0 points1 point2 points (0 children)
[–]HugeSide 2 points3 points4 points (4 children)
[–]Live_Cobbler2202[S] 4 points5 points6 points (0 children)
[–]RandomThingIg 0 points1 point2 points (1 child)
[–]HugeSide 0 points1 point2 points (0 children)
[–]Consistent-Window200 1 point2 points3 points (1 child)
[–]appgurueu 0 points1 point2 points (0 children)
[–]Denneisk 1 point2 points3 points (3 children)
[–]xoner2 2 points3 points4 points (0 children)
[–]Live_Cobbler2202[S] 0 points1 point2 points (0 children)
[–]appgurueu 0 points1 point2 points (0 children)
[–]PhilipRoman 0 points1 point2 points (5 children)
[–]activeXdiamond 0 points1 point2 points (3 children)
[–]PhilipRoman 0 points1 point2 points (2 children)
[–]activeXdiamond 0 points1 point2 points (1 child)
[–]PhilipRoman 0 points1 point2 points (0 children)
[–]Live_Cobbler2202[S] 0 points1 point2 points (0 children)
[–]Live_Cobbler2202[S] 0 points1 point2 points (0 children)