War of Native Speed on Web
Bekket McClane @ SITCON2016
Department of Computer Science Sophomore
Dormitory Guardian
Android Developer (App/System)
FACT-1: Web Pages Need
Javascript
FACT-2: Javascript is Pretty Slow
E.g. Web Page Games
BRIEF INTRODUCTION
▸2x~ faster than normal JS
▸NOT a new language (Strict subset of JS)
▸Nearly static type
KEY: IT’S ALL ABOUT
TYPE
KEY: IT’S ALL ABOUT
TYPE
Why types important?
STATIC TYPE
I know how to handle these two variables
STATIC TYPE
Not allowed
I know how to handle these two variables
STATIC TYPE
I know what type it returns
Not allowed
I know how to handle these two variables
DYNAMIC TYPE (Assume No Optimization Applied)
DYNAMIC TYPEHow should I handle x?
(Assume No Optimization Applied)
DYNAMIC TYPEHow should I handle x?
(Assume No Optimization Applied)
DYNAMIC TYPE
What type it returns?
How should I handle x?
(Assume No Optimization Applied)
OPTIMIZE DYNAMIC TYPE▸ Better Type Inference
(At Runtime)
▸ Do As Much Static Analyze
▸ Inline Cache
HOW ASM.JS FIX THIS?
IN SHORT: (Try To) Ensure Each
Variables’ Type
EXAMPLE
x, y are both integers (32-bits)
EXAMPLE
x, y are both integers (32-bits)
So (x + y) has the same type, too
NO MORE TYPE CHECK
FACT: No Object Type
in ASM.js
FACT: No Object Type
in ASM.js
Use Raw Memory To Store Data
AKA: Manage Memory
ManuallyJust Like C/C++
BONUS: No Garbage Collection!!
(No GC Latency)
PROPERTIES▸ Explicit Type
PROPERTIES▸ Explicit Type
▸ No Object Type
PROPERTIES▸ Explicit Type
▸ No Object Type
▸ Manage Memory Manually
PROPERTIES▸ Explicit Type
▸ No Object Type
▸ Manage Memory Manually
Hard To Write Asm.js From Scratch
C/C++ LLVM EMSCRIPTEN Asm.js
Emscripten Flow
LLVM IR
BRIEF INTRODUCTION
▸AOT Compile
▸Portable(PNaCl) / Non-Portable
▸Use LLVM IR
LLVM IR
However,
Native Client Only Use A Subset of LLVM IR
C/C++, Haskell etc. LLVM IR x86, ARM,
MIPS etc.
Platform neutral (In PNaCl)
NaCl‣ Package prebuilt platform-dependent
binary images (one for each architecture).
‣ Only package LLVM bitcode, compile into (platform-dependent) executable before execution.
PNaCl
Manifest Example (NaCl)
Platform Dependent
Manifest Example (PNaCl)
Platform Independent
API
Platform Neutral
Pepper API (For Both C, C++)
HOWEVER…
Wrong Expectation
On LLVM IR
At The Beginning?
LLVM IR is NOT Designed
as Tool for Cross-Platform(e.g. Java bytecode)
FACT:
Evidences (In IR)
Evidences (In IR)▸ Target Triple / Target Data Layout
(e.g. x86_64-apple-macosx10.11.0)
Evidences (In IR)▸ Target Triple / Target Data Layout
(e.g. x86_64-apple-macosx10.11.0)
▸ Explicit Calling Convention
Evidences (In IR)▸ Target Triple / Target Data Layout
(e.g. x86_64-apple-macosx10.11.0)
▸ Explicit Calling Convention
▸ Target Dependent Attributes
BRIEF INTRODUCTION
▸In binary format
▸AST-like structure
▸Interpret, AOT or JIT
USE WITH CAUTIOUS
First Commit: Spring, 2015
Low-Level Instructions
Low-Level Instructions
Explicit Type
AST-like Structure
S-expression
Pros: Fast Parsing (Interpreting)
SandBox Memory Model
One Large Linear Virtual Memory
‣ Simple Memory Access
‣ A Linear Address Space per Module
‣ Secure
Dynamic Link
‣ Useful for CDN (Only need to downloaded once)
‣ Use alternative invoke instructioncall_import
‣ Will Define Proper ABI In Future
v8 support
‣ Run wasm(binary form) in v8!
https://github.com/v8/v8/tree/master/src/wasm
sexpr-wasm-prototype
‣ Turn (human readable) s-expressionTo binary format wasm
‣ Use with v8
https://github.com/WebAssembly/sexpr-wasm-prototype
SUMMARY▸ We still need static type to aid
performance
▸ Binary format would become the future trends
▸ JS would work with other solutions in parallel
FACT: Web Pages Need
Javascript
FACT: Javascript won’t be
the only player anymore
It’s gonna be fun!
Thank you
CONTACT▸ github / bitbucket: mshockwave
▸ mshockwave.blogspot.tw
▸ [email protected]
▸ www.facebook.com/bekket.mcclane