Slide 1

Slide 1 text

Vladimir Agafonkin High Performance Data Visualizations April 2013

Slide 2

Slide 2 text

rain.in.ua

Slide 3

Slide 3 text

agafonkin.com /mourner

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

data visualization

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

not static anymore

Slide 14

Slide 14 text

responding to user actions clicking, hovering, scrolling, touch gestures, etc.

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

navigating through data

Slide 19

Slide 19 text

filtering

Slide 20

Slide 20 text

demand for real-time interactivity is increasing

Slide 21

Slide 21 text

responsibility for processing data is shifting from the server to the browser

Slide 22

Slide 22 text

lots of data + lots of rendering = big performance problem

Slide 23

Slide 23 text

pure JS fast rendering, DOM slow

Slide 24

Slide 24 text

rule #1 the less stuff we render the better

Slide 25

Slide 25 text

data processing rendering

Slide 26

Slide 26 text

data processing rendering

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

data reduction

Slide 29

Slide 29 text

search

Slide 30

Slide 30 text

loading data once searching data lots of times

Slide 31

Slide 31 text

search index

Slide 32

Slide 32 text

grid

Slide 33

Slide 33 text

tree data structures •binary heap •binary search tree •range tree •k-d tree •quadtree •R-tree

Slide 34

Slide 34 text

points – quadtree

Slide 35

Slide 35 text

rectangles – R-tree

Slide 36

Slide 36 text

var tree = new RTree(); tree.insert( {x: 5, y: 10, w: 10, h: 15}, obj); ... tree.search( {x: 7, y: 7, w: 5, h: 5}); github.com/imbcmdth/RTree

Slide 37

Slide 37 text

kothic.org/js

Slide 38

Slide 38 text

collision detection

Slide 39

Slide 39 text

Crossfilter (many dimensions)

Slide 40

Slide 40 text

geometric clipping

Slide 41

Slide 41 text

polyline clipping Cohen-Sutherland algorithm

Slide 42

Slide 42 text

polygon clipping Sutherland-Hodgeman algorithm

Slide 43

Slide 43 text

polyline simplification

Slide 44

Slide 44 text

distance-based simplification

Slide 45

Slide 45 text

Douglas-Peucker simplification

Slide 46

Slide 46 text

mourner.github.com/simplify-js

Slide 47

Slide 47 text

clustering grouping objects which are close to each other

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

hierarchical clustering once for all zoom levels

Slide 51

Slide 51 text

data loading and processing

Slide 52

Slide 52 text

UI JS browser freezes on heavy calculations UI

Slide 53

Slide 53 text

Web Workers

Slide 54

Slide 54 text

... worker.postMessage(HUGE_DATA_ARRAY); ... loading and sending to Worker

Slide 55

Slide 55 text

UI Worker JS browser freezes on data loading and sending data loading UI

Slide 56

Slide 56 text

importScripts('data.js'); ... onmessage = function (e) { var result = searchData(e.data.query); postMessage(result); } Loading in Worker

Slide 57

Slide 57 text

UI Worker JS browser freezes when receiving data from Worker data loading UI UI

Slide 58

Slide 58 text

var array = new Float16Array(len); ... var buffer = array.buffer; postMessage(buffer, [buffer]); // buffer stops being available transferable objects (Chrome, Firefox)

Slide 59

Slide 59 text

UI Worker JS browser doesn't freeze, data is sent as ArrayBuffer data loading UI UI

Slide 60

Slide 60 text

function addNumbers(a, b) { 'use asm'; a = a | 0; // int b = +b; // double return +(a + b); // double } bright future — asm.js

Slide 61

Slide 61 text

bright future — asm.js •useful for computational bottlenecks •FF Nightly; V8 on the way •backwards compatibility!

Slide 62

Slide 62 text

rendering backends SVG, Canvas, WebGL

Slide 63

Slide 63 text

SVG •fast native events for interactivity •easy to update separate objects •slows down the browser (with a large number of objects)

Slide 64

Slide 64 text

Canvas •doesn't affect browser performance after rendering •you can draw something once and copy •pixel data can be manipulated or generated in a Worker

Slide 65

Slide 65 text

Canvas •expensive to redraw with each update •hard to implement interactivity

Slide 66

Slide 66 text

WebGL •main way to visualize in 3D •very fast in 2D if you need to draw lots of sprites •performance gain vs Canvas-2D is questionable in other cases •much more difficult to use; limitations •no support in iOS and IE9-10

Slide 67

Slide 67 text

low number of objects: use SVG lots of stuff to draw: use Canvas

Slide 68

Slide 68 text

Canvas Performance

Slide 69

Slide 69 text

redraw partially

Slide 70

Slide 70 text

draw once to an offscreen canvas and copy

Slide 71

Slide 71 text

function drawStar(ctx, x, y) { ... } drawStar(ctx, 10, 20); drawStar(ctx, 50, 70); ...

Slide 72

Slide 72 text

function drawStar() { ... return canvas; } var star = drawStar(); ctx.drawImage(star, 10, 20); ctx.drawImage(star, 50, 70); ...

Slide 73

Slide 73 text

minimize stroke/fill

Slide 74

Slide 74 text

function drawLine(x1, x2, y1, y2) { ctx.strokeStyle = 'red'; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); } drawLine(10, 20, 30, 40); drawLine(200, 10, 0, 50); drawLine(30, 40, 70, 0);

Slide 75

Slide 75 text

function drawLine(x1, x2, y1, y2) { ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); } ctx.strokeStyle = 'red'; drawLine(10, 20, 30, 40); drawLine(200, 10, 0, 50); drawLine(30, 40, 70, 0); ctx.stroke();

Slide 76

Slide 76 text

generate or manipulate raw pixel data in a Worker

Slide 77

Slide 77 text

var data = ctx.getImageData(0, 0, width, height).data; worker.postMessage(data, [data]); ... worker.onmessage = function (e) { var imageData = ctx.createImageData(width, height); imageData.data.set(e.data); ctx.putImageData(imageData, 0, 0); } Canvas + Worker

Slide 78

Slide 78 text

var pixels = new Uint8ClampedArray( width * height); function drawPixel(x, y, r, g, b, a) { var i = 4 * (256 * y + x); pixels[i] = r; pixels[i + 1] = g; pixels[i + 2] = b; pixels[i + 3] = a; } ... postMessage(pixels.buffer, [pixels.buffer]); drawing pixels in a Worker

Slide 79

Slide 79 text

var pixels = new Uint8ClampedArray(data); for (var x = 0; x < width; x++) { for (var y = 0; y < height; y++) { var i = 4 * (256 * y + x); pixels[i] = 2 * pixels[i]; pixels[i + 1] = 2 * pixels[i + 1]; pixels[i + 2] = 2 * pixels[i + 2]; } } ... postMessage(pixels.buffer, [pixels.buffer]); processing pixels in a Worker

Slide 80

Slide 80 text

hover lookup table

Slide 81

Slide 81 text

canvas + pixel color

Slide 82

Slide 82 text

UTFGrid •65535 symbols •each symbol is 4х4 pixels •1-3 KB per 256х256 tile in average

Slide 83

Slide 83 text

Thanks! Questions? Vladimir Agafonkin [email protected]