Behind the scenes in Playdate's lua runtime, is a multi value return creating any garbage? Does it depend on context? Sorry I know this isn't a Playdate specific question, but I had a surprisingly difficult time finding any resources on this.
I'm currently building a library that works with a 3 dimensional coordinate system, and I want to know if I'd get better performance by working with multi values instead of creating tables.
It's an interesting question. Tables are notorious for garbage collection, but iirc only if they change size.
Anyway, you should be able to figure this out using:
At multiple places in your code.
You would probably get better performance in this case since there is no overhead of a table with multiple return values. They can be thought of like inverse of vargs.
Edit: also means you don’t get any of the useful things a table may add. So choose wisely and if need be, pack and unpack.
Thanks for the idea, matt! I went a did a test run, and found that a table will allocate roughly 152 bytes, whereas the multi-return will not allocate anything. I had to try this a couple of times because I kept realizing that my tests were themselves allocating things, so I'd appreciate a second set of eyes on this to make sure it looks right!
My Gist with Code, Results, and Summary
This was done on the simulator, so unless the device runs a different runtime of Lua than the simulator, I think the takeaway here is that mutli-returns of three numbers will not create any garbage.
Using variadic arguments will not cause much overhead beyond what a typical variable would because they’re essentially the same.
Tables are expensive because they’re not that simple and can do things.
Also, your three arguments create garbage. It’s just better handled. It’s weird that I feel so ignored in this lol.
Sorry about that, Eric! I wasn't quite sure what you meant by "a table with multiple return values," as I'm comparing tables against multiple return values. Also I don't know how vargs behaves to begin with, so I wasn't sure if the performance being similar to vargs meant that it allocates or not.
3 arguments are creating garbage? Is there a reason it's not showing up in the garbage collector on my tests?
Hit us with some explanatory code, @ericlewis
I agree with Eric that creating a table and returning it is less efficient that returning multiple values.
If I am not mistaken returning multiple values will be treated similar to a local value and saved is something like a register. It will not create garbage.
However in my opinion this is something that looks like premature optimization. My advice would simply to still return a table if this is something that make more sense in your code.
whatcha mean? If re: garbage, that is probably because they are being used.
Hm, I thought we had a mention of this in the optimization section of the docs, but I guess not. We timed the geometry functions using tables, the custom lua types, and multiple return values, and the multiple values were much faster because you don't have to alloc/dealloc a container for the values. They live on the stack, which usually already has room for them. A lot of the geometry functions have variants that take multiple values instead of geometry types; e.g.