@avsm is there some game plan for JS compilation in OCaml 5+? js_of_ocaml multicore support issue references some relevant work by OCaml multicore project participants, but it’s unclear if that work succeeded or where it can be tracked. JS target is important for significant part of OCaml community and I thing some visibility on what’s happening or what’s going to happen with this would be much appreciated.
I’m not aware of any formalised game plan. Originally to get effects in jsoo there was the approach @Armael implemented using CPS which I then tried to port to
4.12+domains+effects (see here). The major downside is that the CPS code is slow (and adds some time to jsoo compilation). I remember reading/being told that this can be improved by doing a selective CPS. Just to clear this wasn’t a planned bit of work, more just me experimenting in my own time :))
EDIT: I’ll add this to that tracking issue
I wonder how critical it is to get multicore working for JSOO, given that JS runtime itself is single-threaded? Doesn’t Lwt’s concurrency model make much more sense for targeting JS? Also considering the fact that JS platform’s ‘native’ concurrency model is JS Promise, so shipping custom concurrency or multicore runtimes on top of that will only add layers of abstraction with no real benefit…
Effects are designated as the unifying force of Lwt/Async dichotomy (if I get it right), and if effects are not going to be supported on JSOO, a bunch of libraries will stay on Lwt because that still works there, continuing the ecosystem split.
Multicore itself seems orthogonal to effects, but imagine a library that wants to be thread safe and uses a bunch of mutexes internally. Nothing actually stops it from working on JSOO if JS runtime supports mutexes (actually this is what Gopherjs is doing - it runs multithreaded code in single threaded scheduler, and libraries designed for multiple threads can work on JS without extra effort in most cases).
From what I understood, having effects would not really change anything w.r.t. Lwt/Async, they might just be ported over to work using effects going forward, but there would be no real change to their APIs.
Maybe I’m misunderstanding?
imagine a library that wants to be thread safe and uses a bunch of mutexes internally.
OCaml and Lwt both ship with a Mutex module (
Lwt_mutex). Do these not work on JSOO?
As I understand it, any code that uses effects at all won’t be compatible with jsoo, regardless of if it exposes or consumes those effects through a monad like LWT or Async.
Right now I think that even the multicore stdlib isn’t compatible.
There’s no really clean answer for jsoo compilation, with the best mechanism currently being a CPS translation which can “thread” the effect handling through the control flow of the program. As @patricoferris observes and has kindly forward ported, there is a prototype version of this that @Armael built while interning at OCaml Labs a few years ago. If someone wants to have a go at building a selective CPS version of this that is more efficient, then that’d be great.
Will wasm adopt the stack switching extension before OCaml 5.0 is out and we have a significant body of code using effect handlers? Only time will tell, as indeed will the number of motivated hackers who can work on either the jsoo or wasm compilation side to help advance the state-of-the-art here.
It would be useful to state the obvious first. If you don’t care about parallel domains and effect handlers, it should be fairly straight-forward to support jsoo complication for OCaml 5.0.
For effect handlers, CPS is one way to compile them. But there are also other strategies that @dhil, Sam Lindley, Bob Atkey, Jeremy Yallop and I explored a few years ago: https://www.dhil.net/research/papers/handlers_js-proweb2018.pdf. The gopherjs strategy for compiling go routines looks like trampolining, which is one of the strategies that we’d explored. Most of these compilation strategies tend to affect the performance of code that doesn’t use effect handlers since the transformations required to support suspending and resuming computations are program-wide. That said, it would be useful to replicate the experiments for OCaml 5.0 on jsoo as our experiments were not on jsoo (which can optimise whole programs) and JS engines get better all the time.
Wasm effect handlers make it almost trivial to compile OCaml effect handlers to them, but that is not something that can replace targeting JS in the short term. For one, there is no OCaml to Wasm compiler yet.
Right but unless I miss something it will break the current
js_of_ocaml model which basically takes any OCaml bytecode to the browser (modulo C primitives/libraries).
Another thing is that according to what was answered to me here, accessing what you are actually interested in accessing when you compile for the browser – that is the browser APIs – doesn’t seem obvious/streamlined yet. I gather WASM is new and thus cool and all that, but without a good FFI to the browsers API it’s just a useless gimmick to me.
Fragmenting the already small OCaml ecosystem even further with the warning “don’t use effects for anything if any transitive user might want to use it in a web browser” feels like a last ditch resort. If we are forced down this road, we should make sure that the stdlib never uses effects, and that “uses effects” is tracked at the library level (via dune?) and enforced at build time so that people don’t accidentally pick up dependencies that use effects.
At Jane Street at least, the Unix and Sys modules are banned in libraries that claim to be jsoo compatible, and this is enforced at compile time (by shadowing those modules with empty modules).
I don’t think that Dune has this yet, but I think it would be valuable.
Edit: ok, not all of sys; lots of those functions are platform independent
I don’t know what the Jane Street libraries do, but from my experience JSOO appears to give you a compile error if you call up functions in Sys or Unix which it has not wrapped, and ditto the Thread module. I imagine JSOO could do the same with respect to effects without great difficulty. I doubt that dune has to do anything.
I imagine that what would make support for effects desirable in JSOO is a situation where Lwt moves from a monadic implementation to effects. At some point I suspect that will happen.
That’s not the point.
Sniffing uses of system/C libraries is easy to do. Sniffing uses of effects, a language construct which could well be used only internally by a library is an entirely different thing.
Of course it will (and it likely already does).
But again that’s not the point. The point is that currently, the powerful promise of
js_of_ocaml a much less reliable tool.
Agreed. Throwing an exception when an unsupported API is called is one thing, but missing entire language features is another.
I am afraid I have lost the ability to understand the point. You seemed to suggest first that there as a difficult in “sniffing” uses of effects, which would be a problem for JSOO. You now seem to suggest above that there isn’t.
I understand that effects are a language construct and not something implemented in a module in the ocaml library. I also understand the desirability of supporting effects in JSOO. If that is all there is to be said then I am not sure there is anything left of substance so let’s leave it here.
Sorry, I defer to @TyOverby which said it in a much more concise and eloquent way than I did :–)
Not in a programming language that supports abstraction.