rdio's alex gaynor at heroku's waza 2013: why python, ruby and javascript are slow

39
Why Python, Ruby, and Javascript are slow Alex Gaynor Waza 2013

Upload: heroku

Post on 05-Dec-2014

1.414 views

Category:

Documents


0 download

DESCRIPTION

Rdio Software Engineer Alex Gaynor (@alex_gaynor) took to the #Waza 2013 stage (Heroku's Developer Conference) to talk about "Why Python, Ruby and Javascript are Slow". Gaynor argues that developers should aim to make performance beautiful. For more from Gaynor or to contact him, ping him at @Alex_Gaynor. For more on Waza visit http://waza.heroku.com/2013. For Waza videos stay tuned at http://blog.heroku.com or visit http://vimeo.com/herokuwaza

TRANSCRIPT

Page 1: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Why Python, Ruby, and Javascript are slow

Alex GaynorWaza 2013

Page 2: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

You may know me from...

Page 3: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

rdio.com

Page 4: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

• CPython

• Django

• PyPy

Page 5: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Twitter rants about how computers are bad

Page 6: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Topaztopazruby.com

Page 7: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

There is no benchmark but your benchmark

Page 8: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Lame Excuses about why they’re Slow

Page 9: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Dynamic Typing

Page 10: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

“You can monkey patch anything”

Page 11: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Harder to Optimizevs.

Slow

Page 12: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

The Truth

Page 13: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Let’s talk about C

Page 14: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

struct Point { double x; double y; double z;};

Page 15: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

class Point(object): def __init__(self, x, y, z): self.x = x self.y = y self.z = z

Page 16: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

data = { "x" x, "y": y, "z": z,}

Page 17: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Dictionaryvs.

Object

Page 18: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

std::hash_set<std::string, double> point;point["x"] = x;point["y"] = y;point["z"] = z;

Page 19: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

And it would be slow

Page 20: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Why don’t people care?

Page 21: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Let’s talk about strings

Page 22: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Given a string matching: “\w+-\d+”

return the integral part of the value

Page 23: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

int(s.split("-", 1)[1])

Page 24: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

atoi(strchr(s, '-') + 1)

Page 25: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Things that take time

• Hash table lookups

• Allocations

• Copying

Page 26: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

The C way:

BYOB

Page 27: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

char *data = malloc(1024);while (true) { read(fd, data, 1024); char *start = data; while (start < data + 1024) { if (isspace(*start)) { break; } start++; } printf("%s\n", start);}

Page 28: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

while True: data = os.read(fd, 1024) print data.lstrip()

Page 29: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

long *squares(long n) { long *sq = malloc(sizeof(long) * n); for (long i = 0; i < n; i++) { sq[i] = i * i; } return sq;}

Page 30: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

def squares(n): sq = [] for i in xrange(n): sq.append(i * i) return sq

Page 31: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Missing APIs

Page 32: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

from __pypy__ import newlist_hintdef squares(n): sq = newlist_hint(n) for i in xrange(n): sq.append(i * i) return sq

Page 33: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Don’t make us add heuristics

Page 34: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Heuristics = WAG

Page 35: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Growing divide between optimizing and

not

Page 36: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Recap

• Line for line these languages are fast!

• Take care in data structures (data structure heuristics are the WORST)

• We need better no-copy/preallocate APIs

Page 37: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Don’t abandon beauty, simplicity, our values for

performance

Make performance beautiful.

Page 38: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

Thank you!https://speakerdeck.com/alex

@alex_gaynor

Page 39: Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are Slow

If there’s time

• Java collections vs. Array and Hash. Need more choices.

• Stop writing C extensions, use something like cffi

• Teach good benchmarking practices