This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
War of Native Speed on Web
Bekket McClane @ SITCON2016
WHO AM I?
2
Bekket McClane
mshockwave
Department of Computer Science Sophomore
Dormitory Guardian
Android Developer (App/System)
Dragon Slaughter
FACT-1: Web Pages Need
Javascript
FACT-2: Javascript is Pretty Slow
E.g. Web Page Games
ASM.JS
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
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
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
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
TOOL:
C/C++ LLVM EMSCRIPTEN Asm.js
Emscripten Flow
LLVM IR
BENCHMARKS
Lower is Better
Native Client
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
Usage
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