the previous one v3 is ~8.5x slower than v0 PyPy abstractions (almost) for free v5 is ~18% slower than v0, v1, v2 antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 26 / 34
is optimized away? Very rough explanation For a deeper view: http://speakerdeck.com/u/antocuni/p/ pypy-jit-under-the-hood http: //www.youtube.com/watch?v=cMtBUvORCfU antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 27 / 34
0 while i < n: total += i i += 1 return total cdef loop0(i, n, total): assert isinstance(n, int) while True: assert i < n total = int_add_ovf(total, i) assert not_overflow(total) i = int_add_ovf(i, 1) assert not_overflow(i) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 28 / 34
0 while i < n: total += i i += 1 return total cdef loop0(i, n, total): assert isinstance(n, int) while True: assert i < n total = int_add_ovf(total, i) assert not_overflow(total) i = int_add_ovf(i, 1) assert not_overflow(i) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 28 / 34
while i < n: if i % 2: total += i else: total += (i-5) i += 1 return total cdef loop0(i, n, total): assert isinstance(n, int) while True: assert i < n assert i % 2 != 0 total = int_add_ovf(total, i) assert not_overflow(total) i = int_add_ovf(i, 1) assert not_overflow(i) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 29 / 34
while i < n: if i % 2: total += i else: total += (i-5) i += 1 return total cdef loop0(i, n, total): assert isinstance(n, int) while True: assert i < n assert i % 2 != 0 total = int_add_ovf(total, i) assert not_overflow(total) i = int_add_ovf(i, 1) assert not_overflow(i) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 29 / 34
while i < n: if i % 2: total += i else: total += (i-5) i += 1 return total cdef loop0(i, n, total): assert isinstance(n, int) while True: assert i < n if i % 2 != 0: total = int_add_ovf(total, i) assert not_overflow(total) i = int_add_ovf(i, 1) assert not_overflow(i) else: tmp = int_sub_ovf(i, 5) assert not_overflow(tmp) total = int_add_ovf(total, tmp) i = int_add_ovf(i, 1) assert not_overflow(i) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 30 / 34
total = 0 i = 0 while i < n: total = fn(total, i) i += 1 return total assert version(globals()) == 42 assert id(fn.__code__) == 0x1234 # assert isinstance(n, int) while True: assert i < n total = int_add_ovf(total, i) # inlined! assert not_overflow(total) i = int_add_ovf(i, 1) assert not_overflow(i) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 31 / 34
i < n: p = Point(i, i+1) total += p.distance() i += 1 return total assert ... assert isinstance(n, int) assert isinstance(i, float) while True: assert i < n # Point() is "virtualized" into p_x and p_y p_x = i p_y = float_add(i, 1.0) # # inlined call to Point.hypot tmp = c_call(math.hypot, p_x, p_y) total = float_add(total, tmp) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 33 / 34
i < n: p = Point(i, i+1) total += p.distance() i += 1 return total assert ... assert isinstance(n, int) assert isinstance(i, float) while True: assert i < n # Point() is "virtualized" into p_x and p_y p_x = i p_y = float_add(i, 1.0) # # inlined call to Point.hypot tmp = c_call(math.hypot, p_x, p_y) total = float_add(total, tmp) antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 33 / 34
14:00-15:30 Come and ask us questions! "PyPy meets Python 3 and numpy" Armin Rigo Friday, 14:00 Or, just talk to us :) @pypyproject, @antocuni antocuni (EuroPython 2017) PyPy: abstractions for free July 12 2017 34 / 34