-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend table allocation options (e.g. table fill & resize) #172
Comments
WASM is a bit different because there you can't grow the table by simply writing to it. These aren't difficult to implement, but I'd like to hear the motivation from the ergonomics or performance perspective. For example, when we were adding
So for either of these to be added I think it's important to know what type of code would leverage these and how, because it'll make the case for including or not including these easier. |
This is all makes sense to me. I'll try and do a mock of your bullets for both functions as well as giving some specific generalized use cases... For table fill:
For table resize:
|
Coming back to this, it would be interesting to see examples of code where either resize or fill would be useful. Ideally we'd look at some algorithm implementation that we can then benchmark with or without first-class functions like this to determine how important these would be. Overall I'm hesitant to expose |
I'm going to close this for now, feel free to suggest examples of code where filling a table with the same value is important wrt performance and then we can reevaluate. |
Currently, luau implements two very nice functions for table allocation:
table.move
&table.create
However, luau doesn't offer much support at all for doing our own reallocations of tables, such as filling and shrinking/growing (resizing).
table.move
andtable.create
both are useful for allocating new data, and rearranging existing data, however, these still limit the prior two use cases with overhead.All of these do one-time allocations, and perform bulk table changes, which is important because it allows for reducing the computational cost of a lot of code in a very general way. (Which especially applies to porting many lower level algorithms or assembly langs into luau, but also applies directly to DOD code which benefits the most)
I also got a little carried away with my nerding, decided to embrace it and pretty much wrote a whole entire RFC (a wack one) for this, even though it's probably really excessive. I will probably make a PR since the implementation of the two proposed functions should be quick. Not sure though.
Pretty much an RFC??
Proposed APIs
table.resize(table, newSize)
- Resizes the input array to the input sizePossibly just a wrapper to
luaH_resizearray
with extra sanity and validation?table.fill(table, indexA, indexB, value)
- Writesvalue
fromindexA
, toindexB
into the array, resizing it if necessary.Considered alternatives
I considered
table.move
for trying to perform resizes, but it will only resize a table to allow for all of the elements being moved to fit (plus it's not particularly elegant). This does not allow us to grow tables, or shrink them (which, in comparison would be a bit ambiguous and could be reasonable to address separate to growing). It also does not allow us to do bulk writes to existing tables without allocation overhead (fill).While
table.create
has the desired fill behaviour for tables and could be combined withtable.move
to get a decently close alternative, it allocates a brand new table, which is probably not something we would really like when we might want to be dynamically resizing a table for DOD, say, a big list of debris data. We basically double the amount of writes, and allocate a new, potentially large table.Justification of incompleteness
The implementation of both of these behaviours would make the implementations of both other functions possible to write direct pure luau ports of. For example,
table.create
&table.move
both essentially can be thought of as utilizing both underlying features.This might make the two seem redundant, but I would argue that because all four of these behaviours exist in other places as their own instructions (e.g. WASM) as well as the justification that the reduced overhead is desirable for where any of these functions would be desired to be used.
Because both the
table.move
&table.create
functions don't have direct pure lua ports in luau without requiring excessive allocations, I would argue that this justifies incompleteness, considering that the use cases of the proposed functions are general and used in luau as general utilities, which are non specific.Example in WASM
There are two cases I found being used in a lot of lower level algorithms which also apply generally, table filling, and table shrinking/growing (resizing), for example, WebAssembly has a comparable table system to lua. This system actually has two instructions that luau has,
table.move
is essentially a 1:1 implementation of WASM'stable.copy
instruction, andtable.create
is essentially a 1:1 implementation of WASM'stable.init
instructionWASM offers both the ability to fill, and the ability to (limitedly) resize tables.
The text was updated successfully, but these errors were encountered: