Slip.js is a handwritten asm.js implementation of Slip. The language was originally designed as a variant of Scheme and implemented in C by Prof. T. D'Hondt at the Vrije Universiteit Brussel. This version aims to bring that same language to the web while maintaining similar performance as the original native Slip. For this reason, the interpreter runs on top of asm.js, a very optimizable low-level subset of JavaScript.
How to Remove Document Management Hurdles with X-Docs?
A Performant Scheme Interpreter in asm.js
1. A Performant Scheme interpreter in asm.js
Noah Van Es, Jens Nicolay, Quentin Stievenart, Theo D’Hondt, Coen De Roover
6 April 2016, Symposium on Applied Computing, Pisa
noahves@vub.ac.be @noahvanes github.com/noahvanes
2. Context: JavaScript
‣ Language of the web
‣ “Universal virtual machine”
‣ Supports other languages:
‣ popular compilation target
‣ implementation language for interpreters
3. Context: JavaScript
‣ Language of the web
‣ “Universal virtual machine”
‣ Supports other languages:
‣ popular compilation target
‣ implementation language for interpreters
Problem: performance is unacceptable!
10. Compiling from C to asm.js
‣ Used as a compilation target
‣ Emscripten toolchain
C program LLVM IR asm.js
11. Compiling from C to asm.js
‣ Used as a compilation target
‣ Emscripten toolchain
‣ Portable IR
‣ can be further compiled AOT …
‣ … or executed directly as JS
C program LLVM IR asm.js
12. Integrating asm.js by hand
‣ Integrate handwritten asm.js into existing application
‣ Key questions:
‣ is asm.js human-writable?
‣ can existing interpreters migrate to asm.js?
‣ balance between development effort and
performance?
13. SLIP: Simple Language Implementation Platform
‣ Scheme-like language
‣ Omits non-idiomatic features
‣ http://soft.vub.ac.be/~tjdhondt/PLE
14. SLIP: Simple Language Implementation Platform
‣ Scheme-like language
‣ Omits non-idiomatic features
‣ http://soft.vub.ac.be/~tjdhondt/PLE
15. SLIP: Base implementations
‣ Reference C implementation: ~6000 LOC
‣ Plain JS implementation: ~4000 LOC
‣ Design characteristics:
✓ continuation-passing style
✓ register-machine architecture
✓ low-level memory model
✓ garbage collection
✓ trampoline
✓ unified abstract grammar
21. Development effort
‣ Challenges in writing asm.js by hand
‣ limited type system
‣ explicit typecasts
‣ manual memory management
‣ poor abstraction mechanisms
‣ interface between JS and asm.js
22. Development effort
‣ Challenges in writing asm.js by hand
‣ limited type system
‣ explicit typecasts
‣ manual memory management
‣ poor abstraction mechanisms
‣ interface between JS and asm.js
23. Macros to the rescue
‣ Macros to facilitate writing asm.js by hand
‣ Using the sweet.js macro expander
‣ Macros help to improve:
‣ readability
‣ maintainability
‣ performance
27. Performance comparison with JavaScript
‣ Too much overhead in first iterations
‣ asm3: ~80% faster than original JS implementation
speedup
asm0 asm1 asm2 asm3
0
1
2
3
4
5
28. Performance comparison with C
milliseconds
0
2000
4000
6000
8000
tower-fib nqueens qsort hanoi tak cpstak ctak destruct array1 primes
asm4 native C Emscripten
29. Performance comparison with C
‣ asm4 is ~19% slower than native C
‣ asm4 is ~46% faster than Emscripten
nqueens
qsort
hanoi
tak
cpstak
ctak
destruct
array1
0,0
0,2
0,4
0,6
0,8
1,0
1,2
1,4
1,6
1,8
2,0
native slip.js (asm4) compiled
asm0 asm1 asm2 asm3
slowdown
native C asm4 Emscripten
31. Conclusion
‣ Integrated asm.js into interpreter
‣ Development effort: considerable
‣ more components had to be ported
‣ maintainability & readability improved by macros
‣ Performance: huge improvements
‣ close to native speed on the web!
noahves@vub.ac.be
github.com/noahvanes