PyPy.js: What? How? Why?

  • Ryan Kelly (@rfkelly)
  • Mozilla Accounts Team
  • 2015-04-10

The Web

Two aspects to the web.

Technology:

  • HTTP
  • HTML
  • CSS
  • JS

Culture:

  • open
  • ubiquitous
  • secure
  • trustworthy

We should try to make the web better! If the web is missing something good, it's a bug... like Python!

PyPy.js

Not the first attempt... at least the 10th!

For the web to support python, there are three criteria:

  1. Compatibility
    1. big numbers
    2. exceptions
    3. access to globals()
    4. etc.
  2. Performance
    1. from test import pystone
    2. pystone.main()
    3. want same-as-system performance
  3. Web-ish-ness
    1. not just a python console in a browser
    2. interaction with JavaScript
    3. import js; js.eval('1 + 1')
    4. js.globals.alert('hi!')

Can we hit all three goals?

PyPy.js = PyPy + Emscripten

PyPy

  • python intrepreter written in "RPython"
  • a toolchain for translating RPython to C
  • end up with machine-native code

Emscripten

  • an LLVM backend that generates JavaScript
  • a simulated POSIX environment, in JavaScript
  • originally for porting games to the web

asm.js

  • "An extraordinarily optimizable, low-level subset of JavaScript"
  • JavaScript is a 32-bit assembly language in disguise

PyPy.js

It's crazy to introduce all these layers, but that's what we've been doing in computing for the last 30 years anyway. The question is, is it worth it?

http://arewepythonyet.com

  • currently about 1.5 faster than typical CPython on the host
  • currently about 10-20 slower than native PyPy
  • currently a lot slower than native JavaScript for DOM manipulation
  • currently about 20 MB of JS to download

Why?

BECAUSE IT'S AWESOME