SWTT 140407 session04

30
JavaScript in the small Satish Chandra Manu Sridharan, Simon Jensen, Koushik Sen, Ming Jin

description

SWTT(7th Apr 2014) Session 04 - Title: "JavaScript in the Small" - Speaker: Satish Chandra, Samsung

Transcript of SWTT 140407 session04

Page 1: SWTT 140407 session04

JavaScript in the smallSatish Chandra

!Manu Sridharan, Simon Jensen, Koushik Sen, Ming Jin

Page 2: SWTT 140407 session04

Wearables computers have arrived

Products from Samsung, Pebble, Google, Qualcomm, Nike and Fitbit

Page 3: SWTT 140407 session04

Wearable computers

• Form factor: capable of being worn for an extended period of time

• Smart: advanced processing and wireless connectivity

• Programmable: extend functionality by installing [third-party] apps

Page 4: SWTT 140407 session04

Outline

• Programming models for wearables

• Why JavaScript?

• Application memory considerations

• Evolving JavaScript

Page 5: SWTT 140407 session04

What’s different from smartphones?

• Less resources than on a smartphone

CPU, memory, battery capacity

• No independent WAN connectivity (current generation)

Make do with Bluetooth to a host

• Smaller (or no) display

• Sensors enabled by form factor

Page 6: SWTT 140407 session04

Application models• Standalone applications

• Applications dependent on a host (typically a smartphone)

Bluetooth connectivity to the host

Some future devices may have direct connectivity to the internet

Page 7: SWTT 140407 session04

Gear application model standalone

Wearable web app (HTML/CSS/JS)

Not

ifica

tion

Com

mun

icat

ion

Dev

ice

API

Page 8: SWTT 140407 session04

Gear application model companion

Wearable web app (HTML/CSS/JS)

Not

ifica

tion

Com

mun

icat

ion

Dev

ice

API

Host app

Not

ifica

tion

Com

mun

icat

ion

Page 9: SWTT 140407 session04

Template-based app model Wearable web app*

Not

ifica

tion

Com

mun

icat

ion

Dev

ice

API

Host app

Not

ifica

tion

Com

mun

icat

ion

Template manager

A template is equipped to display a JSON object in a fixed UI format. A template manager supports a set of templates. Does not need full web app support

Page 10: SWTT 140407 session04

Why JavaScript?• Popular. Atwood’s law.

• Runs in smartphones, desktops, servers, and even in embedded controllers

• Suitable for event-based programming

• Easy to build responsive UIs for various screen resolutions (with HTML/CSS)

Page 11: SWTT 140407 session04

WearScript

• JavaScript on Google Glass

• Motivation: rapid experimentation with apps

http://www.wearscript.com/en/latest/

Page 12: SWTT 140407 session04

JS in embedded controllers

48 KB RAM32 MB RAM

Page 13: SWTT 140407 session04

JavaScript, efficiently• Wearables are likely to have less computational

resources than smartphones

• Should wearables pay for a full-fledged JavaScript environment?

Less memory

Expectation of longer battery life too

• What if, realistic apps on wearables need only limited scripting support?

Page 14: SWTT 140407 session04

Memory size

2011 2012 2013 20142010

0.5G

1.0G

2.0G

?

Page 15: SWTT 140407 session04

JavaScript Memory Best Practices

Page 16: SWTT 140407 session04

Avoid Memory Leaks• Space leaks: objects kept reachable after last use

• If reachable, cannot be collected by GC

• With old browsers, also GC bugs with DOM objects

• Workarounds in frameworks (GWT, jQuery)

• Less of an issue with modern browsers

Page 17: SWTT 140407 session04

Leak Patterns• Closures: sometimes retain unexpected pointers

• Detached DOM nodes: Pointers from JavaScript heap prevent GC

• Event listeners: additional pointers must be cleared when DOM node dies

• Standard issues: caches, etc.

Page 18: SWTT 140407 session04

Diagnosing Leaks

Heap Snapshots in Chrome Dev Tools https://developers.google.com/chrome-developer-tools/docs/javascript-memory-profiling

Page 19: SWTT 140407 session04

“3 Snapshot” Technique1. Take a snapshot

2. Perform possibly-leaking action

3. Do steps 1 and 2 again

4. Take a third snapshot.

5. In third snapshot, view objects allocated between first and second snapshot (i.e., leaked objects)

Page 20: SWTT 140407 session04

Memory-Efficient Code• Avoid hash table object representation!

• For V8 objects, stable type structure!

• Don’t delete properties

• Don’t add properties unpredictably

• Same for arrays, and avoid mixing types

Page 21: SWTT 140407 session04

Staleness

• Staleness: long gap between last use and garbage collection !• A high number of stale objects indicates a potential problem !• To prevent staleness the programmer should ensure that object

becomes unreachable sooner so they can be garbage collected

!• Typical causes of staleness: Closures, caches and event listeners

Object allocated ! Object used ! Object is unreachable !

Staleness!

Page 22: SWTT 140407 session04

Detecting Staleness

• Difficult to accomplish with periodic heap snapshots because “use” is not recorded

• A continuous dynamic analysis that records all allocations, reads and writes is needed

Also keep track of links between objects

Object allocated ! Object used ! Object is unreachable !

Staleness!

Page 23: SWTT 140407 session04

Jalangi• Jalangi is a framework for doing record and replay

of JavaScript programs Developed at Samsung

!• Recorded executions can be replayed with

different analyses !

• Supports recording in both browser and node.js !

• Open source: https://github.com/SRA-SiliconValley/jalangi

Page 24: SWTT 140407 session04

Leaner JavaScript?

Page 25: SWTT 140407 session04

Rendering JS runtime / GC JS interpreter

JS libs / frameworks (jQuery)

Application

Memory usage analyses: leak / bloat detection,

object-equality profiling

Framework specialization, lazy

loading

Ahead-of-time compilation

Quasi-static memory

management

Restricted DOM API

Page 26: SWTT 140407 session04

Rendering JS runtime / GC JS interpreter

JS libs / frameworks (jQuery)

Application

Memory usage analyses: leak / bloat detection,

object-equality profiling

Framework specialization, lazy

loading

Ahead-of-timecompilation

Quasi-staticmemory

management

Restricted DOM API

Language restrictions

Page 27: SWTT 140407 session04

Language Restrictions• asm.js

Only typed array

Enabler for AOT compilation: no GC, no unboxing, no runtime checks, efficient loads and stores, etc

• LLJS

C-like, manual memory management. (asm.js with C-like syntactic sugar)

• Statically type-able subsets

• Other proposals (interest from ECMA?)

Page 28: SWTT 140407 session04

Espruino• Executes from source!

• Every datatype is allocated in a 20-byte chunk (strings and typed arrays are packed)

• Objects use two chunks per (key, value) pair

• Reference counting and occasional mark-and-sweep

Page 29: SWTT 140407 session04

Tessel

• AOT compiles JavaScript to Lua byte-code outside the device

• The device contains a Lua JIT runtime

Page 30: SWTT 140407 session04

Questions?