News for hackers. Stuff that matters.
Re:WebAssembly: Docker Without Containers by batmansmk on Wednesday December 21, 2022 @07:15AM (#34079661)
You can compile any C / C++ app down to wasm. In fact that’s the raison d’être of the technology: to provide a portable safe way to run binaries. Here is a link to Postgres in wasm for instance: https://supabase.com/blog/postgres-wasm. The way it works is that instead of outputting assembly for a given architecture in the backend compiler, it outputs wasm instructions that are designed to map all architectures, not like the jvm which is designed primarily for Java in the front end.
Re:WebAssembly: Docker Without Containers by mytherin on Wednesday December 21, 2022 @07:41AM (#34079936)
That's not entirely true. C/C++ code can certainly be compiled down to WASM, but you cannot interface with the operating system as you would in a normal C/C++ program. To get around that restriction postgres-wasm ships an entire Linux distribution that is run inside the browser. This comes with an immense performance penalty.

To get an impression of the performance penalty, just run the following query:

  SELECT SUM(i) FROM generate_series(0, 1000000, 1) tbl(i);

This simple query completes in 100ms locally on my laptop, but takes 17265ms in postgres-wasm. That is a slowdown of 170x.

Now that is not WASM's fault - when running the same query in duckdb-wasm [1] on my laptop the query takes 10ms using WASM, and 5ms when run locally, with a slow-down of only a factor of 2. But in order to achieve those results we did have to adopt the DuckDB codebase to compile natively to WASM. That is absolutely possible but it does take engineering effort - particularly when it comes to larger older projects that are not designed from the ground up with this in mind.

[1] https://shell.duckdb.org

Re:WebAssembly: Docker Without Containers by riddleronroof on Wednesday December 21, 2022 @09:36AM (#34081221)
Thank you for these details. Always suspected these claims but hadn’t dug deep enough.

Seems like some X can now run in wasm should come with disclaimer (includes Linux)

Re:WebAssembly: Docker Without Containers by pjmlp on Wednesday December 21, 2022 @08:09AM (#34080229)
Here, have your C and C++ compiler for JVM bytecode from 2006.

http://nestedvm.ibex.org/

Re:WebAssembly: Docker Without Containers by kenjackson on Wednesday December 21, 2022 @08:12AM (#34080258)
But don’t I have to compile all my dependencies too? This seems like a lot to ask, but maybe it’s common in some domains.
Re:WebAssembly: Docker Without Containers by cle on Wednesday December 21, 2022 @08:34AM (#34080505)
> You can compile any C / C++ app down to wasm.

This is incorrect, there is a long list of limitations that your C/C++ code must conform to in order to compile to WASM. There's a whole section dedicated to this in the Emscripten docs: https://emscripten.org/docs/porting/index.html.

The chances your existing C/C++ app will compile to WASM and run correctly are much smaller than with Docker.

> not like the jvm which is designed primarily for Java in the front end

This is also wrong, JVM bytecode is explicitly designed to be polylingual and is the compilation target for many non-Java languages like Scala, Kotlin, and Clojure. WASM being a compilation target is not what makes it unique from the JVM.

Re:WebAssembly: Docker Without Containers by mike_hearn on Wednesday December 21, 2022 @08:53AM (#34080734)
Ironically you actually can run LLVM bitcode on the JVM these days, including inside an optional sandbox. It can run "anything" in the unsandboxed mode (as long as it can compile with LLVM), because it allows native calls out to the OS. So the universal [J]VM vision has actually happened, it's just nobody really knows about it.

LLVM bitcode isn't all that portable though, and of course, the binary will still be OS specific because C/C++ code relies on native APIs. The primary reason to do this is so the Graal JIT compiler can optimize native code and higher level dynamic script/bytecode together and remove interop overhead.

However, you can theoretically run whole programs this way inside the Graal sandbox. If you do that you get an emulation of POSIX that is reimplemented on top of the Java standard library, so the code becomes portable, and in managed mode there's an additional party trick - the native C/C++ malloc is replaced with garbage collected allocations and memory accesses are bounds checked. So code run this way gets all the memory safety errors blocked automatically. This upgrade comes with two costs though, one is slower execution/more memory usage, and the other is you have to buy GraalVM EE. The community edition can run bitcode, but not in the sandboxed/managed mode.

Oh and GraalVM can also run WASM. So you can have cake and eat it, everything running together via their 'polyglot' interop system.

Re:WebAssembly: Docker Without Containers by jjtheblunt on Wednesday December 21, 2022 @11:49AM (#34083062)
how does pthread_create compile down?
Re:WebAssembly: Docker Without Containers by azakai on Wednesday December 21, 2022 @12:00PM (#34083226)
In Emscripten that uses a pthread implementation layer built on top of Web Workers + a shared wasm Memory. Basically memory is shared, and you have atomic instructions, and each thread of execution gets its own Web Worker.

That has some limitations, but for the most part it works just like you would expect pthreads to.

Re:WebAssembly: Docker Without Containers by jjtheblunt on Wednesday December 21, 2022 @03:33PM (#34086032)
what's the non-most part that differs?
Re:WebAssembly: Docker Without Containers by azakai on Wednesday December 21, 2022 @03:38PM (#34086092)
The main thread is a little special on the Web since it can't block, which can cause issues (like pthread_create doesn't immediately create an available pthread). There are workarounds for most of those issues (like pre-allocating a threadpool), and many applications work well, but sometimes not out of the box. See

https://emscripten.org/docs/porting/pthreads.html#special-co...

Re:WebAssembly: Docker Without Containers by jjtheblunt on Wednesday December 21, 2022 @03:42PM (#34086132)
thank you