5

There's a long list of languages that can be compiled into Wasm. Is there any performance gain from writing in something like C or Rust over Python? Or is it all the same since it is being compiled to Wasm?

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
user82395214
  • 829
  • 14
  • 37
  • 5
    Depends what you’re using to compile them to WebAssembly and what your code is, but almost certainly yes. – Ry- Dec 06 '19 at 02:57
  • 1
    The lack of garbage collection in C (or C++) and Rust makes them a better fit for WebAssembly. There were plans to expose the native garbage collector but not sure if it has ever materialised. – Selcuk Dec 06 '19 at 03:05

2 Answers2

8

Short answer: Yes, because Python, the language itself, is not compiled to Wasm, but its interpreter.

Saying Python supports Wasm does not always means the same. Firstly, Python is NOT a compiled language, it's a script language. Don't expect a script language will be compiled to a native (or Wasm) language because it is not meant to work that way.

Then how Python supports Wasm? Python interpreters/runtimes like cpython, which is written in C, are compiled to Wasm. There are two popular Python runtimes that supports Python: pyodide and Wasm port for micropython (there are a lot of efforts to run Python in a browser besides the two). Both of them are interpreters that translate Python to their own bytecode and then execute bytecode in Wasm. Of course there will be huge performance penalties just like cpython in the native environment.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
Bumsik Kim
  • 5,853
  • 3
  • 23
  • 39
  • 1
    So is the Python bytecode shipped along with the Python-WASM-interpreter to the client to be executed? – user82395214 Dec 06 '19 at 03:56
  • 1
    No, Python bytecode is generated by the interpreter at runtime. You cannot pre-ship the bytecode but the original script only. That is the performance penalties. – Bumsik Kim Dec 06 '19 at 04:13
  • 1
    "Python is NOT a compiled language." It is not compiled for the CPU it runs on but for the virtual machine that Python provides. – Noctis Skytower Dec 16 '19 at 20:30
3

Compiling to WebAssembly is basically just simulating a special form of assembly targeting virtual hardware. When you read "can compile language X" into Wasm, it doesn't always mean the language literally compiles directly to Wasm. In the case of Python, to my knowledge, it means "they compiled Python interpreters to Wasm" (e.g. CPython, PyPy), so the whole Python interpreter is Wasm, but it still interprets Python source code files normally, it doesn't convert them to special Wasm modules or anything. Which means all the overhead of the Python interpreter is there, on top of the overhead of the Wasm engine, etc.

So yes, C and Rust (which can target Wasm directly by swapping out the compiler backend) will still run faster than Python code targeting CPython compiled to Wasm, for the same reasons. Tools that speed up Python when run natively (e.g. Cython, raw CPython C extensions, etc.) may also work in Wasm to get the same speed ups, but it's not a free "Compile slow interpreted language to Wasm and become fast compiled language"; computers aren't that smart yet.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271