-
Notifications
You must be signed in to change notification settings - Fork 59
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
How to write Wasm friendly code #28
Comments
I think we can consider it along the same lines as embedded development (at least for now). Another limiting factor right now it that wasm is only a 32 bit target right now. We can't use threads or the like until this becomes accepted and implemented as part of the wasm spec beyond the MVP release. This limits usage of crates with dependencies like crossbeam and rayon that are built around parallelization and lock free data structures. What might be considered good practice for now is letting a library declare a wasm feature flag to As with any project tests to make sure it compiles and runs properly can help ensure that a library is wasm compatible. We should consider figuring out how to best run and test code in a wide variety of contexts such as in an embeded space or in various browsers. Ideally it should just work the same regardless of the environment but as we all know, computers. These are just a few of my thoughts but I'd love to see what others think/point out inconsistencies or wrong things in what I've stated etc. as well as experiences dealing with wasm. Blog posts like this one are a good example of this. The author describes some difficulties with porting the library to wasm but mostly it seemed not too error prone! |
As far as testing goes you can already run your normal Rust One thing that's still missing is a way to write asynchronous tests. This doesn't matter much if your library is pure Rust, but if you want to call out to JavaScript and that JS code is asynchronous (e.g. you use |
Is there Edge, Safari and Firefox support as well or at least planned somehow? Getting it working on every browser that supports it is good rather than letting one web browser dominate the space.
Would something like Neon help out here? Or no? |
It's not an ideal solution, but right now I'm using // Single threaded
if cfg!(target_arch = "wasm32") {
(self.populace.len()..self.amount).map(closure).collect()
// Multi threaded
} else {
(self.populace.len()..self.amount).into_par_iter().map(closure).collect()
} Of course this isn't ideal:
But despite its flaws, it's a solution that works for me right now. An open question is: should we have runtime detection for wasm features (e.g. threads), or is the compile-time |
I agree! I'd like to at least also get headless Firefox support in myself, sooner or later. As for other browsers - it'd be great to have them too, but I have a lot of other higher priority stuff I want to add first. (Although if anyone's interested patches are of course welcome.)
More like being able to supply your own test harness would help, e.g. being able to do something like this (JS testing frameworks like #[test]
fn test_foobar(done: Box<FnOnce>) {
do_something_async.then(move |_| {
done();
});
} In normal Rust this is not necessary since you can just block on anything asynchronous, but in the Web environment you can't since its asynchronicity is inherently cooperative and single-threaded.
It would be great to have some |
Ah okay I see what you meant by the test harness. Maybe |
@koute Did you hear about empterpreter? I didn't look into it, but maybe it is something that might help in this. |
Yeah, I did, but as far as I can see that's only for asm.js (?). I'm fairly certain it would be possible to implement asynchronous tests for the |
The emterpreter is a generated VM (in asm.js) and bytecode. WASM is already a bytecode, so you 'just' need the VM, a wasm interpreter, in any language of your choice (since you can compile the VM to wasm). You could go the emterpreter route, but I extracting it out of emscripten and hooking it up in the way you want seems like a bit of a pain for something you'd eventually throw away. Instead you could 'create' a wasm interpreter directly (since 'creating a wasm interpreter' one may just mean taking an existing one and tweaking it). |
|
@fitzgen For tests they won't be very useful, but maybe for benchmarks they will. But only if they have similar performance characteristics to production environment JITs. |
@est31 this isn't an insurmountable issue, any interpreter would likely need modification anyway to allow pausing (it's not top of many people's feature lists). I jotted down some notes after looking at parity-wasm briefly (thanks, didn't realise it had an interpreter):
You then have a pauseable wasm interpreter for the web and can run tests that assume synchronous behaviour inside nodejs. I don't know if this is a worthy goal or not, just wanted to give some input on how it might be possible. |
Oh I see the actual goal was pausing support to drive async tests. I think the simplest solution would be to add experimental support for an ability to mark functions that return futures as Implementation wise I believe this is much easier than wiring up a whole interpreter inside wasm and handling all the interaction between the interpreter and the javascript environment. In fact there is a hack to enable async support without the need for a JIT/interpreter: just use web workers and let them communicate with the main thread via shared array buffers. While the result for the computation hasn't reached the worker thread yet it will have to busy wait as JavaScript has gotten no sleep function. So it is a hack. But likely faster than running an interpreter :p. Also another downside is that thanks to meltdown+spectre SharedArrayBuffers have been disabled by default. Temporarily they claim and I hope they will be re-enabled some day. For now you need to enable them manually, at least Firefox has an option for this. |
There's no 'good' solution.
Which you think is easiest depends on your own personal metrics and whether you want a solution now or in the long term. |
Folks who are interested in testing related things, should drop a comment with their needs over at rust-lang/rfcs#2318 and see if that eRFC meets them. cc @Manishearth |
we should not track this here- it's definitely a wider issue that is already addressed in the wasm-bindgen book and the tutorial (always can be improved of course! - but we should file more specific issues on the relevant repo) closing. |
Per a comment by @epage on reddit: How can a library author ensure that their library can be used from wasm? What does this mean in particular?
The text was updated successfully, but these errors were encountered: