Save 37% off PRO during our Black Friday Sale! »

Ryan Kelly: PyPy.js: What? How? Why?

Ryan Kelly: PyPy.js: What? How? Why?

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Ryan Kelly:
PyPy.js: What? How? Why?
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@ Kiwi PyCon 2014 - Sunday, 14 Sep 2014 - Track 1
http://kiwi.pycon.org/

**Audience level**

Intermediate

**Description**

PyPy.js is an experiment in building a fast and compliant in-browser python interpreter, by compiling PyPy into javascript and retargeting its JIT to emit asmjs code at runtime. This talk will demonstrate the combination of technologies that make such a thing possible, the results achieved so far, and the challenges that still remain when taking python onto javascript's home turf.

**Abstract**

PyPy.js is an experiment in building a fast, compliant, in-browser python interpreter. By compiling the PyPy interpreter into javascript, and retargeting its JIT compiler to emit asmjs code at runtime, it is possible to run python code in the browser at speeds competitive with a native python environment. This talk will demonstrate the combination of technologies that make such a thing possible, the results that have been achieved so far, and the challenges that still remain when trying to take python onto javascript's home turf.

We'll cover: an overview of PyPy and why it's a good fit for this type of project; an introduction to asmjs and the rise of javascript as a compile target; what it looks like when you smoosh these two technologies together; a comparison with other approaches such as brython; and some concrete suggestions for how the result might be useful in practice.

**YouTube**

https://www.youtube.com/watch?v=pt-e-X_q-dk

6b880a0b67fac54c42c77fe70d97334d?s=128

New Zealand Python User Group

September 14, 2014
Tweet

Transcript

  1. PyPy.js What? How? Why? Ryan Kelly ryan@rfk.id.au @rfkelly

  2. Mozilla

  3. Mozilla

  4. Mozilla ❤ The Web

  5. The Web Technology: HTTP HTML CSS JavaScript Culture: Open Ubiquitous

    Secure Trustworthy
  6. The Web Cons: HTTP HTML CSS JavaScript Pros: Open Ubiquitous

    Secure Trustworthy
  7. The Web is worth Keeping

  8. The Web is worth Improving

  9. Andreas Gal, Mozilla CTO: "For Mozilla, anything that the Web

    can’t do, or anything that the Web is not faster and better at than native technologies, is a bug." https://bugzilla.mozilla.org/
  10. A Bug in the Web "I want to code in

    Python dammit"
  11. PyPy.js What? How? Why?

  12. What?

  13. PyPy.js: What?

  14. PyPy.js is ... Not the First Pyjamas PyJS Skulpt Brython

    PythonJS RapydScript Pystachio Pyjaco ...?
  15. PyPy.js is ... An Experiment

  16. PyPy.js is ... Compliant

  17. PyPy.js is ... Fast

  18. None
  19. None
  20. None
  21. None
  22. PyPy.js is ... In­Browser v a r v m =

    n e w P y P y J S ( ) ; v m . r e a d y . t h e n ( f u n c t i o n ( ) { . . . } ) v m . s e t ( " x " , 1 2 ) . t h e n ( . . . ) v m . e v a l ( " x = x + 1 " ) . t h e n ( . . . ) v m . g e t ( " x " ) . t h e n ( . . . )
  23. PyPy.js is ... In­Browser

  24. PyPy.js is ... "An experiment in building a fast, compliant,

    in­browser python environment" But...
  25. PyPy.js is ... Not so Fast

  26. PyPy.js is ... Humongous Raw GZip pypy.vm.js 15 MB 2

    MB pypy.vm.js.mem 5.5 MB 1.4 MB
  27. PyPy.js is ... Humongous Raw GZip pypy.vm.js 14 MB 1.8

    MB pypy.vm.js.mem 5.5 MB 1.4 MB
  28. How?

  29. PyPy

  30. PyPy: A Python interpreter, written in "RPython" A toolchain for

    translating RPython to C A JIT­Compiler­Generator
  31. PyPy:

  32. PyPy: PyPy.py

  33. PyPy: PyPy.py PyPy.c rpython

  34. PyPy: PyPy.py PyPy.c PyPy.x86 rpython gcc

  35. PyPy: PyPy.py PyPy.c mycode.py PyPy.x86 rpython gcc

  36. PyPy: PyPy.py PyPy.c mycode.x86 mycode.py PyPy.x86 rpython gcc pypy jit

  37. Emscripten

  38. Emscripten: An LLVM backend that generates JavaScript A simulated POSIX

    environment, in JavaScript Originally for porting games to the web
  39. Emscripten:

  40. Emscripten: mycode.c

  41. Emscripten: mycode.c mycode.js emcc

  42. Emscripten: mycode.c mycode.js mycode.x86 js jit emcc

  43. asm.js "An extraordinarily optimizable, low­level subset of JavaScript"

  44. f u n c t i o n S q

    r t M o d u l e ( s t d l i b , f o r e i g n , h e a p ) { " u s e a s m " ; v a r s q r t = s t d l i b . M a t h . s q r t ; v a r H E A P F 6 4 = n e w s t d l i b . F l o a t 6 4 A r r a y ( h e a p ) ; f u n c t i o n s q r t _ p t r ( a d d r ) { a d d r = a d d r | 0 ; v a r x = 0 . 0 ; x = + H E A P F 6 4 [ a d d r > > 3 ] ; H E A P F 6 4 [ a d d r > > 3 ] = + s q r t ( x ) ; } r e t u r n { s q r t _ p t r : s q r t _ p t r } ; }
  45. PyPy.js: How?

  46. PyPy.js: How? PyPy.py

  47. PyPy.js: How? PyPy.py PyPy.c rpython

  48. PyPy.js: How? PyPy.py PyPy.c PyPy.js rpython emcc

  49. PyPy.js: How? PyPy.py PyPy.c PyPy.js PyPy.x86 rpython js jit emcc

  50. PyPy.js: How? PyPy.py PyPy.c PyPy.js mycode.py PyPy.x86 rpython js jit

    emcc
  51. PyPy.js: How? PyPy.py PyPy.c PyPy.js mycode.js mycode.py PyPy.x86 rpython js

    jit emcc pypy jit
  52. PyPy.js: How? PyPy.py PyPy.c PyPy.js mycode.js mycode.x86 mycode.py PyPy.x86 rpython

    js jit emcc pypy jit js jit
  53. Awful?

  54. PyPy.js: How? PyPy.py PyPy.c PyPy.js mycode.js mycode.x86 mycode.py PyPy.x86 rpython

    js jit emcc pypy jit js jit
  55. PyPy.js: How? PyPy.py PyPy.c PyPy.js mycode.js mycode.x86 mycode.py PyPy.x86 rpython

    js jit emcc pypy jit js jit cpu micro-ops prediction pipelining ...
  56. It's awful all the way down

  57. Why?

  58. PyPy.js: Why? http://www.smbc­comics.com/index.php?id=2088

  59. PyPy.js: Why? http://www.smbc­comics.com/index.php?id=2088

  60. PyPy.js: Why? < s c r i p t t

    y p e = " t e x t / p y t h o n " > p r i n t ( " b u h - b y e j a v a s c r i p t ! " ) < / s c r i p t >
  61. You're gonna have a bad time

  62. Consider Alternatives http://brython.info http://github.com/PythonJS

  63. py2exe

  64. py2web

  65. IPython

  66. Free Stuff!

  67. Please, file bugs against the Web

  68. ryan@rfk.id.au @rfkelly http://pypyjs.org/ http://github.com/rfk/talk­pypyjs­what­how­why

  69. None