workers of the web - braziljs 2013

105
Thibault Imbert Group Product Manager | Web Platform Workers of the Web Thibault Imbert @thibault_imbert Friday, August 23, 13

Upload: thibault-imbert

Post on 15-Jan-2015

12.345 views

Category:

Technology


1 download

DESCRIPTION

Concurrency in JavaScript, the potential and limitations.

TRANSCRIPT

Page 1: Workers of the web - BrazilJS 2013

Thibault ImbertGroup Product Manager | Web Platform

Workers of the WebThibault Imbert@thibault_imbert

Friday, August 23, 13

Page 2: Workers of the web - BrazilJS 2013

Records

Friday, August 23, 13

Page 3: Workers of the web - BrazilJS 2013

Brazilian disco-funk

Friday, August 23, 13

Page 4: Workers of the web - BrazilJS 2013

Brazilian disco-funk

Friday, August 23, 13

Page 5: Workers of the web - BrazilJS 2013

Brazilian disco-funk

Friday, August 23, 13

Page 6: Workers of the web - BrazilJS 2013

Brazilian disco-funk

Friday, August 23, 13

Page 7: Workers of the web - BrazilJS 2013

Brazil’s food

Friday, August 23, 13

Page 8: Workers of the web - BrazilJS 2013

Brazil’s food

Friday, August 23, 13

Page 9: Workers of the web - BrazilJS 2013

Brazil

Friday, August 23, 13

Page 10: Workers of the web - BrazilJS 2013

Estou me mudando para o Brasil

Friday, August 23, 13

Page 11: Workers of the web - BrazilJS 2013

Adobe & the web?

Friday, August 23, 13

Page 12: Workers of the web - BrazilJS 2013

Adobe & the web?

http://html.adobe.com/webplatform/

Friday, August 23, 13

Page 13: Workers of the web - BrazilJS 2013

Friday, August 23, 13

Page 14: Workers of the web - BrazilJS 2013

JavaScript concurrency

Friday, August 23, 13

Page 15: Workers of the web - BrazilJS 2013

JavaScript concurrencyMultithreading

Friday, August 23, 13

Page 16: Workers of the web - BrazilJS 2013

A single threaded world

Friday, August 23, 13

Page 17: Workers of the web - BrazilJS 2013

Examples

Friday, August 23, 13

Page 18: Workers of the web - BrazilJS 2013

Your budget

1000ms / 60fps = 16.67ms

Friday, August 23, 13

Page 19: Workers of the web - BrazilJS 2013

A frame

Rendering (repaint, reflow, etc.)

Network (XHR)

JavaScript (misc logic)6ms

2ms

8ms

16ms

UI Thread

Friday, August 23, 13

Page 20: Workers of the web - BrazilJS 2013

The budget

6ms

2ms

8ms

Budget

16ms

Rendering (repaint, reflow, etc.)

Network (XHR)

JavaScript (misc logic)

UI Thread

Friday, August 23, 13

Page 21: Workers of the web - BrazilJS 2013

12ms

4ms

10ms

Budget

Over budget

26ms

Rendering (repaint, reflow, etc.)

Network (XHR)

JavaScript (misc logic)

UI Thread

Friday, August 23, 13

Page 22: Workers of the web - BrazilJS 2013

Over budget

12ms

4ms

10ms

Budget

10ms

Rendering (repaint, reflow, etc.)

Network (XHR)

JavaScript (misc logic)

26ms

UI Thread

UI Impact

Friday, August 23, 13

Page 23: Workers of the web - BrazilJS 2013

6ms

4ms

20ms

Budget

Over budget

30ms

Rendering (repaint, reflow, etc.)

Network (XHR)

JavaScript (misc logic)

UI impact

UI Thread

Friday, August 23, 13

Page 24: Workers of the web - BrazilJS 2013

Dev tools

Time spent

Impact on fps

Friday, August 23, 13

Page 25: Workers of the web - BrazilJS 2013

So how do we fix that?

Friday, August 23, 13

Page 26: Workers of the web - BrazilJS 2013

Web Workers

Friday, August 23, 13

Page 27: Workers of the web - BrazilJS 2013

Support

Friday, August 23, 13

Page 28: Workers of the web - BrazilJS 2013

Detection

Friday, August 23, 13

Page 29: Workers of the web - BrazilJS 2013

Detection

function supports_web_workers() { return window.Worker != null;}

Friday, August 23, 13

Page 30: Workers of the web - BrazilJS 2013

Detection

function supports_web_workers() { return window.Worker != null;}

if (Modernizr.webworkers) { // Workers are available} else { // fallback}

Friday, August 23, 13

Page 31: Workers of the web - BrazilJS 2013

Most common use case

1. Responsive programming

When you have an expensive computation to perform and don’t want to block the UI.

Friday, August 23, 13

Page 32: Workers of the web - BrazilJS 2013

By default, the UI thread

Rendering

Network

JavaScript6ms

2ms

8ms

16ms

UI Thread

Friday, August 23, 13

Page 33: Workers of the web - BrazilJS 2013

In parallel

Rendering

Network

JavaScript6ms

2ms

8ms

16ms

UI Thread(Thread 1)

10ms

Web Worker(Thread 2)

JavaScript

Friday, August 23, 13

Page 34: Workers of the web - BrazilJS 2013

In parallel

Rendering

Network

JavaScript6ms

2ms

8ms

16ms 28ms

JavaScript

UI Thread(Thread 1)

Web Worker(Thread 2)

Network

Friday, August 23, 13

Page 35: Workers of the web - BrazilJS 2013

In parallel

Rendering

Network

JavaScript6ms

2ms

8ms

16ms 44ms

JavaScript

UI Thread(Thread 1)

Web Worker(Thread 2)

Network

Friday, August 23, 13

Page 36: Workers of the web - BrazilJS 2013

In parallel

Rendering

Network

JavaScript6ms

2ms

8ms

16ms 44ms

UI Thread(Thread 1)

Web Worker(Thread 2)

Loading remote data

Parsing of XHR dataEncoding/DecodingPhysicsAI (pathfinding, etc.)

Friday, August 23, 13

Page 37: Workers of the web - BrazilJS 2013

Life as a worker

What is available:

XMLHttpRequestThe Application Cache

Spawning other web workersThe navigator object

The location object (read-only)setTimeout()/clearTimeout() and setInterval()/clearInterval()

Importing external scripts using the importScripts() method

What is not:

The DOMThe window object

The document objectThe parent object

Friday, August 23, 13

Page 38: Workers of the web - BrazilJS 2013

Web Workers == Threads ?

Workers and threads

Friday, August 23, 13

Page 39: Workers of the web - BrazilJS 2013

1. Workers are higher-level than threads and safe

First difference

Friday, August 23, 13

Page 40: Workers of the web - BrazilJS 2013

Why not threads?

Threads are hard to use.

Too low-level for web development.

We should not expose that level of complexity to web developers.

Locks, manual synchronization, race conditions, really?

Friday, August 23, 13

Page 41: Workers of the web - BrazilJS 2013

var worker = new Worker("background.js");

JS VM Impact on memorySlow instantiation time

Thread

Higher-level than threads

Own heap and stack

Friday, August 23, 13

Page 42: Workers of the web - BrazilJS 2013

2. Data passing

Second difference

Friday, August 23, 13

Page 43: Workers of the web - BrazilJS 2013

Threads and memory

Shared memory

Thread 2

Thread 5

Thread 1

Thread 4

Thread 3

Friday, August 23, 13

Page 44: Workers of the web - BrazilJS 2013

Threads and memory

Shared memory

Thread 2

Thread 5

Thread 1

Thread 4

Thread 3

Friday, August 23, 13

Page 45: Workers of the web - BrazilJS 2013

Threads and memory

Friday, August 23, 13

Page 46: Workers of the web - BrazilJS 2013

With Web Workers, nothing is shared

var worker1 = new Worker("background-task-1.js");

var worker3 = new Worker("background-task-3.js");

var worker2 = new Worker("background-task-2.js");

Cloning

Cloning

Transfer of ownership

Friday, August 23, 13

Page 47: Workers of the web - BrazilJS 2013

1 Web Worker == 1 core ?

Workers and cores

Friday, August 23, 13

Page 48: Workers of the web - BrazilJS 2013

Workers and cores

UI Thread(Thread 1)

Web Worker(Thread 2)

Tim

e

Single CPU

Friday, August 23, 13

Page 49: Workers of the web - BrazilJS 2013

Workers and cores

Tim

e

Multicore CPU

UI Thread(Thread 1)

Web Worker(Thread 2)

Friday, August 23, 13

Page 50: Workers of the web - BrazilJS 2013

Workers and cores

Friday, August 23, 13

Page 51: Workers of the web - BrazilJS 2013

Workers and cores

1. It is not possible to specify if a Web Worker should run on a specific core.

Friday, August 23, 13

Page 52: Workers of the web - BrazilJS 2013

Workers and cores

1. It is not possible to specify if a Web Worker should run on a specific core.

2. Web Workers on a single core CPU, will not run in parallel but will still not block the UI.

Friday, August 23, 13

Page 53: Workers of the web - BrazilJS 2013

Workers and cores

1. It is not possible to specify if a Web Worker should run on a specific core.

2. Web Workers on a single core CPU, will not run in parallel but will still not block the UI.

3. On a multicore CPU, Web Workers can run truly in parallel if the OS decides to.

Friday, August 23, 13

Page 54: Workers of the web - BrazilJS 2013

mostra o código!

Friday, August 23, 13

Page 55: Workers of the web - BrazilJS 2013

Creating a worker

// create a new workervar worker = new Worker("background.js");

Friday, August 23, 13

Page 56: Workers of the web - BrazilJS 2013

Background logic

self.postMessage('Hello from Web Worker!');

// create a new workervar worker = new Worker("background.js");

Current worker

Friday, August 23, 13

Page 57: Workers of the web - BrazilJS 2013

Background logic

self.postMessage('Hello from Web Worker!');

// create a new workervar worker = new Worker("background.js");

Current worker Send data

Friday, August 23, 13

Page 58: Workers of the web - BrazilJS 2013

Background logic

self.postMessage('Hello from Web Worker!');

// create a new workervar worker = new Worker("background.js");

// listen to the response from the Workerworker.addEventListener('message', receiveMessage); // callback handling the response, data is available in the event object// outputs: Hello from Web Worker!function receiveMessage (e){    console.log (e.data);}

Current worker Send data

Friday, August 23, 13

Page 59: Workers of the web - BrazilJS 2013

Catching errors

var width = document.innerWidth;self.postMessage(width);

// create a new workervar worker = new Worker("background.js");

// listen to the response from the Workerworker.addEventListener('error', receiveMessage); // callback handling the error// outputs: Uncaught ReferenceError: document is not definedfunction receiveMessage (e){    console.log (e.data);}

Trying to access an object not available from a Worker

Friday, August 23, 13

Page 60: Workers of the web - BrazilJS 2013

Data types supported for communication

Friday, August 23, 13

Page 61: Workers of the web - BrazilJS 2013

Data types supported for communication

Primitive types: Number, String, Boolean.

Friday, August 23, 13

Page 62: Workers of the web - BrazilJS 2013

Data types supported for communication

Primitive types: Number, String, Boolean.

Composite data types: plain JSON objects, ImageData and TypedArray types.

Friday, August 23, 13

Page 63: Workers of the web - BrazilJS 2013

Running expensive computation

// create a new workervar worker = new Worker("background.js");

// listen to the response from the Workerworker.addEventListener('message', receiveMessage); // callback handling the response, data is available in the event object// outputs: 400000000function receiveMessage (e){    console.log (e.data);}

var slowSquare = function (n) { var i = 0; while (++i < n * n) {} return i;};

self.postMessage ( slowSquare( 20000 ) );

Friday, August 23, 13

Page 64: Workers of the web - BrazilJS 2013

Examples

Friday, August 23, 13

Page 65: Workers of the web - BrazilJS 2013

Data cloning

self.postMessage('Hello from Web Worker!');

self.postMessage([1, 13, 34, 50]);

self.postMessage({name: Bob, age: 30});

self.postMessage([{name: Tom, age: 20}]);

Friday, August 23, 13

Page 66: Workers of the web - BrazilJS 2013

Data cloning

Web Worker A Web Worker B

clone

Friday, August 23, 13

Page 67: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]

Web Worker A Web Worker B

clone

Friday, August 23, 13

Page 68: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]self.postMessage(data);

Web Worker A Web Worker B

clone

Friday, August 23, 13

Page 69: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]self.postMessage(data);

Web Worker A Web Worker B

[1, 13, 34, 50]clone

Friday, August 23, 13

Page 70: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]self.postMessage(data);

Web Worker A Web Worker B

[1, 13, 34, 50]clone

data[2] = 100;

Friday, August 23, 13

Page 71: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]self.postMessage(data);

Web Worker A Web Worker B

[1, 13, 34, 50]clone

var incomingArray = e.data;

data[2] = 100;

Friday, August 23, 13

Page 72: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]self.postMessage(data);

Web Worker A Web Worker B

[1, 13, 34, 50]clone

var incomingArray = e.data;// outputs: [1, 2, 3, 4] console.log (incomingArray);data[2] = 100;

Friday, August 23, 13

Page 73: Workers of the web - BrazilJS 2013

Data cloning

var data = [1, 2, 3, 4]self.postMessage(data);

Web Worker A Web Worker B

[1, 13, 34, 50]clone

var incomingArray = e.data;// outputs: [1, 2, 3, 4] console.log (incomingArray);data[2] = 100;

Overhead

Friday, August 23, 13

Page 74: Workers of the web - BrazilJS 2013

Sending a 16mb typedarray with cloning

// Create a 16MB "file" and fill it.var uInt8View = new Uint8Array(1024*1024*16);for (var i = 0; i < uInt8View.length; ++i) {    uInt8View[i] = i;}

// transfer ownership to the workerworker.postMessage(uInt8View.buffer);

Friday, August 23, 13

Page 75: Workers of the web - BrazilJS 2013

Platform Time (ms)

iOS7 (Safari/iPhone 5) 214

iOS6 (Safari/iPhone 4S) 524

MacBook Pro (Chrome/10.8.4) 75

Sending a 16mb typedarray

Friday, August 23, 13

Page 76: Workers of the web - BrazilJS 2013

Transfer of ownership

// Create a 16MB "file" and fill it.var uInt8View = new Uint8Array(1024*1024*16);for (var i = 0; i < uInt8View.length; ++i) {    uInt8View[i] = i;}

// transfer ownership to the workerworker.postMessage(uInt8View.buffer, [uInt8View.buffer]);

Friday, August 23, 13

Page 77: Workers of the web - BrazilJS 2013

Transfer of ownership

Web Worker A Web Worker B

reference transferred to Web Worker B

Friday, August 23, 13

Page 78: Workers of the web - BrazilJS 2013

Transfer of ownership

var data = [1, 2, 3, 4]

Web Worker A Web Worker B

reference transferred to Web Worker B

Friday, August 23, 13

Page 79: Workers of the web - BrazilJS 2013

Transfer of ownership

var data = [1, 2, 3, 4]self.postMessage(uInt8View.buffer,

Web Worker A Web Worker B

reference transferred to Web Worker B

Friday, August 23, 13

Page 80: Workers of the web - BrazilJS 2013

Transfer of ownership

var data = [1, 2, 3, 4]self.postMessage(uInt8View.buffer, [uInt8View.buffer]);

Web Worker A Web Worker B

reference transferred to Web Worker B

Friday, August 23, 13

Page 81: Workers of the web - BrazilJS 2013

Transfer of ownership

var data = [1, 2, 3, 4]self.postMessage(uInt8View.buffer, [uInt8View.buffer]);

Web Worker A Web Worker B

reference transferred to Web Worker B

// triggers runtime exceptionuInt8View.buffer = 12;

Friday, August 23, 13

Page 82: Workers of the web - BrazilJS 2013

Transfer of ownership

var data = [1, 2, 3, 4]self.postMessage(uInt8View.buffer, [uInt8View.buffer]);

Web Worker A Web Worker B

reference transferred to Web Worker B

var incomingArray = e.data;

// triggers runtime exceptionuInt8View.buffer = 12;

Friday, August 23, 13

Page 83: Workers of the web - BrazilJS 2013

Transfer of ownership

var data = [1, 2, 3, 4]self.postMessage(uInt8View.buffer, [uInt8View.buffer]);

Web Worker A Web Worker B

reference transferred to Web Worker B

var incomingArray = e.data;// outputs: [1, 2, 3, 4] console.log (incomingArray);

// triggers runtime exceptionuInt8View.buffer = 12;

Friday, August 23, 13

Page 84: Workers of the web - BrazilJS 2013

Transfer of ownership, almost a 3x boost

Platform Time (ms)

iOS7 (Safari/iPhone 5) 80 (was 214)

iOS6 (Safari/iPhone 4S) 162 (was 524)

MacBook Pro (Chrome/10.8.4) 37 (was 75)

Friday, August 23, 13

Page 85: Workers of the web - BrazilJS 2013

Exciting use case

Friday, August 23, 13

Page 86: Workers of the web - BrazilJS 2013

Exciting use case

2. Parallel programming

Friday, August 23, 13

Page 87: Workers of the web - BrazilJS 2013

Exciting use case

2. Parallel programming

When you want to leverage multiple CPU cores by having computations running concurrently to solve a

specific task.

Friday, August 23, 13

Page 88: Workers of the web - BrazilJS 2013

Parallelization

Friday, August 23, 13

Page 89: Workers of the web - BrazilJS 2013

Parallelization

Friday, August 23, 13

Page 90: Workers of the web - BrazilJS 2013

Parallelization

Worker 1 Worker 2

Worker 4Worker 3

Friday, August 23, 13

Page 91: Workers of the web - BrazilJS 2013

But...

Friday, August 23, 13

Page 92: Workers of the web - BrazilJS 2013

But...

1. Cloning overhead is high and limits parallelization opportunities.

Friday, August 23, 13

Page 93: Workers of the web - BrazilJS 2013

But...

1. Cloning overhead is high and limits parallelization opportunities.

2. Transfer of ownership does not work for parallelization.

Friday, August 23, 13

Page 94: Workers of the web - BrazilJS 2013

But...

1. Cloning overhead is high and limits parallelization opportunities.

2. Transfer of ownership does not work for parallelization.

3. A more efficient communication model has to be designed for Web Workers to allow faster

communication.

Friday, August 23, 13

Page 95: Workers of the web - BrazilJS 2013

But...

1. Cloning overhead is high and limits parallelization opportunities.

2. Transfer of ownership does not work for parallelization.

3. A more efficient communication model has to be designed for Web Workers to allow faster

communication. Software transactional memory?

Friday, August 23, 13

Page 96: Workers of the web - BrazilJS 2013

Friday, August 23, 13

Page 97: Workers of the web - BrazilJS 2013

OK, let’s see if you remember the main points.

Friday, August 23, 13

Page 98: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 99: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 100: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 101: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 102: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 103: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 104: Workers of the web - BrazilJS 2013

To summarize

1. Leverage Web Workers if you don’t want your UI to lock: true or false?

2. Web Workers are available pretty much everywhere on desktop and mobile: true or false?

3. Web Workers are as low-level as threads: true or false?

4. Message cloning is faster than transfer of ownership: true or false?

5. Web Workers work great to keep the UI responsive: true or false?

6. Parallel programming and responsive programming are the same things: true or false?

Friday, August 23, 13

Page 105: Workers of the web - BrazilJS 2013

@thibault_imbert

Blog: typedarray.org

obrigado!

Slides: http://bit.ly/13Lbk8d/More details: http://bit.ly/14IWKKj

Friday, August 23, 13