41. foo.bar in JavaScript
found = HashTable.FindEntry(key)
if (found) return found;
for (pt = GetPrototype();
pt != null;
pt = pt.GetPrototype()) {
found = pt.HashTable.FindEntry(key)
if (found) return found;
}
44. Add Type for object
add property y
add property x
http://code.google.com/apis/v8/design.html
45. Inline Cache
• Slow lookup at first time
• Modify the JIT code in-place
• Next time will directly jump to the address
46. Inline cache make simple
return foo.lookupProperty(bar);
function fun(foo) {
return foo.bar;
}
if (foo[hiddenClass] == 0xfe1) {
return foo[indexOf_bar];
}
return foo.lookupProperty(bar);
47. 实际代码中的JS并不会那么动态
Delete操作只占了0.1%
“An Analysis of the Dynamic Behavior of JavaScript...”
99%的原始类型可以在运行通过静态分析确定
97%的属性访问可以被inline cache
“TypeCastor: Demystify Dynamic Typing of JavaScript...”
48. V8 can’t handle delete yet
20x times
slower!
http://jsperf.com/test-v8-delete
67. High-Level IR (Hydrogen)
• AST to SSA
• Type inference (type feedback from inline cache)
• Compiler optimization
• Function inline
• Loop-invariant code motion, Global value numbering
• Eliminate dead phis
• ...
68. Loop-invariant code motion
tmp = x + y;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
a[i] = x + y; a[i] = tmp;
} }
69. Function inline limit for now
• big function (large than 600 bytes)
• have recursive
• have unsupported statements
• with, switch
• try/catch/finally
• ...
71. Built-in objects written in JS
function ArraySort(comparefn) {
...
// In-place QuickSort algorithm.
// For short (length <= 22) arrays, insertion sort is used for efficiency.
if (!IS_SPEC_FUNCTION(comparefn)) {
comparefn = function (x, y) {
if (x === y) return 0;
if (%_IsSmi(x) && %_IsSmi(y)) {
return %SmiLexicographicCompare(x, y);
}
x = ToString(x);
y = ToString(y);
if (x == y) return 0;
else return x < y ? -1 : 1;
};
}
...
v8/src/array.js
79. Node.JS
• Pros • Cons
• Easy to write Async I/O • Lack of great libraries
• One language for everything • Large JS is hard to maintain
• Maybe Faster than PHP, Python • Easy to have Memory leak
(compare to PHP, Erlang)
• Bet on JavaScript is safe
• Still too youth, unproved
80. Why Dart?
• Build for large application
• option type, structured, libraries, tools
• Performance
• lightweight process like erlang
• easy to write a faster vm than javascript
81. The future of Dart?
• It will not replace JS
• But it may replace GWT, and become a better choice for
Building large front-end application
• with great IDE, mature libraries
• and some way to communicate with JavaScript
83. How to make JavaScript faster?
• Wait for ES6: StructType, const, WeakMap, yield...
• High performance build-in library
• WebCL
• Embed another language
• KL(FabricEngine), GLSL(WebGL)
• Wait for Quantum computer :)
84. Things you can learn also
• NaN tagging
• Polymorphic Inline Cache
• Type Inference
• Regex JIT
• Runtime optimization
• ...
85. References
• The behavior of efficient virtual • Context Threading: A Flexible and
machine interpreters on modern Efficient Dispatch Technique for
architectures Virtual Machine Interpreters
• Virtual Machine Showdown: Stack • Effective Inline-Threaded
Versus Registers Interpretation of Java Bytecode
Using Preparation Sequences
• The implementation of Lua 5.0
• Smalltalk-80: the language and its
• Why Is the New Google V8 Engine implementation
so Fast?
86. References
• Design of the Java HotSpotTM • LLVM: A Compilation Framework
Client Compiler for Java 6 for Lifelong Program Analysis &
Transformation
• Oracle JRockit: The Definitive Guide
• Emscripten: An LLVM-to-JavaScript
• Virtual Machines: Versatile Compiler
platforms for systems and
processes • An Analysis of the Dynamic
Behavior of JavaScript Programs
• Fast and Precise Hybrid Type
Inference for JavaScript
87. References
• Adaptive Optimization for SELF • Design, Implementation, and
Evaluation of Optimizations in a
• Bytecodes meet Combinators: Just-In-Time Compiler
invokedynamic on the JVM
• Optimizing direct threaded code by
• Context Threading: A Flexible and selective inlining
Efficient Dispatch Technique for
Virtual Machine Interpreters • Linear scan register allocation
• Efficient Implementation of the • Optimizing Invokedynamic
Smalltalk-80 System
• Threaded Code
88. References
• Why Not a Bytecode VM? • Making the Compilation
"Pipeline" Explicit- Dynamic
• A Survey of Adaptive Compilation Using Trace Tree
Optimization in Virtual Machines Specialization
• An Efficient Implementation of • Uniprocessor Garbage Collection
SELF, a Dynamically-Typed Techniques
Object-Oriented Language Based
on Prototypes
89. References
• Representing Type Information in • The Structure and Performance of
Dynamically Typed Languages Efficient Interpreters
• The Behavior of Efficient Virtual • Know Your Engines: How to Make
Machine Interpreters on Modern Your JavaScript Fast
Architectures
• IE Blog, Chromium Blog, WebKit
• Trace-based Just-in-Time Type Blog, Opera Blog, Mozilla Blog,
Specialization for Dynamic Wingolog’s Blog, RednaxelaFX’s
Languages Blog, David Mandelin’s Blog,
Brendan Eich’s Blog...