On Thursday, 8 October 2015 at 03:12:03 UTC, Martin Nowak wrote:
We might be able to reuse the existing delegate capture mechanism to create continuations. Then await would "simply" rewrite the rest of the body as delegate, similar to how the foreach body can be transformed into a delegate.

Not sure how that works? What is needed is to preallocate a "continuation" that is large enough to cover all "stack allocated" state that has to be retained when the coroutine yields, at all yield points. But with reuse of memory for dead variables (like in optimized stack usage where you reuse stack memory that is dead). Then use those areas of the continuation during computation rather than the stack.

So basically all functions that may yield have to be available for static analysis before you start up the coroutine and all reachable yield points have to be analysed. So you basically have one big stack frame at the "top of the stack" as a separate object (the "continuation") and everything that has to survive a yield has to go onto that frame.

That's how I would do it anyway.

There is some use-case for HPC code where stackless coroutines make a huge differences (used with a work stealing scheduler), for basic networking code it will only be a small difference.

The current D implementation can run out of stack. That's much less of an issue if the system/thread stack is used in combination with a fixed size preallocated "continuation".

Reply via email to