4 ms·
Why Async, especially considering Zig intends to be something like a High Level "portable Assembly"?
by ksec 3y ago
Why Async, especially considering Zig intends to be something like a High Level "portable Assembly"?
- laserbeam 3y agoWaiting for IO is never cool. Also, zig's version of async is pretty low level and still obeys the no hidden control flow mantra (at least how it was implemented until 0.9.0).
- chubot 3y agoHow was it implemented in the past? Is there any overview of the design iterations Zig has gone through? Seems like Rust is still working on this area with the controversial keyword generics
- somethingor 3y agohttps://kristoff.it/blog/zig-colorblind-async-await/ https://kristoff.it/blog/zig-colorblind-async-await/
- hinkley 3y agoI can't tell from the examples. Does Zig's async support closures or is it strictly out of order execution?
- wredue 3y agoZig does not have closures, and probably never will barring someone coming up with a nice, no hidden flow idea for captures.
- laserbeam 3y agoIt's similar to manual control over the current stack. You pause execution, you dump your current stack onto the heap, and restore it later. Control over concurrency, similar to out of order execution. I'm describing it conceptually but I assume the implementation can be/was smarter/faster. I don't think closures have anything to do with it. There was also some debate about how cancelation of an awaitable function built with those primitives would work. I don't think there was a great answer for that yet.
- Dwedit 3y agoHow the heck would that even work... In C#, they implement Await/Async by converting the function into a Class, just like when you use 'yield return'. Control flow is all over the place. Zig is so strict about "no hidden control flow" that you can't even have destructors (code which runs when a variable goes out of scope)
- wredue 3y agoHuh? Lots of language do not have guaranteed to be called at a specific time “destructors”. Additionally, zig provides this functionality with defer and errdefer, giving you the ability to explicitly call a function at the end of scope.
- DaiPlusPlus 3y ago> Lots of language do not have guaranteed to be called at a specific time “destructors”. Those tend to be GC languages though, where deterministic execution is not a hard requirement.
- wredue 3y agoSure, but this is also what defer is for. There isn’t implicitly called destructors because the languages mantra is “no hidden control flow”. RAII et semantics is almost entirely hidden control flow that you just have to “know”. Lots of people hate when they need to “just know” things to fully parse the code they’re reading. You’re free to dislike those decisions, of course. Personally, I like the target of no hidden flow.
- bsder 3y agoI have a love/hate with this. The big problem with defer/errdefer is that I have no way to mark a function as "This function always needs a defer after it and the compiler needs to yell at me if it doesn't exist." It's also sometimes really hard to scope your defer/errdefer correctly. You may have to twist your code inside out because defer/errdefer ends at a block scope while your variable may not (via: "break :blk varname;"). This all bites so hard for things like reference counting. The bugs ... the bugs ... <mumbles huddled up in corner>
- himujjal 3y agoGo through Zig’s Async. Its a bit like co-routines but low level. You are required to keep track of memory allocations and pointers to function “frames”. For someone coming from a high-level language POV, frames were a new concept. But Zig does it so superb. Zig just needs some runtime event loop like Tokio or AsyncIO from Rust to get up and running with its fantastic async model.
- Bigpet 3y agoHaving some form of coroutines or green threads seems like a good idea to me for something low-level. Otherwise countless projects that need something like it will re-implent their own incompable version with longjmp or similar means.
- anonymoushn 3y agoZig's async, like Lua's, lets people write libraries that are generic over async-ness by default. "dump my registers and jump to another stack" is also a legitimate move in assembly (though that's not how this implementation actually works).
- formerly_proven 3y agoI understand the technical reasons (caused by cultural reasons) why Python ended up with colored async but man oh man was it a bad choice for a high-overhead language to do that…
- DaiPlusPlus 3y ago(you’re referring to this article [1], right?) What alternative approach would you have preferred Python do? While I’m not a fan of the coloured-function approach either, I can’t think of a better alternative that checks-off the most boxes (Java’s Loom looks interesting but in-a-way it seems too-good-to-be-true to me, while low-cost approaches like Zig’s would introduce too much complexity for novice users and people wanting something that just-works) [1]: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...
- gpderetta 3y agogevent?
- kissgyorgy 3y agoZig's async implementation is the best of any languages, because you can run them without an event loop and in that case they will be simply synchronous. This completely eliminates the function coloring problem.
- xxpor 3y agoUsually the coloring problem IME goes the other way: you want to run a synchronous/blocking function in an async context. How does Zig deal with that?
- lvass 3y agoFunction calls?
- Nullabillity 3y agoAnd now you're blocking the event loop.
- lvass 3y agoIf you chose to use cooperative concurrency, it's because you can make it cooperate. Use preemption otherwise.
- j16sdiz 3y agoThey have await keyword. This is noop in sync mode
- kprotty 3y agoDid you mean the `nosuspend` keyword? It only asserts nothing suspends rather than there being a "sync" mode.
- dgb23 3y agoDoes that mean the return types are the same when you use async or not on the same function?
- pjmlp 3y agoModula-2, Mesa, Concurrent Pascal, NEWP all had coroutines support, so actually that is something I like in Zig.