Performance from Aligning Smalltalk & Javascript Classes Dr. - - PowerPoint PPT Presentation

performance from aligning smalltalk javascript classes
SMART_READER_LITE
LIVE PREVIEW

Performance from Aligning Smalltalk & Javascript Classes Dr. - - PowerPoint PPT Presentation

Performance from Aligning Smalltalk & Javascript Classes Dr. Dave Mason Department of Computer Science Ryerson University 1 / 20 Motivation building a dataflow environment (similar to this mornings talk) for the web Amber is very


slide-1
SLIDE 1

Performance from Aligning Smalltalk & Javascript Classes

  • Dr. Dave Mason

Department of Computer Science Ryerson University

1 / 20

slide-2
SLIDE 2

Motivation

building a dataflow environment (similar to this morning’s talk) for the web Amber is very cool, but too slow and complex to interface with native objects curious how close one can make Javascript to Smalltalk

Introduction 2 / 20

slide-3
SLIDE 3

Motivation

building a dataflow environment (similar to this morning’s talk) for the web Amber is very cool, but too slow and complex to interface with native objects curious how close one can make Javascript to Smalltalk

Introduction 2 / 20

slide-4
SLIDE 4

Motivation

building a dataflow environment (similar to this morning’s talk) for the web Amber is very cool, but too slow and complex to interface with native objects curious how close one can make Javascript to Smalltalk

Introduction 2 / 20

slide-5
SLIDE 5

1 // Animal 2 function Animal(name) { 3 this.name = name 4 } 5 Animal.prototype = { // methods 6 canWalk: true, 7 sit: function() { 8 this.canWalk = false 9 alert(this.name + ’ sits down.’) 10 } 11 } 12 // Rabbit 13 function Rabbit(name) { 14 this.name = name 15 } 16 Rabbit.prototype = inherit(Animal.prototype) 17 Rabbit.prototype.jump = function() { // methods 18 this.canWalk = true 19 alert(this.name + ’ jumps!’) 20 } 21 // Usage 22 var rabbit = new Rabbit(’Sniffer’) 23 rabbit.sit() // Sniffer sits. 24 rabbit.jump() // Sniffer jumps!

Introduction 3 / 20

slide-6
SLIDE 6

null undefined Object pro- totype __proto__ constructor fields Object __proto__ prototype fields Function prototype __proto__ constructor fields Function __proto__ prototype fields UserCls prototype __proto__ constructor fields UserCls __proto__ prototype fields a UserCls __proto__ fields another UserCls __proto__ fields an Object __proto__ fields __proto__ constructor prototype

Introduction JS Inheritance 4 / 20

slide-7
SLIDE 7

ProtoObject methods fields ProtoObject class methods Object methods fields Object class methods an Object Behavior methods fields Behavior class methods Class methods fields Class class methods Metaclass methods fields Metaclass class methods UndefinedObject methods fields UndefinedObject class methods nil UserCls methods fields UserCls class methods a UserCls fields

superclass class

Introduction Smalltalk Classes 5 / 20

slide-8
SLIDE 8

Amber

instances of Number are represented by the Javascript number

  • bjects

Boolean, and Date are similarly directly mapped String, Symbol and Character both use Javascript strings OrderedCollection is mapped to Javascript arrays to map to valid Javascript identifiers, message-names are prepended with _ and have every colon (:) replaced by _ because Javascript identifiers have only a single look-up mechanism, instance variables are prepended with @, which means they need to be looked up via the indexing method (obj[’@foo’]) because obj.@foo is invalid syntax.

Introduction Smalltalk Classes 6 / 20

slide-9
SLIDE 9

Amber

instances of Number are represented by the Javascript number

  • bjects

Boolean, and Date are similarly directly mapped String, Symbol and Character both use Javascript strings OrderedCollection is mapped to Javascript arrays to map to valid Javascript identifiers, message-names are prepended with _ and have every colon (:) replaced by _ because Javascript identifiers have only a single look-up mechanism, instance variables are prepended with @, which means they need to be looked up via the indexing method (obj[’@foo’]) because obj.@foo is invalid syntax.

Introduction Smalltalk Classes 6 / 20

slide-10
SLIDE 10

Amber

instances of Number are represented by the Javascript number

  • bjects

Boolean, and Date are similarly directly mapped String, Symbol and Character both use Javascript strings OrderedCollection is mapped to Javascript arrays to map to valid Javascript identifiers, message-names are prepended with _ and have every colon (:) replaced by _ because Javascript identifiers have only a single look-up mechanism, instance variables are prepended with @, which means they need to be looked up via the indexing method (obj[’@foo’]) because obj.@foo is invalid syntax.

Introduction Smalltalk Classes 6 / 20

slide-11
SLIDE 11

Amber

instances of Number are represented by the Javascript number

  • bjects

Boolean, and Date are similarly directly mapped String, Symbol and Character both use Javascript strings OrderedCollection is mapped to Javascript arrays to map to valid Javascript identifiers, message-names are prepended with _ and have every colon (:) replaced by _ because Javascript identifiers have only a single look-up mechanism, instance variables are prepended with @, which means they need to be looked up via the indexing method (obj[’@foo’]) because obj.@foo is invalid syntax.

Introduction Smalltalk Classes 6 / 20

slide-12
SLIDE 12

Amber

instances of Number are represented by the Javascript number

  • bjects

Boolean, and Date are similarly directly mapped String, Symbol and Character both use Javascript strings OrderedCollection is mapped to Javascript arrays to map to valid Javascript identifiers, message-names are prepended with _ and have every colon (:) replaced by _ because Javascript identifiers have only a single look-up mechanism, instance variables are prepended with @, which means they need to be looked up via the indexing method (obj[’@foo’]) because obj.@foo is invalid syntax.

Introduction Smalltalk Classes 6 / 20

slide-13
SLIDE 13

Amber

instances of Number are represented by the Javascript number

  • bjects

Boolean, and Date are similarly directly mapped String, Symbol and Character both use Javascript strings OrderedCollection is mapped to Javascript arrays to map to valid Javascript identifiers, message-names are prepended with _ and have every colon (:) replaced by _ because Javascript identifiers have only a single look-up mechanism, instance variables are prepended with @, which means they need to be looked up via the indexing method (obj[’@foo’]) because obj.@foo is invalid syntax.

Introduction Smalltalk Classes 6 / 20

slide-14
SLIDE 14

Semantic Map

Everything is an object. null, undefined doesNotUnderstand: message to the object. undefined → exception Boolean only:true and false; otherwise signal a mustBeBoolean error. self refers to the current object and super refers to the current

  • bject, but with method resolution starting with the superclass of

the current code. A return from a Smalltalk block returns from the method in which the block is statically defined.

Introduction Smalltalk Classes 7 / 20

slide-15
SLIDE 15

Semantic Map

Everything is an object. null, undefined doesNotUnderstand: message to the object. undefined → exception Boolean only:true and false; otherwise signal a mustBeBoolean error. self refers to the current object and super refers to the current

  • bject, but with method resolution starting with the superclass of

the current code. A return from a Smalltalk block returns from the method in which the block is statically defined.

Introduction Smalltalk Classes 7 / 20

slide-16
SLIDE 16

Semantic Map

Everything is an object. null, undefined doesNotUnderstand: message to the object. undefined → exception Boolean only:true and false; otherwise signal a mustBeBoolean error. self refers to the current object and super refers to the current

  • bject, but with method resolution starting with the superclass of

the current code. A return from a Smalltalk block returns from the method in which the block is statically defined.

Introduction Smalltalk Classes 7 / 20

slide-17
SLIDE 17

Semantic Map

Everything is an object. null, undefined doesNotUnderstand: message to the object. undefined → exception Boolean only:true and false; otherwise signal a mustBeBoolean error. self refers to the current object and super refers to the current

  • bject, but with method resolution starting with the superclass of

the current code. A return from a Smalltalk block returns from the method in which the block is statically defined.

Introduction Smalltalk Classes 7 / 20

slide-18
SLIDE 18

Semantic Map

Everything is an object. null, undefined doesNotUnderstand: message to the object. undefined → exception Boolean only:true and false; otherwise signal a mustBeBoolean error. self refers to the current object and super refers to the current

  • bject, but with method resolution starting with the superclass of

the current code. In Javascript, this refers to the object from-which the name lookup was done that lead to the current function executing. A return from a Smalltalk block returns from the method in which the block is statically defined.

Introduction Smalltalk Classes 7 / 20

slide-19
SLIDE 19

Semantic Map

Everything is an object. null, undefined doesNotUnderstand: message to the object. undefined → exception Boolean only:true and false; otherwise signal a mustBeBoolean error. self refers to the current object and super refers to the current

  • bject, but with method resolution starting with the superclass of

the current code. A return from a Smalltalk block returns from the method in which the block is statically defined.

Introduction Smalltalk Classes 7 / 20

slide-20
SLIDE 20

Semantic Map

A return from a Smalltalk block returns from the method in which the block is statically defined.

1 foo: n 2 n timesRepeat: [ 3 ^n 4 ]. 5 ^ -1

Introduction Smalltalk Classes 7 / 20

slide-21
SLIDE 21

Semantic Map

A return from a Smalltalk block returns from the method in which the block is statically defined.

1 foo: n 2 n timesRepeat: [ 3 ^n 4 ]. 5 ^ -1 1 function _foo_(n) { 2 var $exit=Object.create(null); 3 try { 4 n._timesRepeat_(function(){throw n;}) 5 } catch ($e) {if ($e===$exit) return $e;throw $e} 6 }

Introduction Smalltalk Classes 7 / 20

slide-22
SLIDE 22

ProtoObject __proto__ prototype constructor methods/fields Class prototype ProtoObject prototype __proto__ constructor methods null ProtoObject class __proto__ prototype constructor Metaclass prototype Metaclass Object __proto__ prototype constructor methods/fields Object pro- totype __proto__ constructor methods Object class __proto__ prototype constructor UserCls __proto__ prototype constructor methods/fields UserCls prototype __proto__ constructor methods UserCls class __proto__ prototype constructor a UserCls __proto__ fields another UserCls __proto__ fields an Object __proto__ fields a non-Smalltalk Object __proto__ fields

__proto__ constructor prototype

Introduction Amber-Direct Smalltalk Class Structure 8 / 20

slide-23
SLIDE 23

Browser Test Version

  • Min. Version

Release Date Chrome 43.0.2357.65 34 2014-04 Firefox 38.0.1 31 2014-07 Internet- Explorer 11.0.19 11 2013-10 NodeJS 0.12.2 0.10? 2013 Opera 29.0 13 2013 Safari 8.0.6 7.0? 2013

JS Engines supporting .setPrototypeOf

Introduction 9 / 20

slide-24
SLIDE 24

Microbenchmarks

run on an idle Apple Macbook Pro, with 2.5GHz Intel i5 processor IE11 on virtual machine at least 100,000 operations per “run” 10 warmup runs discarded before 10 captured runs

Timings 10 / 20

slide-25
SLIDE 25

amber asBool(v) constructor (v.constructor===Boolean?v:v.nonbool()) eq (v===true||(v===false?v:v.nonbool())) instance (v instanceof Boolean?v:v.nonbool()) typeOf (typeof v==="boolean"?v:v.nonbool()) valueOf Boolean.prototype.valueOf.call(v)

Chrome Firefox IE11 NodeJS Opera Safari 100 200 scaled timings amber constructor eq instanceof typeof valueOf

Timings Booleans 11 / 20

slide-26
SLIDE 26

null, undefined and doesNotUnderstand:

1 function $recv(o){ 2 if (o == null) return nil; 3 if (typeof o === "object" || 4 typeof o === "function") { 5 return o.klass != null ? o : 6 globals.JSObjectProxy._on_(o); 7 } 8 return o; 9 }

Timings 12 / 20

slide-27
SLIDE 27

1 function MyObject() { 2 this.abc=4; 3 } 4 MyObject.prototype.getter=function getter() {...}; 5 MyObject.prototype.setter=function setter(x) {...}; 1 getvar: obj for: n 2 | tot | 3 tot := 0. 4 n timesRepeat: [ 5 tot := tot + obj abc 6 ]. 7 ^ tot

Timings doesNotUnderstand: 13 / 20

slide-28
SLIDE 28

ChromeFirefox IE11 NodeJS Opera Safari 100 200 scaled timings getVard getVarp getterd getterp putVard putVarp setterd setterp

Timings doesNotUnderstand: 14 / 20

slide-29
SLIDE 29

amber $amber(f).foo() eq2 (f==null?nil:f).foo()

  • r

(f||(f==null?nil:f)).foo()

  • r2

(f||(f===undefined||f===null?nil:f)).foo()

  • r3

(f||(f===null||f===undefined?nil:f)).foo()

  • r4

(f||(f===null?nil:f===undefined?nil:f)).foo() recv $recv(f).foo() recvOr $recvOr(f).foo()

  • rRecv

(f||$recv(f)).foo()

Chrome Firefox IE11 NodeJS Opera Safari 100 150 scaled timings amber eq2

  • r
  • r2
  • rRecv

recv recvOr

Timings null, undefined 15 / 20

slide-30
SLIDE 30

dotget

  • .ghi

dotput

  • .ghi=17

idxget

  • [’ghi’]

idxput

  • [’ghi’]=17

Chrome Firefox IE11 NodeJS Opera Safari 100 200 scaled timings dotget dotput idxget idxput

Timings Access to instance variables 16 / 20

slide-31
SLIDE 31

always n+m alwaystes typeof m==="number"?n+m:m.adaptN(n,’+’) send n.__plus_(m) sendtest n.__plusT_(m) test typeof n==="number"?n+m:n.__plus_(m) testtest typeof n==="number"?typeof m==="number"?n+m: m.adaptN(n,’+’):n.__plusT_(m)

Chrome Firefox IE11 NodeJS Opera Safari 100 200 300 scaled timings always alwaystest send sendtest test testtest

Timings Optimizing Numeric Calculations 17 / 20

slide-32
SLIDE 32

Conclusions

can make Javascript align well with Smalltalk can get close to native Javascript performance some surprising performance characteristics

Conclusions 18 / 20

slide-33
SLIDE 33

Roadmap

information for Amber or PharoJS currently working on streamlining Amber JS-object handling number-handling is next priority

Conclusions 19 / 20

slide-34
SLIDE 34

.

Questions?

Thanks to Nicholas Petton, Herbert Vojˇ cík and many others for Amber

Conclusions 20 / 20