Skip to content
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

Add emscripten fiber.h api #1440

Closed
unicomp21 opened this issue Aug 18, 2020 · 25 comments
Closed

Add emscripten fiber.h api #1440

unicomp21 opened this issue Aug 18, 2020 · 25 comments
Labels

Comments

@unicomp21
Copy link

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.

@dcodeIO
Copy link
Member

dcodeIO commented Aug 18, 2020

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 WebWorkers, i.e. cooperative processes with copying in between. If you have something in particular in mind in that regard, please elaborate, but note that fibers are very likely not applicable.

@MaxGraey
Copy link
Member

MaxGraey commented Aug 18, 2020

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.

https://www.youtube.com/watch?v=KUhSjfSbINE

@MaxGraey
Copy link
Member

Regarding emscripten's fiber. It's not "real fibers", just some form of single threaded simulation with stack and threads emulation which required a lot of javascript glue code

@unicomp21
Copy link
Author

Agree, true async/await would be really nice to have. But, emscripten fibers are available and ready to use "now". Why not have both?

@unicomp21
Copy link
Author

unicomp21 commented Aug 22, 2020

@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.

@MaxGraey
Copy link
Member

Again, fibers have several problems:

  • required scheduler (even loop) on host
  • required emulation of shadow stack
  • required a lot of glue code

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

@unicomp21
Copy link
Author

unicomp21 commented Aug 22, 2020

required a lot of glue code

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.

@MaxGraey

@MaxGraey
Copy link
Member

MaxGraey commented Aug 22, 2020

I'm confused, how does fiber.h mandate a bunch of glue code?

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. fiber.h is just api declaration. It doesn't do anything

@unicomp21
Copy link
Author

Right, we have a really nice "working" implementation, why not use it?

@MaxGraey
Copy link
Member

MaxGraey commented Aug 22, 2020

Right, we have a really nice "working" implementation

Where?) All we have is emscripten specific implementation which really depend on browser

@unicomp21
Copy link
Author

In emscripten, which is used to build Assemblyscript, right?

@MaxGraey
Copy link
Member

MaxGraey commented Aug 22, 2020

We don't use emscripten) Only binaryen

@unicomp21
Copy link
Author

That's great because binaryen is what handles the fibers.

https://emscripten.org/docs/api_reference/fiber.h.html?highlight=emscripten_fiber_swap

@MaxGraey
Copy link
Member

binaryen only expose low level mechanism called Asyncify.

@unicomp21
Copy link
Author

unicomp21 commented Aug 22, 2020

emscripten = clang + binaryen + emsdk

binaryen only expose low level mechanism called Asyncify.

fiber.h sits on top of that layer, right?

@MaxGraey
Copy link
Member

MaxGraey commented Aug 22, 2020

emscripten = clang + binaryen + emsdk

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.

@unicomp21
Copy link
Author

quite different

what's so different?

@dcodeIO
Copy link
Member

dcodeIO commented Aug 22, 2020

Fibers are a concept used by certain programming languages to implement certain language features. AssemblyScript, however, is based on JavaScript, with its concepts of async and await and WebWorker, none of which require an intermediate concept of fibers. It is right that these can be implemented on top of Binaryen's asyncify. but arguing for some random fibers header from the internet specifically doesn't make any sense.

@MaxGraey
Copy link
Member

MaxGraey commented Aug 22, 2020

what's so different?

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.

@jtenner
Copy link
Contributor

jtenner commented Aug 22, 2020

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.

@unicomp21
Copy link
Author

unicomp21 commented Aug 22, 2020

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

@unicomp21
Copy link
Author

unicomp21 commented Aug 22, 2020

so I guess what I'm hearing is we already have low level asyncify hooks as seen here

#1436

but bringing the abstraction up a little higher is out of the question?

https://github.com/AssemblyScript/assemblyscript/blob/8a55531dfd8527cf96d8557fdc25c3603fa49ae9/std/assembly/bindings/asyncify.ts

@MaxGraey
Copy link
Member

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.

@MaxGraey
Copy link
Member

MaxGraey commented Aug 22, 2020

@unicomp21
Copy link
Author

Would love to have this in the browser ...

https://github.com/chriskohlhoff/asio/search?q=co_await&unscoped_q=co_await

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants