Know Your Engines How to Make Your JavaScript Fast

Size: px
Start display at page:

Download "Know Your Engines How to Make Your JavaScript Fast"

Transcription

1 Know Your Engines How to Make Your JavaScript Fast Dave Mandelin 8 November 2011 O Reilly Velocity Europe

2 JavaScript is getting really fast and programs are getting crazy Bullet physics engine (ammo.js) H.264 decoder at 30 fps (broadway.js)

3 But it s still easy to go slow function f() { var sum = 0; for (var i = 0; i < N; ++i) { sum += i; function f() { eval( ); var sum = 0; for (var i = 0; i < N; ++i) { sum += i;

4 But it s still easy to go slow function f() { var sum = 0; for (var i = 0; i < N; ++i) { sum += i; function f() { eval( ); var sum = 0; for (var i = 0; i < N; ++i) { sum += i; 0 without eval with eval with eval( ) up to 10x slower!

5 Making JavaScript Fast Or, Not Making JavaScript Slow How JITs make JavaScript not slow How not to ruin animation with pauses How to write JavaScript that s not slow

6 The 2006 JavaScript Engine

7 Inside the 2006 JS Engine DOM Front End Interpreter Standard Library Garbage Collector

8 Inside the 2006 JS Engine // JavaScript source e.innerhtml = n + items ; DOM Front End Interpreter Standard Library Garbage Collector

9 Inside the 2006 JS Engine // JavaScript source e.innerhtml = n + items ; DOM Front End Interpreter Standard Library // bytecode (AST in some engines) tmp_0 = add var_1 str_3 setprop var_0 innerhtml tmp_0 Garbage Collector

10 Inside the 2006 JS Engine // JavaScript source e.innerhtml = n + items ; DOM Front End Interpreter Standard Library Run the bytecode // bytecode (AST in some engines) tmp_0 = add var_1 str_3 setprop var_0 innerhtml tmp_0 Garbage Collector

11 Inside the 2006 JS Engine // JavaScript source e.innerhtml = n + items ; DOM Front End Interpreter Standard Library Run the bytecode Reclaim memory // bytecode (AST in some engines) tmp_0 = add var_1 str_3 setprop var_0 innerhtml tmp_0 Garbage Collector

12 Inside the 2006 JS Engine Set innerhtml // JavaScript source e.innerhtml = n + items ; DOM Front End Interpreter Standard Library Run the bytecode Reclaim memory // bytecode (AST in some engines) tmp_0 = add var_1 str_3 setprop var_0 innerhtml tmp_0 Garbage Collector

13 Why it s hard to make JS fast Because JavaScript is an untyped language. untyped = no type declarations

14 Operations in an untyped language x = y + z can mean many things if y and z are numbers, numeric addition if y and z are strings, concatenation and many other cases; y and z can have different types

15 Engine-Internal Types JS engines use finer-grained types internally. JavaScript type number object

16 Engine-Internal Types JS engines use finer-grained types internally. JavaScript type number Engine type 32-bit* integer 64-bit floating-point object

17 Engine-Internal Types JS engines use finer-grained types internally. JavaScript type number Engine type 32-bit* integer 64-bit floating-point object { a: 1 { a: 1, b: 2 { a: get... { a: 1, proto = new C

18 Engine-Internal Types JS engines use finer-grained types internally. JavaScript type number Engine type 32-bit* integer 64-bit floating-point object { a: 1 { a: 1, b: 2 { a: get... { a: 1, proto = new C Different shapes

19 Values in an untyped language Because JavaScript is untyped, the interpreter needs boxed values. Boxed Unboxed Purpose Storage Computation Examples (INT, 55) 55 (STRING, foo ) foo Definition (type tag, C++ value) C++ value only boxed values can be stored in variables, only unboxed values can be computed with (+, *, etc)

20 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z:

21 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory

22 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory

23 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action

24 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action unbox y and z

25 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action

26 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x

27 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the boxed output x to memory

28 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action unbox y and z This is the only real work! execute the action box the output x write the boxed output x to memory

29 Running Code in the Interpreter Here s what the interpreter must do to execute x = y + z: read the operation x = y + z from memory read the boxed inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the boxed output x to memory This is the only real work! Everything else is overhead.

30 The 2011 JavaScript Engine

31 Inside the 2011 JS Engine JavaScript source Interpreter Garbage Collector DOM Standard Library Front End bytecode/ast

32 Inside the 2011 JS Engine Interpreter Garbage Collector DOM JavaScript source Standard Library Front End Untyped Compiler Compile to x86/x64/arm bytecode/ast

33 Inside the 2011 JS Engine Interpreter Garbage Collector DOM JavaScript source Standard Library Front End Untyped Compiler Fast! x86/x64/arm Compile to x86/x64/arm CPU bytecode/ast

34 Inside the 2011 JS Engine Interpreter Garbage Collector DOM JavaScript source Standard Library Front End Untyped Compiler Fast! x86/x64/arm Compile to x86/x64/arm CPU bytecode/ast Typed Compiler Ultra Fast!

35 Inside the 2011 JS Engine Interpreter Garbage Collector DOM JavaScript source Standard Library Front End Untyped Compiler Fast! x86/x64/arm Compile to x86/x64/arm CPU bytecode/ast Typed Compiler Ultra Fast!

36 Inside the 2011 JS Engine THE SLOW ZONE Interpreter Garbage Collector DOM JavaScript source Standard Library Front End Untyped Compiler Fast! x86/x64/arm Compile to x86/x64/arm CPU bytecode/ast Typed Compiler Ultra Fast!

37 Running Code with the JIT The basic JIT compiler on x = y + z: All Major Browsers read the operation x = y + z from memory read the inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the output x to memory

38 Running Code with the JIT The basic JIT compiler on x = y + z: All Major Browsers read the operation x = y + z from memory read the inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the output x to memory CPU does it for us!

39 Running Code with the JIT The basic JIT compiler on x = y + z: All Major Browsers read the operation x = y + z from memory read the inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action CPU does it for us! box the output x write the output x to memory JIT code can keep things in registers

40 Choosing the action in the JIT

41 Choosing the action in the JIT Many cases for operators like +

42 Choosing the action in the JIT Many cases for operators like + Engines generate fast JIT code for common cases number + number string + string

43 Choosing the action in the JIT Many cases for operators like + Engines generate fast JIT code for common cases number + number string + string Rare cases run in the slow zone number + undefined

44 JITs for Regular Expressions There is a separate JIT for regular expressions Regular expressions are generally faster than manual search Still in the slow zone: Some complex regexes (example: backreferences) Building result arrays (test much faster than exec) All Major Browsers

45 Object Properties function f(obj) { return obj.a + 1;

46 Object Properties function f(obj) { return obj.a + 1; Need to search obj for a property named a slow

47 Object Properties function f(obj) { return obj.a + 1; Need to search obj for a property named a May need to search prototype chain up several levels slow super-slow

48 Object Properties function f(obj) { return obj.a + 1; Need to search obj for a property named a May need to search prototype chain up several levels Finally, once we ve found it, get the property value slow super-slow fast!

49 ICs: a mini-jit for objects All Major Browsers

50 ICs: a mini-jit for objects All Major Browsers Properties become fast with inline caching (we prefer IC)

51 ICs: a mini-jit for objects All Major Browsers Properties become fast with inline caching (we prefer IC) Basic plan:

52 ICs: a mini-jit for objects All Major Browsers Properties become fast with inline caching (we prefer IC) Basic plan: 1. First time around, search for the property in the Slow Zone

53 ICs: a mini-jit for objects All Major Browsers Properties become fast with inline caching (we prefer IC) Basic plan: 1. First time around, search for the property in the Slow Zone 2. But record the steps done to actually get the property

54 ICs: a mini-jit for objects All Major Browsers Properties become fast with inline caching (we prefer IC) Basic plan: 1. First time around, search for the property in the Slow Zone 2. But record the steps done to actually get the property 3. Then JIT a little piece of code that does just those steps

55 ICs: Example Example Code var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { return obj.b + 1;

56 ICs: Example Example Code var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { return obj.b + 1; Generated JIT Code... jump slowpropaccess continue_1:... slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

57 Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { return obj.b + 1; ICs: Example Generated JIT Code... jump slowpropaccess continue_1:... slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

58 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { return obj.b + 1; icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 Generated JIT Code... jump slowpropaccess continue_1:... slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

59 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { return obj.b + 1; icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 Generated JIT Code... jump slowpropaccess continue_1:... slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

60 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { return obj.b + 1; icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 Generated JIT Code... jump slowpropaccess continue_1:... slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

61 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; shape=15, in position 0 function f(obj) { return obj.b + 1; icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 Generated JIT Code... jump slowpropaccess continue_1:... slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

62 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; shape=15, in position 0 function f(obj) { return obj.b + 1; Generated JIT Code... jump slowpropaccess continue_1:... icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 icstub_2: compare obj.shape, 15 jumpiffalse slowpropaccess load obj.props[0] jump continue_1 slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

63 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; shape=15, in position 0 function f(obj) { return obj.b + 1; Generated JIT Code... jump slowpropaccess continue_1:... icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 icstub_2: compare obj.shape, 15 jumpiffalse slowpropaccess load obj.props[0] jump continue_1 slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

64 ICs: Example Example Code shape=12, in position 1 var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; shape=15, in position 0 function f(obj) { return obj.b + 1; Generated JIT Code... jump slowpropaccess continue_1:... icstub_1: compare obj.shape, 12 jumpiffalse slowpropaccess load obj.props[1] jump continue_1 icstub_2: compare obj.shape, 15 jumpiffalse slowpropaccess load obj.props[0] jump continue_1 slowpropaccess:... set up call call ICGetProp ; C++ Slow Zone jump continue_1

65 var q = 4; var r; function f(obj) { r = q; These are fast because of ICs Global Variable Access

66 These are fast because of ICs Global Variable Access var q = 4; var r; function f(obj) { r = q; Direct Property Access var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; function f(obj) { obj2.b = obj1.c;

67 These are fast because of ICs Global Variable Access var q = 4; var r; function f(obj) { r = q; Direct Property Access var obj1 = { a: 1, b: 2, c: 3 ; var obj2 = { b: 2 ; Closure Variable Access var f = function() { var x = 1; var g = function() { var sum = 0; for (var i = 0; i < N; ++i) { sum += x; return sum; return g(); function f(obj) { obj2.b = obj1.c;

68 Prototype chains don t hurt function A(x) { this.x = x; function B(y) { this.y = y; B.prototype = new A; function C(z) { this.z = z; C.prototype = new B;

69 Prototype chains don t hurt function A(x) { this.x = x; function B(y) { this.y = y; B.prototype = new A; function C(z) { this.z = z; C.prototype = new B; proto new C(1) new A new B

70 Prototype chains don t hurt function A(x) { this.x = x; function B(y) { this.y = y; B.prototype = new A; function C(z) { this.z = z; C.prototype = new B; proto new C(1) new A new B new C(2)

71 Prototype chains don t hurt function A(x) { this.x = x; function B(y) { this.y = y; B.prototype = new A; proto new C(1) new A new B new C(2) new C(3) function C(z) { this.z = z; C.prototype = new B;

72 Prototype chains don t hurt function A(x) { this.x = x; function B(y) { this.y = y; B.prototype = new A; proto new C(1) new A new B new C(2) new C(3) function C(z) { this.z = z; C.prototype = new B; Shape of new C objects determines prototype

73 Prototype chains don t hurt function A(x) { this.x = x; function B(y) { this.y = y; B.prototype = new A; proto new C(1) new A new B new C(2) new C(3) function C(z) { this.z = z; C.prototype = new B; Shape of new C objects determines prototype -> IC can generate code that checks shape, then reads directly from prototype without walking

74 Many Shapes Slow Down ICs What happens if many shapes of obj are passed to f? function f(obj) { return obj.p; ICs end up looking like this:

75 Many Shapes Slow Down ICs What happens if many shapes of obj are passed to f? function f(obj) { return obj.p; jumpif shape!= 12 read for shape 12 ICs end up looking like this:

76 Many Shapes Slow Down ICs What happens if many shapes of obj are passed to f? function f(obj) { return obj.p; jumpif shape!= 12 read for shape 12 ICs end up looking like this: jumpif shape!= 15 read for shape 15

77 Many Shapes Slow Down ICs What happens if many shapes of obj are passed to f? function f(obj) { return obj.p; jumpif shape!= 12 read for shape 12 ICs end up looking like this: jumpif shape!= 15 read for shape 15 jumpif shape!= 6 read for shape 6

78 Many Shapes Slow Down ICs What happens if many shapes of obj are passed to f? function f(obj) { return obj.p; jumpif shape!= 12 read for shape 12 ICs end up looking like this: jumpif shape!= 16 read for shape jumpif shape!= 15 read for shape 15 jumpif shape!= 6 read for shape 6 jumpif shape!= 22 read for shape 22 jumpif shape!= 3 read for shape 3

79 Many shapes in practice nanoseconds/iteration IE Opera Chrome Firefox Safari IE Opera Chrome Firefox Safari Slow Zone for 2+ shapes # of shapes doesn t matter! more shapes -> slower slower with more shapes, but levels off in Slow Zone # of shapes at property read site

80 Deeply Nested Closures are Slower var f = function() { var x; var g = function() { var h = function() { var y; var i = function () { var j = function() { z = x + y;

81 Deeply Nested Closures are Slower var f = function() { var x; var g = function() { var h = function() { var y; var i = function () { var j = function() { z = x + y; f call object h call object j call object First call to f

82 Deeply Nested Closures are Slower var f = function() { var x; var g = function() { var h = function() { var y; var i = function () { var j = function() { z = x + y; f call object h call object j call object First call to f f call object h call object j call object Second call to f

83 Deeply Nested Closures are Slower var f = function() { var x; var g = function() { var h = function() { var y; var i = function () { var j = function() { z = x + y; f call object h call object j call object First call to f f call object h call object j call object Second call to f Prototype chains don t slow us down, but deep closure nesting does. Why?

84 Deeply Nested Closures are Slower var f = function() { var x; var g = function() { var h = function() { var y; var i = function () { var j = function() { z = x + y; f call object h call object j call object First call to f f call object h call object j call object Second call to f Prototype chains don t slow us down, but deep closure nesting does. Why? Every call to f generates a unique closure object to hold x.

85 Deeply Nested Closures are Slower var f = function() { var x; var g = function() { var h = function() { var y; var i = function () { var j = function() { z = x + y; f call object h call object j call object First call to f f call object h call object j call object Second call to f Prototype chains don t slow us down, but deep closure nesting does. Why? Every call to f generates a unique closure object to hold x. The engine must walk to find the right x each time

86 Properties in the Slow Zone

87 var a = {; a.x; Properties in the Slow Zone Undefined Property (Fast on Firefox, Chrome)

88 var a = {; a.x; Properties in the Slow Zone Undefined Property (Fast on Firefox, Chrome) DOM Access (I only tested.id, so take with a grain of salt-- other properties may differ) var a = document.getbyelementid( foo ); a.id;

89 Properties in the Slow Zone Undefined Property (Fast on Firefox, Chrome) var a = {; a.x; Scripted Getter (Fast on IE) var a = { x: get() { return 1; ; a.x; DOM Access (I only tested.id, so take with a grain of salt-- other properties may differ) var a = document.getbyelementid( foo ); a.id;

90 Properties in the Slow Zone Undefined Property (Fast on Firefox, Chrome) var a = {; a.x; Scripted Getter (Fast on IE) var a = { x: get() { return 1; ; a.x; DOM Access (I only tested.id, so take with a grain of salt-- other properties may differ) var a = document.getbyelementid( foo ); a.id; Scripted Setter var a = { x: set(y) { this.x_ = y; ; a.x = 1;

91 The Typed JIT Compiler Firefox 3.5+ (Tracemonkey, JM+TI) Chrome 11+ (Crankshaft)

92 Types FTW! If only JavaScript had type declarations...

93 Types FTW! If only JavaScript had type declarations... The JIT would know the type of every local variable

94 Types FTW! If only JavaScript had type declarations... The JIT would know the type of every local variable Know exactly what action to use (no type checks)

95 Types FTW! If only JavaScript had type declarations... The JIT would know the type of every local variable Know exactly what action to use (no type checks) Local variables don t need to be boxed (or unboxed)

96 Types FTW! If only JavaScript had type declarations... The JIT would know the type of every local variable Know exactly what action to use (no type checks) Local variables don t need to be boxed (or unboxed) I call this kind of JIT a typed compiler

97 But JS doesn t have types

98 But JS doesn t have types Problem: JS doesn t have type declarations

99 But JS doesn t have types Problem: JS doesn t have type declarations Solution: run the program for a bit, monitor types Bonus points: use static analysis to infer more types

100 But JS doesn t have types Problem: JS doesn t have type declarations Solution: run the program for a bit, monitor types Bonus points: use static analysis to infer more types Then recompile optimized for those types

101 Running with the Typed JIT On x = y + z: Firefox 3.5+ Chrome 11+ read the operation x = y + z from memory read the inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the output x to memory

102 Running with the Typed JIT On x = y + z: Firefox 3.5+ Chrome 11+ read the operation x = y + z from memory read the inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the output x to memory

103 Running with the Typed JIT On x = y + z: Firefox 3.5+ Chrome 11+ read the operation x = y + z from memory read the inputs y and z from memory check the types of y and z and choose the action unbox y and z execute the action box the output x write the output x to memory

104 function getpop(city) { return popdata[city.id]; for (var i = 0; i < N; ++i) { total += getpop(city); Further Optimization 1 original code Automatic Inlining

105 Further Optimization 1 Automatic Inlining original code function getpop(city) { return popdata[city.id]; for (var i = 0; i < N; ++i) { total += getpop(city); JIT compiles as if you wrote this for (var i = 0; i < N; ++i) { total += popdata[city.id];

106 Further Optimization 2 Loop Invariant Code Motion (LICM, hoisting ) original code for (var i = 0; i < N; ++i) { total += a[i] * (1 + options.tax);

107 Further Optimization 2 Loop Invariant Code Motion (LICM, hoisting ) original code for (var i = 0; i < N; ++i) { total += a[i] * (1 + options.tax); JIT compiles as if you wrote this var f = 1 + options.tax; for (var i = 0; i < N; ++i) { total += a[i] * f;

108 Optimize Only Hot Code

109 Optimize Only Hot Code Typed JITs have longer compile time which would imply longer startup before JS runs

110 Optimize Only Hot Code Typed JITs have longer compile time which would imply longer startup before JS runs To avoid this, engines apply typed JITs selectively Only on hot code (~ runs long enough to be measurable) Only if judged to be worthwhile (incomprehensible heuristics)

111 Current Limitations

112 Current Limitations What happens if the types change after compiling? Just a few changes -> recompile, slight slowdown Many changes -> give up and deoptimize to untyped JIT

113 Current Limitations What happens if the types change after compiling? Just a few changes -> recompile, slight slowdown Many changes -> give up and deoptimize to untyped JIT Array elements, object properties, and closed-over variables Usually still boxed, so fast, but not as fast as C! Typed arrays help in theory, but not always optimized in practice yet

114 Current Limitations What happens if the types change after compiling? Just a few changes -> recompile, slight slowdown Many changes -> give up and deoptimize to untyped JIT Array elements, object properties, and closed-over variables Usually still boxed, so fast, but not as fast as C! Typed arrays help in theory, but not always optimized in practice yet JS semantics require overflow checks for integer math

115 Type-stable JavaScript The key to running faster in future JITs is type-stable JavaScript. This means JavaScript where you could declare a single engine-internal type for each variable.

116 Type-stable JS: examples var g = 34; var o1 = { a: 56 ; var o2 = { a: 99 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; g = 0; Type-stable

117 Type-stable JS: examples var g = 34; var o1 = { a: 56 ; var o2 = { a: 99 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; g = 0; Type-stable NOT type-stable var g = 34; var o1 = { a: 56 ; var o2 = { z: 22, a: 56 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; g = hello ;

118 Type-stable JS: examples var g = 34; var o1 = { a: 56 ; var o2 = { a: 99 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; g = 0; Type-stable NOT type-stable var g = 34; var o1 = { a: 56 ; var o2 = { z: 22, a: 56 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; Different shapes g = hello ;

119 Type-stable JS: examples var g = 34; Type-stable var o1 = { a: 56 ; var o2 = { a: 99 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; NOT type-stable var g = 34; var o1 = { a: 56 ; var o2 = { z: 22, a: 56 ; for (var i = 0; i < 10; ++i) { var o = i % 2? o1 : o2; g += o.a; Different shapes g = 0; g = hello ; Type change

120 Garbage Collection

121 new Object(); new MyConstructor(); { a: 4, b: 5 Object.create(); What Allocates Memory? Objects new Array(); [ 1, 2, 3, 4 ]; Arrays Strings new String( hello ); <p> + e.innerhtml + </p>

122 What Allocates Memory? Objects new Object(); new MyConstructor(); { a: 4, b: 5 Object.create(); Function Objects var x = function () {... new Function(code); Arrays new Array(); [ 1, 2, 3, 4 ]; Strings new String( hello ); <p> + e.innerhtml + </p>

123 What Allocates Memory? Objects new Object(); new MyConstructor(); { a: 4, b: 5 Object.create(); Function Objects var x = function () {... new Function(code); new Array(); [ 1, 2, 3, 4 ]; Arrays Strings new String( hello ); <p> + e.innerhtml + </p> Closure Environments function outer(str) { var name = str; return function inner() { return Hi, + name;

124 What Allocates Memory? Objects new Object(); new MyConstructor(); { a: 4, b: 5 Object.create(); Function Objects var x = function () {... new Function(code); new Array(); [ 1, 2, 3, 4 ]; Arrays Strings new String( hello ); <p> + e.innerhtml + </p> Closure Environments function outer(str) { var name = str; return function inner() { return Hi, + name; name is stored in an implicitly created object!

125 GC Pauses Your Program! Time JavaScript Running GC Running JS Paused

126 GC Pauses Your Program! Time JavaScript Running GC Running JS Paused Basic GC algorithm (mark and sweep) Traverse all reachable objects (from locals, window, DOM) Recycle objects that are not reachable

127 GC Pauses Your Program! Time JavaScript Running GC Running JS Paused Basic GC algorithm (mark and sweep) Traverse all reachable objects (from locals, window, DOM) Recycle objects that are not reachable The JS program is paused during GC for safe traversal

128 GC Pauses Your Program! Time JavaScript Running GC Running JS Paused Basic GC algorithm (mark and sweep) Traverse all reachable objects (from locals, window, DOM) Recycle objects that are not reachable The JS program is paused during GC for safe traversal Pauses may be long: 100 ms or more Serious problem for animation Can also be a drag on general performance

129 Reducing Pauses with Science 1 Generational GC Chrome

130 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects

131 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area

132 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area Promote long-lived objects to a rarely collected tenured area

133 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area Promote long-lived objects to a rarely collected tenured area Simple GC JavaScript Running GC Running JS Paused

134 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area Promote long-lived objects to a rarely collected tenured area Simple GC JavaScript Running GC Running JS Paused Generational GC JavaScript Running

135 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area Promote long-lived objects to a rarely collected tenured area Simple GC JavaScript Running GC Running JS Paused Generational GC JavaScript Running nursery collection (<100 us)

136 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area Promote long-lived objects to a rarely collected tenured area Simple GC JavaScript Running GC Running JS Paused Generational GC JavaScript Running nursery collection (<100 us) tenured collection

137 Reducing Pauses with Science 1 Generational GC Chrome Idea: Optimize for creating many short-lived objects Create objects in a frequently collected nursery area Promote long-lived objects to a rarely collected tenured area Simple GC JavaScript Running GC Running JS Paused Generational GC JavaScript Running fewer pauses! nursery collection (<100 us) tenured collection

138 Generational GC by Example scavenging young generation (aka nursery) mark-and-sweep tenured generation Message Array Message

139 Generational GC by Example scavenging young generation (aka nursery) Point mark-and-sweep tenured generation Message Array Message

140 Generational GC by Example scavenging young generation (aka nursery) Point Point mark-and-sweep tenured generation Message Array Message

141 Generational GC by Example scavenging young generation (aka nursery) Point Point Line mark-and-sweep tenured generation Message Array Message

142 Generational GC by Example scavenging young generation (aka nursery) Point Point Line a b mark-and-sweep tenured generation Message Array Message

143 Generational GC by Example scavenging young generation (aka nursery) Point Point Line Point a b mark-and-sweep tenured generation Message Array Message

144 Generational GC by Example scavenging young generation (aka nursery) Point Point Line Point Message a b mark-and-sweep tenured generation Message Array Message

145 Generational GC by Example scavenging young generation (aka nursery) Point Point Line Point Message a b mark-and-sweep tenured generation Message Array Message

146 Generational GC by Example scavenging young generation (aka nursery) Point Point Line Point Message Point a b mark-and-sweep tenured generation Message Array Message

147 Generational GC by Example scavenging young generation (aka nursery) Point Point Line Point Point a b mark-and-sweep tenured generation Message Message Array Message

148 Generational GC by Example scavenging young generation (aka nursery) mark-and-sweep tenured generation Message Message Array Message

149 Reducing Pauses with Science 1I Incremental GC Chrome

150 Reducing Pauses with Science 1I Incremental GC Chrome Idea: Do a little bit of GC traversal at a time

151 Reducing Pauses with Science 1I Incremental GC Chrome Idea: Do a little bit of GC traversal at a time Simple GC JavaScript Running GC Running JS Paused

152 Reducing Pauses with Science 1I Incremental GC Chrome Idea: Do a little bit of GC traversal at a time Simple GC JavaScript Running GC Running JS Paused Incremental GC

153 Reducing Pauses with Science 1I Incremental GC Chrome Idea: Do a little bit of GC traversal at a time Simple GC JavaScript Running GC Running JS Paused Incremental GC shorter pauses!

154 Reducing Pauses in Practice

155 Reducing Pauses in Practice For simple GCs Fewer live objects -> shorter pauses Lower allocation rate (objects/second) -> less frequent pauses Lots of live data + allocate objects in hot loops = very bad

156 Reducing Pauses in Practice For simple GCs Fewer live objects -> shorter pauses Lower allocation rate (objects/second) -> less frequent pauses Lots of live data + allocate objects in hot loops = very bad For incremental GCs Lots of live objects -> slows down program a bit if GC is not generational Mostly, don t worry about pauses!

157 JavaScript Engines in Practice

158 Performance Faults Performance fault: when a tiny change hurts performance Sometimes, just makes one statement slower Other times, deoptimizes the entire function! Reasons we have performance faults bug, tends to get quickly rare case, will get fixed if not rare hard to optimize, RSN...

159 Strings

160 Strings In the Slow Zone, but some things are faster than you might think

161 Strings In the Slow Zone, but some things are faster than you might think.substring() is fast, O(1) Don t need to copy characters, just point within original

162 Strings In the Slow Zone, but some things are faster than you might think.substring() is fast, O(1) Don t need to copy characters, just point within original Concatenation is also optimized Batch up inputs in a rope or concat tree, concat all at once Performance fault: prepending (Opera)

163 Strings In the Slow Zone, but some things are faster than you might think.substring() is fast, O(1) // Prepending example var s = ; Don t need to copy characters, just for point (var i within = 0; i < original 100; ++i) { s = i + s; Concatenation is also optimized Batch up inputs in a rope or concat tree, concat all at once Performance fault: prepending (Opera)

164 Arrays fast: dense array var a = []; for (var i = 0; i < 100; ++i) { a[i] = 0; 3-15x slower: sparse array var a = []; a[10000] = 0; for (var i = 0; i < 100; ++i) { a[i] = 0; a.x = 7; // Fx, IE only Want a fast array? Make sure it s dense 0..N fill or push fill is always dense Huge gaps are always sparse N..0 fill is sparse on Firefox adding a named property is sparse on Firefox, IE

165 Iteration over Arrays fastest: index iteration // This runs in all in JIT code, // so it s really fast. for (var i = 0; i < a.length; ++i) { sum += a[i];

166 Iteration over Arrays 3-15x slower: functional style fastest: index iteration // This runs in all in JIT code, // so it s really fast. for (var i = 0; i < a.length; ++i) { sum += a[i]; // This makes N function calls, // and most JITs don t optimize // through C++ reduce(). sum = a.reduce(function(a, b) { return a + b; ); 20-80x slower: for-in // This calls a C++ function to // navigate the property list. for (var i in a) { sum += a[i];

167 Functions Function calls use ICs, so they are fast Manual inlining can still help sometimes Key performance faults: f.call() x slower than f() f.apply() x slower than f() arguments - often very slow, but varies

168 Creating Objects Creating objects is slow Doesn t matter too much how you create or populate

169 Creating Objects Creating objects is slow Doesn t matter too much how you create or populate Exception: Constructors on Chrome are fast function Cons(x, y, z) { this.x = x; this.y = y; this.z = z; for (var i = 0; i < N; ++i) new Cons(i, i + 1, i * 2);

170 OOP Styling

171 OOP Styling Prototype function Point(x, y) { this.x = x; this.y = y; Point.prototype = { distance: function(pt2)...

172 OOP Styling Prototype function Point(x, y) { this.x = x; this.y = y; Point.prototype = { distance: function(pt2)... Information-Hiding function Point(x, y) { return { distance: function(pt2)...

173 OOP Styling Prototype function Point(x, y) { this.x = x; this.y = y; Point.prototype = { distance: function(pt2)... Information-Hiding function Point(x, y) { return { distance: function(pt2)... Instance Methods function Point(x, y) { this.x = x; this.y = y; this.distance = function(pt2)...

174 OOP Styling Prototype function Point(x, y) { this.x = x; this.y = y; Point.prototype = { distance: function(pt2)... Information-Hiding function Point(x, y) { return { distance: function(pt2)... Prototype style is much faster to create (each closure creates a function object) Instance Methods function Point(x, y) { this.x = x; this.y = y; this.distance = function(pt2)...

175 OOP Styling Prototype function Point(x, y) { this.x = x; this.y = y; Point.prototype = { distance: function(pt2)... Information-Hiding function Point(x, y) { return { distance: function(pt2)... Prototype style is much faster to create (each closure creates a function object) Using the objects is about the same Instance Methods function Point(x, y) { this.x = x; this.y = y; this.distance = function(pt2)...

176 Exceptions Exceptions assumed to be rare in perf-sensitive code running a try statement is free on most browers throw/catch is really slow There are many performance faults around exceptions just having a try statement deoptimizes on some browers try-finally is perf fault on some

177 eval and with Short version: Do not use anywhere near performance sensitive code! Mind-Bogglingly Awful 5-100x slower than using a function call var sum = 0; for (var i = 0; i < N; ++i) { sum = eval( sum + i ); Still Terrible 2-10x slower than without eval var sum = 0; eval( ); for (var i = 0; i < N; ++i) { sum = eval( sum + i );

178 Top 5 Things to Know

179 Top 5 Things to Know 5. Avoid eval, with, exceptions near perf-sensitive code

180 Top 5 Things to Know 5. Avoid eval, with, exceptions near perf-sensitive code 4. Avoid creating objects in hot loops

181 Top 5 Things to Know 5. Avoid eval, with, exceptions near perf-sensitive code 4. Avoid creating objects in hot loops 3. Use dense arrays (know what causes sparseness)

182 Top 5 Things to Know 5. Avoid eval, with, exceptions near perf-sensitive code 4. Avoid creating objects in hot loops 3. Use dense arrays (know what causes sparseness) 2. Write type-stable code

183 Top 5 Things to Know 5. Avoid eval, with, exceptions near perf-sensitive code 4. Avoid creating objects in hot loops 3. Use dense arrays (know what causes sparseness) 2. Write type-stable code 1....

184 Talk To Us JS engine developers want to help you. Tell us about: Performance faults you run into Exciting apps that require fast JS Anything interesting you discover about JS performance

JavaScript: Sort of a Big Deal,

JavaScript: Sort of a Big Deal, : Sort of a Big Deal, But Sort of Quirky... March 20, 2017 Lisp in C s Clothing (Crockford, 2001) Dynamically Typed: no static type annotations or type checks. C-Like Syntax: curly-braces, for, semicolons,

More information

Lecture 13: Garbage Collection

Lecture 13: Garbage Collection Lecture 13: Garbage Collection COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer/Mikkel Kringelbach 1 Garbage Collection Every modern programming language allows programmers

More information

TRANSLATING DART TO EFFICIENT JAVASCRIPT. Kasper Lund Google

TRANSLATING DART TO EFFICIENT JAVASCRIPT. Kasper Lund Google TRANSLATING DART TO EFFICIENT JAVASCRIPT Kasper Lund Google Translating Dart to efficient JavaScript Kasper Lund, Google Who am I? Kasper Lund, software engineer at Google Projects OOVM: Embedded Smalltalk

More information

Structure of Programming Languages Lecture 10

Structure of Programming Languages Lecture 10 Structure of Programming Languages Lecture 10 CS 6636 4536 Spring 2017 CS 6636 4536 Lecture 10: Classes... 1/23 Spring 2017 1 / 23 Outline 1 1. Types Type Coercion and Conversion Type Classes, Generics,

More information

JavaScript and V8 A functional-ish language and implementation in the mainstream

JavaScript and V8 A functional-ish language and implementation in the mainstream JavaScript and V8 A functional-ish language and implementation in the mainstream Andreas Rossberg, Google Munich rossberg@google.com CEFP 2011, Budapest Outline JavaScript Background Functional concepts

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Programming Language Implementation

Programming Language Implementation A Practical Introduction to Programming Language Implementation 2014: Week 10 Garbage Collection College of Information Science and Engineering Ritsumeikan University 1 review of last week s topics dynamic

More information

Kasper Lund, Software engineer at Google. Crankshaft. Turbocharging the next generation of web applications

Kasper Lund, Software engineer at Google. Crankshaft. Turbocharging the next generation of web applications Kasper Lund, Software engineer at Google Crankshaft Turbocharging the next generation of web applications Overview Why did we introduce Crankshaft? Deciding when and what to optimize Type feedback and

More information

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8 Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

More information

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime Runtime The optimized program is ready to run What sorts of facilities are available at runtime Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis token stream Syntactic

More information

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready. CSE 341, Spring 2011, Final Examination 9 June 2011 Please do not turn the page until everyone is ready. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper.

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 14: Memory Management Viktor Leijon Slides largely by Johan Nordlander with material generously provided by Mark P. Jones. 1 First: Run-time Systems 2 The Final Component:

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

Javascript. Many examples from Kyle Simpson: Scope and Closures

Javascript. Many examples from Kyle Simpson: Scope and Closures Javascript Many examples from Kyle Simpson: Scope and Closures What is JavaScript? Not related to Java (except that syntax is C/Java- like) Created by Brendan Eich at Netscape later standardized through

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1 Agenda CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Summer 2004 Java virtual machine architecture.class files Class loading Execution engines Interpreters & JITs various strategies

More information

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8 Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley. CS61C Machine Structures Lecture 3 Introduction to the C Programming Language 1/23/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L03 Introduction to C (1) Administrivia

More information

Managed runtimes & garbage collection

Managed runtimes & garbage collection Managed runtimes Advantages? Managed runtimes & garbage collection CSE 631 Some slides by Kathryn McKinley Disadvantages? 1 2 Managed runtimes Portability (& performance) Advantages? Reliability Security

More information

The Generational Hypothesis

The Generational Hypothesis Generational GC 1 The Generational Hypothesis Hypothesis: most objects "die young" i.e., they are only needed for a short time, and can be collected soon A great example of empirical systems work Found

More information

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley Managed runtimes & garbage collection CSE 6341 Some slides by Kathryn McKinley 1 Managed runtimes Advantages? Disadvantages? 2 Managed runtimes Advantages? Reliability Security Portability Performance?

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

Static Analysis of Dynamically Typed Languages made Easy

Static Analysis of Dynamically Typed Languages made Easy Static Analysis of Dynamically Typed Languages made Easy Yin Wang School of Informatics and Computing Indiana University Overview Work done as two internships at Google (2009 summer and 2010 summer) Motivation:

More information

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc. Chapter 1 GETTING STARTED SYS-ED/ Computer Education Techniques, Inc. Objectives You will learn: Java platform. Applets and applications. Java programming language: facilities and foundation. Memory management

More information

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications JavaScript Started as a simple script in a Web page that is interpreted and run by the browser Supported by most modern browsers Allows dynamic update of a web page More generally, allows running an arbitrary

More information

The JavaScriptCore Virtual Machine. Filip Pizlo Apple Inc.

The JavaScriptCore Virtual Machine. Filip Pizlo Apple Inc. The JavaScriptCore Virtual Machine Filip Pizlo Apple Inc. 3 Pizlo Keynotes / Week ICCV 17 Symmetry as the fundamental prior in human 3D vision Zygmunt Pizlo webkit.org https://svn.webkit.org/repository/webkit/trunk

More information

Identifying the Sources of Cache Misses in Java Programs Without Relying on Hardware Counters. Hiroshi Inoue and Toshio Nakatani IBM Research - Tokyo

Identifying the Sources of Cache Misses in Java Programs Without Relying on Hardware Counters. Hiroshi Inoue and Toshio Nakatani IBM Research - Tokyo Identifying the Sources of Cache Misses in Java Programs Without Relying on Hardware Counters Hiroshi Inoue and Toshio Nakatani IBM Research - Tokyo June 15, 2012 ISMM 2012 at Beijing, China Motivation

More information

Sista: Improving Cog s JIT performance. Clément Béra

Sista: Improving Cog s JIT performance. Clément Béra Sista: Improving Cog s JIT performance Clément Béra Main people involved in Sista Eliot Miranda Over 30 years experience in Smalltalk VM Clément Béra 2 years engineer in the Pharo team Phd student starting

More information

Running class Timing on Java HotSpot VM, 1

Running class Timing on Java HotSpot VM, 1 Compiler construction 2009 Lecture 3. A first look at optimization: Peephole optimization. A simple example A Java class public class A { public static int f (int x) { int r = 3; int s = r + 5; return

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 - Spring 2013 1 Memory Attributes! Memory to store data in programming languages has the following lifecycle

More information

CS61C : Machine Structures

CS61C : Machine Structures Get your clickers ready...! inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 3 Introduction to the C Programming Language (pt 1) 2013-01-28! Hello to Nishant Varma watching from India!!!Senior

More information

Client-Side Web Technologies. JavaScript Part I

Client-Side Web Technologies. JavaScript Part I Client-Side Web Technologies JavaScript Part I JavaScript First appeared in 1996 in Netscape Navigator Main purpose was to handle input validation that was currently being done server-side Now a powerful

More information

JavaScript. What s wrong with JavaScript?

JavaScript. What s wrong with JavaScript? JavaScript 1 What s wrong with JavaScript? A very powerful language, yet Often hated Browser inconsistencies Misunderstood Developers find it painful Lagging tool support Bad name for a language! Java

More information

Interprocedural Type Specialization of JavaScript Programs Without Type Analysis

Interprocedural Type Specialization of JavaScript Programs Without Type Analysis Interprocedural Type Specialization of JavaScript Programs Without Type Analysis Maxime Chevalier-Boisvert joint work with Marc Feeley ECOOP - July 20th, 2016 Overview Previous work: Lazy Basic Block Versioning

More information

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications JavaScript Started as a simple script in a Web page that is interpreted and run by the browser Supported by most modern browsers Allows dynamic update of a web page More generally, allows running an arbitrary

More information

MEAP Edition Manning Early Access Program WebAssembly in Action Version 1

MEAP Edition Manning Early Access Program WebAssembly in Action Version 1 MEAP Edition Manning Early Access Program WebAssembly in Action Version 1 Copyright 2018 Manning Publications For more information on this and other Manning titles go to www.manning.com welcome Thank you

More information

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material. Lincoln Land Community College Capital City Training Center 130 West Mason Springfield, IL 62702 217-782-7436 www.llcc.edu/cctc WA2442 Introduction to JavaScript Objectives This intensive training course

More information

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java) COMP 412 FALL 2017 Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java) Copyright 2017, Keith D. Cooper & Zoran Budimlić, all rights reserved. Students enrolled in Comp 412 at Rice

More information

A new Mono GC. Paolo Molaro October 25, 2006

A new Mono GC. Paolo Molaro October 25, 2006 A new Mono GC Paolo Molaro lupus@novell.com October 25, 2006 Current GC: why Boehm Ported to the major architectures and systems Featurefull Very easy to integrate Handles managed pointers in unmanaged

More information

Dynamic Languages Strike Back. Steve Yegge Stanford EE Dept Computer Systems Colloquium May 7, 2008

Dynamic Languages Strike Back. Steve Yegge Stanford EE Dept Computer Systems Colloquium May 7, 2008 Dynamic Languages Strike Back Steve Yegge Stanford EE Dept Computer Systems Colloquium May 7, 2008 What is this talk about? Popular opinion of dynamic languages: Unfixably slow Not possible to create IDE-quality

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Pointers and Memory 1

Pointers and Memory 1 Pointers and Memory 1 Pointer values Pointer values are memory addresses Think of them as a kind of integer values The first byte of memory is 0, the next 1, and so on A pointer p can hold the address

More information

Introduction. CS 2210 Compiler Design Wonsun Ahn

Introduction. CS 2210 Compiler Design Wonsun Ahn Introduction CS 2210 Compiler Design Wonsun Ahn What is a Compiler? Compiler: A program that translates source code written in one language to a target code written in another language Source code: Input

More information

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC330 Fall 2018 1 Memory Attributes Memory to store data in programming languages has the following lifecycle

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 Spring 2017 1 Memory Attributes Memory to store data in programming languages has the following lifecycle

More information

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank Algorithms for Dynamic Memory Management (236780) Lecture 4 Lecturer: Erez Petrank!1 March 24, 2014 Topics last week The Copying Garbage Collector algorithm: Basics Cheney s collector Additional issues:

More information

Node.js Training JavaScript. Richard richardrodger.com

Node.js Training JavaScript. Richard richardrodger.com Node.js Training JavaScript Richard Rodger @rjrodger richardrodger.com richard.rodger@nearform.com A New Look at JavaScript Embracing JavaScript JavaScript Data Structures JavaScript Functions Functional

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 3 JVM and optimization. A first look at optimization: Peephole optimization. A simple example A Java class public class A { public static int f (int x) { int r = 3; int

More information

Java Without the Jitter

Java Without the Jitter TECHNOLOGY WHITE PAPER Achieving Ultra-Low Latency Table of Contents Executive Summary... 3 Introduction... 4 Why Java Pauses Can t Be Tuned Away.... 5 Modern Servers Have Huge Capacities Why Hasn t Latency

More information

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps Garbage Collection Garbage collection makes memory management easier for programmers by automatically reclaiming unused memory. The garbage collector in the CLR makes tradeoffs to assure reasonable performance

More information

INF5750. Introduction to JavaScript and Node.js

INF5750. Introduction to JavaScript and Node.js INF5750 Introduction to JavaScript and Node.js Outline Introduction to JavaScript Language basics Introduction to Node.js Tips and tools for working with JS and Node.js What is JavaScript? Built as scripting

More information

Understanding Browsers

Understanding Browsers Understanding Browsers What Causes Browser Display Differences? Different Browsers Different Browser Versions Different Computer Types Different Screen Sizes Different Font Sizes HTML Errors Browser Bugs

More information

Robust Memory Management Schemes

Robust Memory Management Schemes Robust Memory Management Schemes Prepared by : Fadi Sbahi & Ali Bsoul Supervised By: Dr. Lo ai Tawalbeh Jordan University of Science and Technology Robust Memory Management Schemes Introduction. Memory

More information

A JVM Does What? Eva Andreasson Product Manager, Azul Systems

A JVM Does What? Eva Andreasson Product Manager, Azul Systems A JVM Does What? Eva Andreasson Product Manager, Azul Systems Presenter Eva Andreasson Innovator & Problem solver Implemented the Deterministic GC of JRockit Real Time Awarded patents on GC heuristics

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 C Strings, Arrays, & Malloc 2007-06-27 Scott Beamer, Instructor Sun announces new supercomputer: Sun Constellation CS61C L3 C Pointers

More information

2 rd class Department of Programming. OOP with Java Programming

2 rd class Department of Programming. OOP with Java Programming 1. Structured Programming and Object-Oriented Programming During the 1970s and into the 80s, the primary software engineering methodology was structured programming. The structured programming approach

More information

Vector and Free Store (Pointers and Memory Allocation)

Vector and Free Store (Pointers and Memory Allocation) DM560 Introduction to Programming in C++ Vector and Free Store (Pointers and Memory Allocation) Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides

More information

Habanero Extreme Scale Software Research Project

Habanero Extreme Scale Software Research Project Habanero Extreme Scale Software Research Project Comp215: Garbage Collection Zoran Budimlić (Rice University) Adapted from Keith Cooper s 2014 lecture in COMP 215. Garbage Collection In Beverly Hills...

More information

CGS 3066: Spring 2015 JavaScript Reference

CGS 3066: Spring 2015 JavaScript Reference CGS 3066: Spring 2015 JavaScript Reference Can also be used as a study guide. Only covers topics discussed in class. 1 Introduction JavaScript is a scripting language produced by Netscape for use within

More information

HW1 due Monday by 9:30am Assignment online, submission details to come

HW1 due Monday by 9:30am Assignment online, submission details to come inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

More information

Implementation Garbage Collection

Implementation Garbage Collection CITS 3242 Programming Paradigms Part IV: Advanced Topics Topic 19: Implementation Garbage Collection Most languages in the functional, logic, and object-oriented paradigms include some form of automatic

More information

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Objects Outline Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based languages (Smalltalk) Prototype-based languages (JavaScript)

More information

BEAMJIT, a Maze of Twisty Little Traces

BEAMJIT, a Maze of Twisty Little Traces BEAMJIT, a Maze of Twisty Little Traces A walk-through of the prototype just-in-time (JIT) compiler for Erlang. Frej Drejhammar 130613 Who am I? Senior researcher at the Swedish Institute

More information

Trace-based JIT Compilation

Trace-based JIT Compilation Trace-based JIT Compilation Hiroshi Inoue, IBM Research - Tokyo 1 Trace JIT vs. Method JIT https://twitter.com/yukihiro_matz/status/533775624486133762 2 Background: Trace-based Compilation Using a Trace,

More information

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types Announcements Types Working on requirements this week Work on design, implementation Lecture 17 CS 169 Prof. Brewer CS 169 Lecture 16 1 Prof. Brewer CS 169 Lecture 16 2 Outline Type concepts Where do types

More information

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides Garbage Collection Last time Compiling Object-Oriented Languages Today Motivation behind garbage collection Garbage collection basics Garbage collection performance Specific example of using GC in C++

More information

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications JavaScript CS 4640 Programming Languages for Web Applications 1 How HTML, CSS, and JS Fit Together {css} javascript() Content layer The HTML gives the page structure and adds semantics Presentation

More information

Get the (Spider)monkey off your back

Get the (Spider)monkey off your back Get the (Spider)monkey off your back Exploiting Firefox through the Javascript engine by eboda and bkth from phoenhex Who are we? Security enthusiasts who dabble in vulnerability research on their free

More information

Language Based isolation of Untrusted JavaScript

Language Based isolation of Untrusted JavaScript Dept. of Computer Science, Stanford University Joint work with Sergio Maffeis (Imperial College London) and John C. Mitchell (Stanford University) Outline 1 Motivation 2 Case Study : FBJS Design Attacks

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 C Pointers 2004-09-08 Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia Cal flies over Air Force We re ranked 13 th in the US and

More information

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications JavaScript CS 4640 Programming Languages for Web Applications 1 How HTML, CSS, and JS Fit Together {css} javascript() Content layer The HTML gives the page structure and adds semantics Presentation

More information

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace Project there are a couple of 3 person teams regroup or see me or forever hold your peace a new drop with new type checking is coming using it is optional 1 Compiler Architecture source code Now we jump

More information

Advanced Programming & C++ Language

Advanced Programming & C++ Language Advanced Programming & C++ Language ~6~ Introduction to Memory Management Ariel University 2018 Dr. Miri (Kopel) Ben-Nissan Stack & Heap 2 The memory a program uses is typically divided into four different

More information

Recency Types for Dynamically-Typed, Object-Based Languages

Recency Types for Dynamically-Typed, Object-Based Languages Recency Types for Dynamically-Typed, Object-Based Languages Phillip Heidegger, Peter Thiemann Albert-Ludwigs-Universität Freiburg 15.10.2008 Task: Maintenance Finding bugs in JavaScript programs Understanding

More information

Compiler Construction Lent Term 2013 Lectures 9 & 10 (of 16)

Compiler Construction Lent Term 2013 Lectures 9 & 10 (of 16) Compiler Construction ent Term 2013 ectures 9 & 10 (of 16) Assorted topics Tuples/records A peek at universal polymorphism exceptions linking and loading bootstrapping Timothy G. Griffin tgg22@cam.ac.uk

More information

High-Level Language VMs

High-Level Language VMs High-Level Language VMs Outline Motivation What is the need for HLL VMs? How are these different from System or Process VMs? Approach to HLL VMs Evolutionary history Pascal P-code Object oriented HLL VMs

More information

Garbage Collection. Vyacheslav Egorov

Garbage Collection. Vyacheslav Egorov Garbage Collection Vyacheslav Egorov 28.02.2012 class Heap { public: void* Allocate(size_t sz); }; class Heap { public: void* Allocate(size_t sz); void Deallocate(void* ptr); }; class Heap { public: void*

More information

Self-Optimizing AST Interpreters

Self-Optimizing AST Interpreters Self-Optimizing AST Interpreters Thomas Würthinger Andreas Wöß Lukas Stadler Gilles Duboscq Doug Simon Christian Wimmer Oracle Labs Institute for System Software, Johannes Kepler University Linz, Austria

More information

Lecture 13: Complex Types and Garbage Collection

Lecture 13: Complex Types and Garbage Collection Lecture 13: Complex Types and Garbage Collection COMP 524 Programming Language Concepts Stephen Olivier March 17, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

More information

JavaScript: The Good Parts

JavaScript: The Good Parts JavaScript: The Good Parts The World's Most Misunderstood Programming Language Douglas Crockford Yahoo! Inc. A language of many contrasts. The broadest range of programmer skills of any programming language.

More information

6.001 Recitation 23: Register Machines and Stack Frames

6.001 Recitation 23: Register Machines and Stack Frames 6.001 Recitation 23: Register achines and Stack Frames RI: Gerald Dalley, dalleyg@mit.edu, 8 ay 2007 http://people.csail.mit.edu/dalleyg/6.001/sp2007/ iscellany apply: See the IT Scheme documentation for

More information

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction Lecture content TDDA69 Data and Program Structure Introduction Cyrille Berger Course Introduction to the different Programming Paradigm The different programming paradigm Why different paradigms? Introduction

More information

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides) Objects Deian Stefan (Adopted from my & Edward Yang s CS242 slides) Outline Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages Class-based

More information

Truffle A language implementation framework

Truffle A language implementation framework Truffle A language implementation framework Boris Spasojević Senior Researcher VM Research Group, Oracle Labs Slides based on previous talks given by Christian Wimmer, Christian Humer and Matthias Grimmer.

More information

JAVA PERFORMANCE. PR SW2 S18 Dr. Prähofer DI Leopoldseder

JAVA PERFORMANCE. PR SW2 S18 Dr. Prähofer DI Leopoldseder JAVA PERFORMANCE PR SW2 S18 Dr. Prähofer DI Leopoldseder OUTLINE 1. What is performance? 1. Benchmarking 2. What is Java performance? 1. Interpreter vs JIT 3. Tools to measure performance 4. Memory Performance

More information

Today. Continue our very basic intro to JavaScript. Lambda calculus

Today. Continue our very basic intro to JavaScript. Lambda calculus JavaScript (cont) Today Continue our very basic intro to JavaScript Lambda calculus Last lecture recap JavaScript was designed in 10 days Lots of unsatisfactory parts (in retrospect); many due due to the

More information

Identifying and Fixing Parameter Sniffing

Identifying and Fixing Parameter Sniffing Identifying and Fixing Parameter Sniffing Brent Ozar www.brentozar.com sp_blitz sp_blitzfirst email newsletter videos SQL Critical Care 2017 Brent Ozar Unlimited. All rights reserved. 1 This is genuinely

More information

CS 241 Honors Memory

CS 241 Honors Memory CS 241 Honors Memory Ben Kurtovic Atul Sandur Bhuvan Venkatesh Brian Zhou Kevin Hong University of Illinois Urbana Champaign February 20, 2018 CS 241 Course Staff (UIUC) Memory February 20, 2018 1 / 35

More information

Plot SIZE. How will execution time grow with SIZE? Actual Data. int array[size]; int A = 0;

Plot SIZE. How will execution time grow with SIZE? Actual Data. int array[size]; int A = 0; How will execution time grow with SIZE? int array[size]; int A = ; for (int i = ; i < ; i++) { for (int j = ; j < SIZE ; j++) { A += array[j]; } TIME } Plot SIZE Actual Data 45 4 5 5 Series 5 5 4 6 8 Memory

More information

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM. IR Generation Announcements My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM. This is a hard deadline and no late submissions will be

More information

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery. HTML5/CSS3/JavaScript Programming Course Summary Description This class is designed for students that have experience with basic HTML concepts that wish to learn about HTML Version 5, Cascading Style Sheets

More information

Ruby: Introduction, Basics

Ruby: Introduction, Basics Ruby: Introduction, Basics Computer Science and Engineering College of Engineering The Ohio State University Lecture 4 Ruby vs Java: Similarities Imperative and object-oriented Classes and instances (ie

More information

Deconstructing Java TM

Deconstructing Java TM Deconstructing Java TM Gilad Bracha 1 Original Sin: Primitive Types Eight types: bool, byte, char, short, int, long, float, double... and void too! Eight special cases in many APIs Cannot store in collections

More information

Java Performance Tuning

Java Performance Tuning 443 North Clark St, Suite 350 Chicago, IL 60654 Phone: (312) 229-1727 Java Performance Tuning This white paper presents the basics of Java Performance Tuning and its preferred values for large deployments

More information

Programming language components

Programming language components Programming language components syntax: grammar rules for defining legal statements what's grammatically legal? how are things built up from smaller things? semantics: what things mean what do they compute?

More information

High Performance Managed Languages. Martin Thompson

High Performance Managed Languages. Martin Thompson High Performance Managed Languages Martin Thompson - @mjpt777 Really, what is your preferred platform for building HFT applications? Why do you build low-latency applications on a GC ed platform? Agenda

More information

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16 1 Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16 Towards JVM Dynamic Languages Toolchain Insert Picture Here Attila

More information