Predictable Sized Function Stacks for Stackful Coroutine Optimization #1683
Replies: 1 comment
-
On determining the stack size of the functions: This is especially useful in cases in which we have many suspended coroutines and the stack size becomes problematic. On coroutines w/o suspensions, we consume roughly as much stack as we would if we would just create one thread per core. On allocating the spawn frame on the heap: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Yesterday I heard of an interesting way of how Rust knows the size of required stack for functions for its futures. They just calculate it. For this, the following requirements need to be upholded in the language:
The latter requirement sounds very alarming for me, since recursion is never part of the API of the function but the compiler still keeps track of this information. Like function coloring, but you only see the color with a UV flashlight. However, recursion is not completely impossible to achieve here, we just need to start a new heap-allocated task. If someone doesn't want to do that, they can also rewrite the recursion as a loop (which probably also uses a stack data structure).
In Concurrency Hylomorphism, @lucteo talked about weakly structured concurrency, in which case the spawned task needs to allocate its stack on the heap, since it may complete after we escaped from the function.

I was wondering what the performance benefit would be in our case if we knew how much stack a function would be using at most. Then we could allocate potentially much smaller stacks in most cases, so maybe it could be used as an optimization, in the cases when we do have enough information (Hylo is not planning to require monomorphizing as aggressively as Rust does, so this technique is not always applicable). What are your thoughts?
Annotations like
@tailrec
or@norec
could guarantee this optimization can always happen. This exposes some implementation details in the API but it's not something that cannot be worked around inside the function if they need to change those later.Beta Was this translation helpful? Give feedback.
All reactions