Add builtin table.shallowCopy method

As a Roblox developer, it is currently too hard to performantly shallow copy a table.

Shallow copying comes up fairly often when dealing with immutable datatypes and libraries (e.g. Rodux) - however, shallow copying currently relies on a user-provided implementation that simply iterates over all key-value pairs in a table, and inserts them one-by-one. This is not good for performance when shallow copying large tables, since the new table must resize often while values are being inserted.

In comparison, array tables in lua currently have table.move and table.create, which help solve this problem. My benchmarks with table.move show me that it is approximately 6 times faster for arrays of size 1000 than iterating and assigning keys and values - and this performance boost would likely translate to dictionary tables as well.

It would be a huge boost to the performance of my game (which relies heavily on Rodux and immutability), if there was a native table.shallowCopy method that also pre-allocates the memory required.

22 Likes

Hmm, this seems like it could be extremely fast in C because you can copy and move the memory stack instead of remaking the same table.

Support.

4 Likes