ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.5k stars 2.45k forks source link

Proposal: Event loop redesign #8224

Open kprotty opened 3 years ago

kprotty commented 3 years ago

The current event loop is not ready yet (relatively slow, windows unfinished, bugs/races) and many wish for it to be. From the discord communities at least, there seems to be enough interest to warrant addressing this, with some interested in helping out. My question then is: what do people really want out of the standard libraries event loop? Toying with some designs locally produced incomplete implementations from lack of a direction. In addition to what people want from an event loop, how do they also want it implemented? Here are some example properties:

I'm open to adding more considerations if others have some. Just want to understand the requirements before making any sort of PR.

kprotty commented 1 year ago

@ethindp Zig async is a language feature while the event loop is a library component which uses it. This means an event loop is not required for use in other environments like WASM/embedded as you've noted. Event loop just refers to the abstraction which starts and schedules coroutines (async frames). As for "too complicated", that's too subjective of a criteria; It would be more helpful to list which areas or things you were trying to (or would like to) do that felt confusing instead.

Regarding Rust, the "yield points" are explicit awaits. You also can't "wake up" a Task there. You instead implement generators (called Futures) which take in things called Wakers as a way for you to tell the executor/caller when to poll them again. Learning about async rust (as opposed to relating it to Zig) is probably out of scope for this issue.

I'd disagree regarding not focusing on efficiency early: Event loops aren't something you can "optimize later" as they are limited by your designs. For instance, you can't optimize a select-based API to use io_uring/IOCP efficiently. Nor could you make something like libuv or libdispatch without internal heap allocation.

I agree that people will write their own, but I think its better to at least make doing so easier by providing high performance smaller components like an IO queue, thread pool, timer queue, etc.

He-Pin commented 11 months ago

It's worth to take a look at cats-effect 's design too.

aep commented 10 months ago

This type of zero-copy IO model is common in HPC environments and uses DPDK/efvi/vma/XDP etc.

poking around DPDK right now and super anxious about using zig, because of the possibility that some day it will just start having an opinion about IO and then you end up with both things next to each other and having to sync them, like they do with rust now.