It is a pretty simple workaround but I just want to know the technical reason
Can’t say for sure, but my guess is that since the table library insert method is programmed at the low C level (according to lua docs), it thereby bypasses higher level metamethods specifically designed for Lua usage? I could very well be wrong on this, that’s just my intuition. I actually haven’t confirmed if table.insert does not trigger __newindex like you said, but if it’s true, I think that’s a bit dumb in all honesty.
I can’t say with complete certainty either but I have a strong feeling it It is to improve performance by avoiding calls for any metamethods which always have some cost. table.insert()
without the positional argument that shifts the table elements is faster than t[#t+1]
.
This is not the case in vanilla Lua (at least not until version 5.3). However, Luau ttablib.cpp uses lua_rawseti()
at the end of the function behind table.insert()
.
Luau is incredibly optimised. Lua 5.3 table.insert() is more than two times slower.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.