-
-
Notifications
You must be signed in to change notification settings - Fork 670
Add emscripten fiber.h api #1440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don't see fibers to become relevant any time soon in AS, since JS, and in turn TS, and in turn AS are not designed for in-process threading. The closest thing there is are |
I don't think fibers is good approach for our design. Fibers based on stackful coroutines / green threads and required direct access to shadow stack for saving / restoring context "resume" / "suspend" (wasm hasn't shadow stack and simulation this only for fibers quite unnecessary). Also fibers required event loop (scheduler), threads, atomics, mutex. Which also not always possible due to Wasm MVP hasn't threads and some engines like wasmtime / lucet / wasmer didn't implement this proposal yet. Much better approach is building async / await over generators which based on "asymmetric stackless coroutines". It pretty similar how works async / await in javascript and C#. This could implement similar to regenerator without any requirements for host env and extra proposals. |
Regarding emscripten's |
Agree, true async/await would be really nice to have. But, emscripten fibers are available and ready to use "now". Why not have both? |
@dcodeIO @MaxGraey think I may have created some confusion. Fibers are a single-threaded concept. Multiple fibers run on the same cpu thread. ie multiple fibers can run on the main browser thread. With a scheduler they can swap in/out on the main thread cooperatively. Think of them as virtual threads which can run on the main browser thread. Because they swap in/out cooperatively the performance can be better than real threads, which flush the cpu pipeline when they hit a lock. Granted, for large vector array computations (SIMD) there is no substitute for multiple CPU threads. But, if all we're doing is multiplexing i/o, and manipulating non-vector array data, fibers can perform quite well, and eliminate the problem of "callback hell", much like javascript promises. |
Again, fibers have several problems:
Asymmetric stackless coroutines which actually just codegeneraated state machine and don't required .glue code, scheduler and stack. The main drawback of stackless approach it will be slightly more complicated in implementation |
I'm confused, how does fiber.h mandate a bunch of glue code? From what I can tell, they've already done the hard part. We just need to add it. |
Full implementation of fiber concept on wasm require it. To be more precise, saving and restoring the context of the emulated stack and handling scheduler. |
Right, we have a really nice "working" implementation, why not use it? |
Where?) All we have is emscripten specific implementation which really depend on browser |
In emscripten, which is used to build Assemblyscript, right? |
We don't use emscripten) Only binaryen |
That's great because binaryen is what handles the fibers. https://emscripten.org/docs/api_reference/fiber.h.html?highlight=emscripten_fiber_swap |
binaryen only expose low level mechanism called Asyncify. |
emscripten = clang + binaryen + emsdk
fiber.h sits on top of that layer, right? |
Right. Also js specific glue code which emulate many things like threads, handling fiber's contexts, shadow stack, bind web api, dynamic function casts and etc. AssemblyScript has quite different and own technology stack which use only binaryen from that set. |
what's so different? |
Fibers are a concept used by certain programming languages to implement certain language features. AssemblyScript, however, is based on JavaScript, with its concepts of |
First of all own compiler's frontend and backend. Own standard library. Everything different. AS and emscripten share only Binaryen, but even this library used slightly differently. AS uses own binaryen's pass pipeline and don't apply many specific only for emscripten passes. |
Sounds like someone could make a library called fiber-as. Or maybe fibas. Scheduling tasks like that would be very useful. Probably not meant for the assemblyscript std lib. |
what's better, a "channel" (ie like golang) or a "promise" like javascript. one could argue a channel is simply a promise which has "evolved". A channel can be used in place of a promise. I'm clearly advocating something like goroutines and channels, implemented w/ fiber.h |
so I guess what I'm hearing is we already have low level asyncify hooks as seen here but bringing the abstraction up a little higher is out of the question? |
C++20 coroutines, Rust's coroutines, C# coroutines and at last JavaScript generators and async/await. All that languages have stackless coroutines because it has a lot of advantages. |
Also you could be interesting in comparison results of tokio (stackless coroutines) vs "may" (stackful coroutines) and interesting comment from author |
Would love to have this in the browser ... https://github.com/chriskohlhoff/asio/search?q=co_await&unscoped_q=co_await |
This might also be needed by:
#376
But, I'm thinking having fiber.h will be enough to get started creating an AssemblyScript CSP framework (communicating sequential processes) similar to golang goroutines and channels.
The text was updated successfully, but these errors were encountered: