Second Draft: Coroutines

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Thu Jan 16 22:16:10 UTC 2025


On 16/01/2025 11:08 AM, Jin wrote:
> On Wednesday, 15 January 2025 at 16:19:37 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> Ask Adam all about how much hell it was to do in C# before they added 
>> async/await
> 
> I note that with the advent of async/await in JS, development for the 
> browser turned into hell. And when node-fibers (a native nodejs 
> extension that adds support for coroutines with a stack at runtime) was 
> broken, all hell broke loose on the servers. Briefly about async/await 
> problems:
> 
> - [Low performance due to the inability to properly optimize the code.] 
> (https://page.hyoo.ru/#!=btunlj_fp1tum/ 
> View'btunlj_fp1tum'.Details=%D0%90%D1%81%D0%B8%D0%BD%D1%85%D1%80%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%BA%D0%B5%D0%B9%D1%81)

Well yes, this is generating data using a very simple algorithm.

The overhead of a coroutine is always going to be higher than some basic 
integral instructions.

This is to be expected, this is not what it is good at.

> - [Different colors of functions that virally affect the call stack.] 
> (https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your- 
> function/)

I have this same link in the DIP.

This problem exists because you can yield at an abstraction on top of a 
thread.

It exists in a stackful coroutine such as a Fiber in druntime, just as 
much as it does for a stackless coroutine.

The difference between the two coroutine types, is that a stackless 
coroutine will scream it at you. You are forced to handle it. This is a 
feature to aid in thread safety. It is not a bug or undesirable behavior.

If you do not have thread safety modelled by the compiler, you will mess 
it up. It is too easy to do this. It doesn't matter how how much anyone 
argues "oh just do X", people have been making that argument for C wrt. 
pointers without a length for forever, this is no different. And look at 
how the CVE's keep being created due to it.

> - [Inability to abort a deep subtasks without manually drawing a 
> CancellationToken since await is not owning.](https://hackernoon.com/ 
> why-do-you-need-a-cancellation-token-in-c-for-tasks)

This is a good tool if it is what you need.

I don't see the problem here. If you need a way to break cycles due to 
the use of reference counting, an extra type like this can be a great 
way to handle cancellation that lifetimes or error handling alone cannot do.

> - [The need to reinvent the stack as an AsyncContext.](https:// 
> github.com/tc39/proposal-async-context)

This is just weirdo behavior of JavaScript for globals.

It does not apply to D.



More information about the dip.development mailing list