Stream: beginners

Topic: platform-large-record-interop


view this post on Zulip parasrah (Aug 02 2022 at 18:47):

Hi all! Is it generally considered a bad practice to pass a large record from the platform as an argument into the main app function? And calling that main function multiple times?

I'm toying around with a game idea, and want the platform to call into the apps update : State, Action -> State function. Every time there is an input (keypress on terminal, etc), said input would be converted to an Action and would call into update to see how it affects the game state. I assume that means that the inputs State and Action would be allocated on the heap, and have noticed significant effort in some of the examples to avoid doing exactly that. Is this a bad idea, and if so how should I go about it instead?

view this post on Zulip Folkert de Vries (Aug 02 2022 at 18:50):

at least today, records and non-recursive tags are fully on the stack, always

view this post on Zulip Folkert de Vries (Aug 02 2022 at 18:51):

under the hood big records are passed as pointers to this stack memory

view this post on Zulip Folkert de Vries (Aug 02 2022 at 18:51):

so, I think it should be fine?

view this post on Zulip parasrah (Aug 02 2022 at 18:53):

Ok great, and there isn't a significant amount of overhead from the platform calling into the app multiple times? From what I've seen in the example they all only call into it once

view this post on Zulip parasrah (Aug 02 2022 at 18:53):

examples*

view this post on Zulip Folkert de Vries (Aug 02 2022 at 18:54):

no, it's just a function

view this post on Zulip Folkert de Vries (Aug 02 2022 at 18:54):

the existing examples don't need the repeated calls, but more complex examples will

view this post on Zulip Folkert de Vries (Aug 02 2022 at 18:54):

it's something we want to support for sure

view this post on Zulip parasrah (Aug 02 2022 at 18:54):

Okay awesome, thanks for answering my questions! I'm not much of a systems dev so this is somewhat new territory for me

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:31):

An extra note on this: currently if you want something generic that is not tied to the platform, like State would likely be, you have to use Box around it. That way, the platform just sees it as a pointer and can pass it along without knowing anything about it.

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:32):

This example is out of date with current roc, but would be relatively easy to update:

Main function signatures: https://github.com/bhansconnect/roc-ecs/blob/trunk/roc-simple-ecs/Package-Config.roc
The platform wrapper and how it interacts with Roc: https://github.com/bhansconnect/roc-ecs/blob/trunk/roc-simple-ecs/roc-ecs-platform.h

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:34):

As a first foray into Roc, this probably would be a bit complicated to get set up, especially if you are not used to lower level languages.

view this post on Zulip Folkert de Vries (Aug 02 2022 at 19:38):

well yes but we're here to help. What language are you using for the host?

view this post on Zulip parasrah (Aug 02 2022 at 19:40):

I'm using rust, and have been dabbling in it for years*

view this post on Zulip parasrah (Aug 02 2022 at 19:41):

Thanks for the example, that was the next problem I was going to tackle, as you're right I would like the State to be opaque to the platform

view this post on Zulip Folkert de Vries (Aug 02 2022 at 19:41):

then stack allocation becomes a bit harder, because the platform needs a place to put the value

view this post on Zulip Folkert de Vries (Aug 02 2022 at 19:42):

and you don't know statically how large it will be

view this post on Zulip parasrah (Aug 02 2022 at 19:44):

So if it's boxed, it would be allocated on the heap instead? Similar to rust?

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:44):

Yes

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:44):

On the heap with a refcount.

view this post on Zulip Folkert de Vries (Aug 02 2022 at 19:45):

yes, or you just sort of guess the maximum size and store into an array of bytes

view this post on Zulip Folkert de Vries (Aug 02 2022 at 19:46):

on the stack

view this post on Zulip parasrah (Aug 02 2022 at 19:46):

Well I will give it a shot, I can always refactor if perf becomes an issue. @Brendan Hansknecht I'm assuming that's your ecs repo? How did your experiment go?

view this post on Zulip Folkert de Vries (Aug 02 2022 at 19:46):

however that might still run into problems with the ABI

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:51):

Yeah, it is my repo, feel free to reach out with questions in general. I can also update it to current Roc if you want (I think it is only minor changes).

The current difference between this and the equivalent code written in c++ is about 5x. So definitely a hit currently. That being said, it is still running so fast my terrible drawing pipeline is the bottleneck. Some details about it in #beginners > ECS. I think Roc should be able to close that gap, but box currently is totatly unoptimized and probably leads to a lot more copying than it should.

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 19:52):

Would need to look into it again to figure out the performance costs.

view this post on Zulip parasrah (Aug 02 2022 at 19:56):

No need to update it for my sake, I'll do some digging into it when I have the chance. For the time being I might just leave the state in the platform until that starts causing me problems. As far as performance, I don't anticipate it mattering as much for an ascii roguelike but the best method is probably just to try it out and see, thanks!

view this post on Zulip Brendan Hansknecht (Aug 02 2022 at 20:54):

Oh, also, I just realized that there is a likely better example in the Roc repo. It is up to date and built with rust: https://github.com/rtfeldman/roc/tree/trunk/examples/breakout

view this post on Zulip Richard Feldman (Aug 02 2022 at 22:15):

https://mobile.twitter.com/roc_lang/status/1554586049417977857 is a demo of both of those platforms!

Here's a demo from @phillyete 2022 of early-stage native desktop apps in Roc, with a bonus demo of cross-compiling Roc applications to self-contained executables that run on other operating systems: https://youtu.be/XnbKVTO0eUE

- Roc (@roc_lang)

Last updated: Jul 06 2025 at 12:14 UTC