3 ms·
> Coroutine calling is more fundamental and easier to program Citation needed. For coroutines, each coroutine needs its own stack. That means you have to hav
by csense 14y ago
> Coroutine calling is more fundamental and easier to program
Citation needed.
For coroutines, each coroutine needs its own stack. That means you have to have a dynamic memory system baked into the language. And maybe garbage collection too.
> on today's badly designed CPU's you have to think of coroutines as requiring stack swapping
I can't think of an implementation of yield (let alone general coroutines) that doesn't require a separate stack for each coroutine. I admit that I haven't learned very many of the stranger forgotten architectures that are out there, so I might be blinded by the limitations of a somewhat conventional experience.
But I'm also thinking it might even be provable that each coroutine needs its own stack: Think of a program that has m generator functions, where f_1 calls f_2, f_2 calls f_3, ..., f_{m-1} calls f_m. Each of these subroutines creates n copies of its next-level generator, and steps those generators and yields to the parent unpredictably (for example, depending on input from a user-supplied file). It seems like if m and n are large enough, you'll have no choice but to resort to swapping stacks.
- eru 14y ago> For coroutines, each coroutine needs its own stack. That means you have to have a dynamic memory system baked into the language. And maybe garbage collection too. Why? Just statically give every co-routine it's own stack.
- csense 14y agoThe number of coroutine invocations, and the order in which they're cleaned up, could depend on user input. The former could be unbounded. More formally, I'm pretty sure you can write a program that always halts, but for every pair of large numbers N, K > 0, there are at least K different input values that result in (1) at least N coroutine invocations being simultaneously active, and (2) for each of those K different input values, those invocations finish in a different order.