Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update...
Transcript of Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update...
![Page 1: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/1.jpg)
Perl 6 Performance Update
Jonathan Worthington | Edument
![Page 2: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/2.jpg)
The challenge of running Perl 6 fast
The optimizations we're performing to rise to them
The results of various benchmarks
The consequences for those writing Perl 6 programs today
The plans for further improvement
![Page 3: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/3.jpg)
The challenge of running Perl 6 fast
![Page 4: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/4.jpg)
Compiler implemented in Perl 6
Built-ins implemented in Perl 6
Only "native" code is the VM (MoarVM is written in C)
![Page 5: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/5.jpg)
So to make Perl 6 fast, we must…
![Page 6: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/6.jpg)
So to make Perl 6 fast, we must…
…make Perl 6 fast!
![Page 7: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/7.jpg)
Perl 6 is very object-y
![Page 8: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/8.jpg)
Objects
Gather together related data and functionality
Let us work at a higher level of abstraction
Provide polymorphism
![Page 9: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/9.jpg)
Lots of simple things in Perl 6 are objects
Boxes Int Num Str
Numeric-ish Complex Date
DateTime Rat
Range Containers Scalar Array Hash
![Page 10: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/10.jpg)
Objects
− Cost of method resolution − Allocations mean more memory
pressure and more time doing garbage collection
− Harder to analyze/optimize the program
![Page 11: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/11.jpg)
for @values -> $v { # Allocate a Scalar $sv # sin returns a boxed Num my $sv = $v.sin; # + returns a boxed Num do-something(1e0 + $sv); }
![Page 12: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/12.jpg)
Objects are allocated in the GC nursery: a big blob of memory
When it's full, we garbage collect
Scalar Num Num Scalar Num Num
Next allocation
here
![Page 13: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/13.jpg)
Obvious consequence: The quicker we fill the nursery, the
more often we have to do GC, and so the more time we spend on GC
Less obvious consequence:
Objects are spread through memory, so we get lots of CPU cache misses
![Page 14: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/14.jpg)
Perl 6 has types…
…and we often enforce the type constraints at runtime
![Page 15: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/15.jpg)
sub shorten(Str $s, Int $chars) { $s.chars < $chars ?? $s !! $s.substr(0, $chars) ~ '...' }
![Page 16: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/16.jpg)
sub shorten(Str $s, Int $chars) { $s.chars < $chars ?? $s !! $s.substr(0, $chars) ~ '...' } multi infix:<< < >>(Int $a, Int $b) { … }
![Page 17: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/17.jpg)
sub shorten(Str $s, Int $chars) { $s.chars < $chars ?? $s !! $s.substr(0, $chars) ~ '...' } method substr(Int $from, Int $chars) { ... }
![Page 18: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/18.jpg)
sub shorten(Str $s, Int $chars) { $s.chars < $chars ?? $s !! $s.substr(0, $chars) ~ '...' } multi infix:<~>(Str $a, Str $b) { ... }
![Page 19: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/19.jpg)
Most operators are multi subs
Array and hash access are a call to a multi sub that in turn
performs a method call
![Page 20: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/20.jpg)
What if we were to try doing it that way in Perl 5?
![Page 21: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/21.jpg)
my $arr = [1,2,3]; my $total = 0; for (1..10_000_000) { $total += $total + $arr->[1] + $arr->[2]; } print "$total\n";
![Page 22: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/22.jpg)
0.509s
![Page 23: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/23.jpg)
sub at_pos { @_[0]->[@_[1]] } sub postcircumfix { at_pos(@_[0], @_[1]) } my $arr = [1,2,3]; my $total = 0; for (1..10_000_000) { $total += $total + postcircumfix($arr, 1) + postcircumfix($arr, 2); } print "$total\n";
![Page 24: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/24.jpg)
8.39s
![Page 25: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/25.jpg)
sub at_pos { @_[0]->[@_[1]] } sub postcircumfix { at_pos(@_[0], @_[1]) } sub infix_plus { @_[0] + @_[1] } my $arr = [1,2,3]; my $total = 0; for (1..10_000_000) { $total = infix_plus($total, infix_plus(postcircumfix($arr, 1), postcircumfix($arr, 2))); } print "$total\n";
![Page 26: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/26.jpg)
11.48s
![Page 27: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/27.jpg)
Except we didn't actually do any multi-dispatch…
![Page 28: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/28.jpg)
Except we didn't actually do any multi-dispatch…
And in Perl 6, Int is an object…
![Page 29: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/29.jpg)
Except we didn't actually do any multi-dispatch…
And in Perl 6, Int is an object…
And Int automatically upgrades
to a big integer too…
![Page 30: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/30.jpg)
Except we didn't actually do any multi-dispatch…
And in Perl 6, Int is an object…
And Int automatically upgrades
to a big integer too…
And Perl 6 arrays support laziness!
![Page 31: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/31.jpg)
my @arr = 1,2,3; my $total = 0; for ^10_000_000 { $total += @arr[1] + @arr[2]; } say $total;
So what about Perl 6?
![Page 32: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/32.jpg)
Christmas release:
10.3s
![Page 33: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/33.jpg)
Christmas release:
10.3s Faster than the Perl 5
"translation"
![Page 34: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/34.jpg)
Today:
0.886s
![Page 35: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/35.jpg)
Today:
0.886s Within 1.7x of Perl 5,
despite all of the extra abstraction and work
![Page 36: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/36.jpg)
Today:
0.886s Which 1.7x of Perl 5,
despite all of the extra abstraction and work
And a bit faster than the same benchmark
in Python
![Page 37: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/37.jpg)
Of course, nobody wants to know why it's challenging to go fast.
They just want it to be fast.
So, that's what we're doing.
![Page 38: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/38.jpg)
The optimizations we're performing to rise
to the challenges
![Page 39: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/39.jpg)
Programs that we want to develop and maintain
Programs that we want the computer to run
Optimizer
![Page 40: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/40.jpg)
Static optimizer in Rakudo
Dynamic optimizer in MoarVM
![Page 41: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/41.jpg)
Static Optimizations
Rewrites AST into faster constructs
Inlining of native operators
Lexical to local lowering
![Page 42: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/42.jpg)
The static optimizer is…
Mostly doing local transforms
Sticking to cheap analyses, because it doesn't know what's worth a more
sophisticated analysis
![Page 43: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/43.jpg)
The dynamic optimizer is responsible for the
big improvements
![Page 44: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/44.jpg)
On the array access benchmark, it gives a
30x speedup
![Page 45: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/45.jpg)
How?
![Page 46: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/46.jpg)
Bytecode
Intepreter
Stuff happens!
![Page 47: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/47.jpg)
Bytecode
Intepreter
Stuff happens!
Execution Log
![Page 48: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/48.jpg)
Bytecode
Intepreter
Stuff happens!
Execution Log
This parameter is an Int
![Page 49: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/49.jpg)
Bytecode
Intepreter
Stuff happens!
Execution Log
We did an iteration of this
loop
![Page 50: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/50.jpg)
Bytecode
Intepreter
Stuff happens!
Execution Log
Here, we called method foo
![Page 51: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/51.jpg)
Bytecode
Intepreter
Stuff happens!
Execution Log
The method call returned a Bool
![Page 52: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/52.jpg)
Meanwhile, on another thread…
![Page 53: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/53.jpg)
Oooh, a log packed full of statistics!
![Page 54: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/54.jpg)
Stack Simulation
Execution Log
Aggregated / linked statistics
![Page 55: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/55.jpg)
Stack Simulation
Execution Log
Aggregated / linked statistics
This loop did 100 iterations
![Page 56: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/56.jpg)
Stack Simulation
Execution Log
Aggregated / linked statistics
This sub was called 55 times
![Page 57: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/57.jpg)
Stack Simulation
Execution Log
Aggregated / linked statistics
This call returns Int
120 times and Nil 1 time
![Page 58: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/58.jpg)
Planner
Optimization plan
Aggregated / linked statistics
![Page 59: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/59.jpg)
Planner
Optimization plan
Aggregated / linked statistics
Optimize infix:<+> for
(Int, Int)
![Page 60: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/60.jpg)
Planner
Optimization plan
Aggregated / linked statistics
Optimize AT-POS for (Array, Int)
![Page 61: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/61.jpg)
Bytecode isn't suitable for efficient program analysis
So, we parse it into a more
suitable data structure
![Page 62: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/62.jpg)
BB 1
BB 2
BB 3 BB 4
BB 5
BB 6
Loop Conditional
Control Flow Graph
![Page 63: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/63.jpg)
BB 1
BB 2
BB 3 BB 4
BB 5
BB 6
Dominance tree
Block Immediately Dominates
BB1 BB2
BB2 BB3, BB4, BB5
BB3
BB4
BB5 BB6
BB6
![Page 64: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/64.jpg)
Dominance tree
Block Immediately Dominates
BB1 BB2
BB2 BB3, BB4, BB5
BB3
BB4
BB5 BB6
BB6
BB 1
BB 2
BB 3 BB 4
BB 5
BB 6
![Page 65: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/65.jpg)
Dominance tree
Block Immediately Dominates
BB1 BB2
BB2 BB3, BB4, BB5
BB3
BB4
BB5 BB6
BB6
BB 1
BB 2
BB 3 BB 4 BB 5
BB 6
![Page 66: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/66.jpg)
SSA Form
param_rp_i r0, liti16(0) param_rp_i r1, liti16(1) mul_i r0, r0, r0 add_i r0, r0, r1 return_i r0
![Page 67: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/67.jpg)
SSA Form
param_rp_i r0(1), liti16(0) param_rp_i r1, liti16(1) mul_i r0, r0, r0 add_i r0, r0, r1 return_i r0
![Page 68: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/68.jpg)
SSA Form
param_rp_i r0(1), liti16(0) param_rp_i r1(1), liti16(1) mul_i r0, r0, r0 add_i r0, r0, r1 return_i r0
![Page 69: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/69.jpg)
SSA Form
param_rp_i r0(1), liti16(0) param_rp_i r1(1), liti16(1) mul_i r0(2), r0(1), r0(1) add_i r0, r0, r1 return_i r0
![Page 70: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/70.jpg)
SSA Form
param_rp_i r0(1), liti16(0) param_rp_i r1(1), liti16(1) mul_i r0(2), r0(1), r0(1) add_i r0(3), r0(2), r1(1) return_i r0
![Page 71: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/71.jpg)
SSA Form
param_rp_i r0(1), liti16(0) param_rp_i r1(1), liti16(1) mul_i r0(2), r0(1), r0(1) add_i r0(3), r0(2), r1(1) return_i r0(3)
![Page 72: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/72.jpg)
SSA Form
param_rp_i r0(1), liti16(0) param_rp_i r1(1), liti16(1) mul_i r0(2), r0(1), r0(1) add_i r0(3), r0(2), r1(1) return_i r0(3)
(Plus some mechanism to deal with branches. The dominance calculation helps there.)
![Page 73: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/73.jpg)
We associate
facts
with each SSA variable
![Page 74: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/74.jpg)
But statistics aren't facts, they're just
statistics!
![Page 75: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/75.jpg)
So, we insert
guards that deoptimize if the type
isn't what was predicted
![Page 76: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/76.jpg)
Finally, we're ready to go ahead and apply lots of
optimizations!
![Page 77: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/77.jpg)
Rewrite a method lookup into a constant, because we
know the precise type
![Page 78: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/78.jpg)
Rewrite a multi-dispatch into a direct call to the
correct candidate
![Page 79: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/79.jpg)
Rewrite a call to the general code into a call to the
applicable specialization
![Page 80: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/80.jpg)
For small callees, inline the callee's code into that of
the caller
![Page 81: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/81.jpg)
Eliminate duplicate type checks that are already proven by existing facts
![Page 82: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/82.jpg)
Eliminate guards when we can do a proof that its condition will be met
![Page 83: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/83.jpg)
Eliminate conditionals when we can prove which
way they will go
![Page 84: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/84.jpg)
Rewrite attribute access into simple, unchecked,
pointer dereferences
![Page 85: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/85.jpg)
We also recently got
escape analysis
![Page 86: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/86.jpg)
Replace object allocations with a register per attribute
Eliminate, sink, or defer the
object allocation
Do type proofs that look into objects eliminate more guards!
![Page 87: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/87.jpg)
BB 1
BB 2
BB 3 BB 4
BB 5
BB 6
Get an optimized graph…
BB 1
BB 2
BB 4
BB 5
BB 6
![Page 88: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/88.jpg)
…and generate optimized code
BB 1
BB 2
BB 4
BB 5
BB 6
Quickened bytecode
![Page 89: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/89.jpg)
…and, on x64, machine code
BB 1
BB 2
BB 4
BB 5
BB 6
Machine code
![Page 90: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/90.jpg)
The results of various benchmarks
![Page 91: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/91.jpg)
Disclaimer
There's lies, statistics, and benchmarks
Some of these numbers rely on EA-
based optimizations not yet available in a default build
![Page 92: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/92.jpg)
No tricks
I tried to write the kind of code a typical programmer would write,
not use every Perl 6 trick I know to squeeze out more speed.
![Page 93: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/93.jpg)
Compared to the Christmas release, today's Rakudo and MoarVM are
much faster!
![Page 94: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/94.jpg)
Benchmark Xmas Today Improvement Read a million lines (UTF-8) 3.217 0.508 6.33
Array reading and addition 10.214 0.875 11.67
Hash reading 17.357 0.862 20.14
Hash store 40.134 2.247 17.86
Complex 11.092 0.695 15.96
Short-lived point object 21.174 0.369 57.38
Parse 10,000 docker files 23.964 6.145 3.9
Million native calls 4.727 0.898 5.26
![Page 95: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/95.jpg)
But what about compared to Perl 5,
Python, or Ruby?
Not a competition to see which is fastest, but rather to see if Perl 6 is competitive.
![Page 96: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/96.jpg)
Some results are already looking fairly decent…
![Page 97: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/97.jpg)
class Point { has $.x; has $.y; } my $total = 0; for ^1_000_000 { my $p = Point.new(x => 2, y => 3); $total = $total + $p.x + $p.y; } say $total;
Basic object operations on a short-lived object
![Page 98: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/98.jpg)
Basic object operations on a short-lived object
Language Time Perl 6 is... Perl 6 0.385 - Perl 5 0.955 2.48x faster Python 0.351 1.10x slower Ruby 0.191 2.02x slower
![Page 99: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/99.jpg)
Basic object operations on a short-lived object
Language Time Perl 6 is... Perl 6 0.385 - Perl 5 0.955 2.48x faster Python 0.351 1.10x slower Ruby 0.191 2.02x slower
Non-Perls use positional parameters in the
constructor…
![Page 100: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/100.jpg)
Basic object operations on a short-lived object
Language Time Perl 6 is... Perl 6 0.385 - Perl 5 0.955 2.48x faster Python 0.351 1.10x slower Ruby 0.191 2.02x slower
…so we need to EA away the temporary hash to
compete with them
![Page 101: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/101.jpg)
Read a million lines of UTF-8 (checking it) and count the chars
my $fh = open "longfile"; my $chars = 0; for $fh.lines { $chars = $chars + .chars } $fh.close; say $chars
![Page 102: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/102.jpg)
Read a million lines of UTF-8 (checking it) and count the chars
Language Time Perl 6 is... Perl 6 0.509 - Perl 5 0.977 1.92x faster Python 2.207 4.34x faster Ruby 0.412 1.24x slower
![Page 103: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/103.jpg)
Read a million lines of UTF-8 (checking it) and count the chars
Language Time Perl 6 is... Perl 6 0.509 - Perl 5 0.977 1.92x faster Python 2.207 4.34x faster Ruby 0.412 1.24x slower
Perl 6, unlike the others, has grapheme-level strings.
![Page 104: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/104.jpg)
Integer math (allowing use of Perl 6 native int)
sub gcd(int $a is copy, int $b is copy) { while $b ≠ 0 { my int $t = $b; $b = $a % $b; $a = $t; } $a } for ^2_000_000 { die "oops" unless gcd(40, 30) == 10; }
![Page 105: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/105.jpg)
Integer math (allowing use of Perl 6 native int)
Language Time Perl 6 is... Perl 6 0.664 - Perl 5 0.884 1.33x faster Python 0.406 1.63x slower
Ruby 2.69 4.05x faster
![Page 106: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/106.jpg)
Integer math (allowing use of Perl 6 native int)
Language Time Perl 6 is... Perl 6 0.664 - Perl 5 0.884 1.33x faster Python 0.406 1.63x slower
Ruby 2.69 4.05x faster
With JIT, we should really sweep the floor with this
one. Alas, not yet.
![Page 107: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/107.jpg)
Some simple operations using complex numbers
my $total-re = 0e0; for ^2_000_000 { my $x = 5 + 2i; my $y = 10 + 3i; my $z = $x * $x + $y; $total-re = $total-re + $z.re } say $total-re;
![Page 108: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/108.jpg)
Some simple operations using complex numbers
Language Time Perl 6 is... Perl 6 0.175 - Perl 5 40.1 229x faster Python 1.16 6.61x faster Ruby 1.52 8.68x faster
![Page 109: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/109.jpg)
Some simple operations using complex numbers
Language Time Perl 6 is... Perl 6 0.175 - Perl 5 40.1 229x faster Python 1.16 6.61x faster Ruby 1.52 8.68x faster
Hmmm. Obtained using Math::Complex. It's built-in
for other languages.
![Page 110: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/110.jpg)
Some simple operations using complex numbers
Language Time Perl 6 is... Perl 6 0.175 - Perl 5 40.1 229x faster Python 1.16 6.61x faster Ruby 1.52 8.68x faster
EA allows us to totally eliminate the temporary
Complex objects
![Page 111: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/111.jpg)
Really need to do better at arrays and hashes…
![Page 112: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/112.jpg)
Reading from an array, plus basic integer math
my @arr = 1,2,3; my $total = 0; for ^10_000_000 { $total += @arr[1] + @arr[2]; } say $total;
![Page 113: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/113.jpg)
Reading from an array, plus basic integer math
Language Time Perl 6 is... Perl 6 0.886 - Perl 5 0.514 1.72x slower
Python 1.00 1.13x faster Ruby 0.509 1.74x slower
![Page 114: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/114.jpg)
Lots of assignments into a dynamically allocated array
for ^10_000 { my @arr; for ^1_000 { @arr[$_] = 42; } }
![Page 115: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/115.jpg)
Lots of assignments into a dynamically allocated array
Language Time Perl 6 is... Perl 6 0.734 - Perl 5 0.527 1.40x slower Python 0.624 1.18x slower Ruby 0.505 1.46x slower
![Page 116: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/116.jpg)
Lots of assignments into a dynamically allocated array
Language Time Perl 6 is... Perl 6 0.734 - Perl 5 0.527 1.40x slower Python 0.624 1.18x slower Ruby 0.505 1.46x slower
Every array slot is a Scalar, which we have to
allocate.
![Page 117: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/117.jpg)
Lots of assignments into a dynamically allocated array
Language Time Perl 6 is... Perl 6 0.734 - Perl 5 0.527 1.40x slower Python 0.624 1.18x slower Ruby 0.505 1.46x slower
Plus, arrays may be lazy, which creates a little extra
overhead too (for now).
![Page 118: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/118.jpg)
Reading values from a hash and basic integer math
my %h = a => 10, b => 12; my $total = 0; for ^10_000_000 { $total = $total + %h<a> + %h<b>; }
![Page 119: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/119.jpg)
Reading values from a hash and basic integer math
Language Time Perl 6 is... Perl 6 0.886 - Perl 5 0.787 1.12x slower Python 1.15 1.30x faster Ruby 0.597 1.48x slower
![Page 120: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/120.jpg)
Set up lots of hashes with keys obtained from an array
my @keys = 'a'..'z'; for ^500_000 { my %h; for @keys { %h{$_} = 42; } }
![Page 121: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/121.jpg)
Set up lots of hashes with keys obtained from an array
Language Time Perl 6 is... Perl 6 2.30 - Perl 5 1.65 1.35x slower Python 0.837 2.66x slower
Ruby 2.64 1.18x faster
![Page 122: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/122.jpg)
Set up lots of hashes with keys obtained from an array
Language Time Perl 6 is... Perl 6 2.30 - Perl 5 1.65 1.35x slower Python 0.837 2.66x slower
Ruby 2.64 1.18x faster
The Perls certainly are doing hash randomization -
but who else is?
![Page 123: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/123.jpg)
Set up lots of hashes with keys obtained from an array
Language Time Perl 6 is... Perl 6 2.30 - Perl 5 1.65 1.35x slower Python 0.837 2.66x slower
Ruby 2.64 1.18x faster
Perl 6 is, as with arrays, also doing a Scalar
allocation per element
![Page 124: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/124.jpg)
And then some things really need work…
![Page 125: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/125.jpg)
Startup time - important for scripting - is still unimpressive
Language Time Perl 6 is... Perl 6 0.093 - Perl 5 0.0047 19.9x slower
Python 0.011 8.40x slower
Ruby 0.038 2.47x slower
![Page 126: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/126.jpg)
And please, let's not talk about regex performance…
![Page 127: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/127.jpg)
…oh well, OK, if we must…
![Page 128: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/128.jpg)
my $i = 0; for (1..10_000_000) { $i++ if "boo" =~ /^b/ } say $i;
my $i = 0; for ^10_000_000 { $i++ if "boo" ~~ /^b/ } say $i;
Perl 5
Perl 6
![Page 129: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/129.jpg)
my $i = 0; for (1..10_000_000) { $i++ if "boo" =~ /^b/ } say $i;
my $i = 0; for ^10_000_000 { $i++ if "boo" ~~ /^b/ } say $i;
Perl 5
Perl 6
1.60s
38.7s (24x slower)
![Page 130: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/130.jpg)
Rakudo doesn't yet know how to avoid using the regex engine for
simple things - but Perl 5 seems to be really rather good at that.
![Page 131: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/131.jpg)
So what if we manually avoid it in Perl 6, to see what we might be
able to achieve?
![Page 132: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/132.jpg)
my $i = 0; for (1..10_000_000) { $i++ if "boo" =~ /^b/ } say $i;
my $i = 0; for ^10_000_000 { $i++ if "boo".starts-with('b') } say $i;
Perl 5
Perl 6, using starts-with
![Page 133: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/133.jpg)
my $i = 0; for (1..10_000_000) { $i++ if "boo" =~ /^b/ } say $i;
my $i = 0; for ^10_000_000 { $i++ if "boo".starts-with('b') } say $i;
Perl 5
Perl 6, using starts-with
0.700 (2.2x faster)
1.60s
![Page 134: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/134.jpg)
But still…even the case where we do hit the regex engine (or use grammars) needs to be faster.
![Page 135: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/135.jpg)
The consequences for those writing Perl 6
programs today
![Page 136: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/136.jpg)
Inlining means that calling an accessor is about as cheap as
accessing an attribute
And both of those are cheaper than using a hash instead of an object
![Page 137: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/137.jpg)
Similarly, small subs and methods (and private methods) can be
inlined too, so don't worry much over using those
![Page 138: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/138.jpg)
Avoid regexes when a simple method - like starts-with or contains - will do the job
![Page 139: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/139.jpg)
Some constructs are not yet well optimized. There's usually more
than one way to do things, so - on hot path code - experiment with
some other ways.
![Page 140: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/140.jpg)
Slow things today include…
Destructuring (and signature unpacks) Multi-dispatch with where clauses
Flattening into argument lists Multi-dimensional arrays
(But if you're reading this in 2020 or later, check these
are still true, because things improve regularly. )
![Page 141: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/141.jpg)
Assignment into an array or hash copies into the target
Binding, carefully used, can turn
O(n) into O(1)
![Page 142: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/142.jpg)
Some modules are notably faster than others, so consider those too
Recently, got a roughly 5x speedup
by switching YAML module
![Page 143: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/143.jpg)
And, of course, Perl 6 parallelism support can be a great "get out of
jail free" card
![Page 144: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/144.jpg)
The plans for further performance
improvements
![Page 145: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/145.jpg)
Well, obviously…
Optimize away use of regexes where they aren't needed
And make the regex and grammar
implementation fast anyway
![Page 146: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/146.jpg)
More EA
Current focus is getting the latest round of work into user's hands
Beyond that, make EA understand loops, and able to scalar replace
arrays and hashes
![Page 147: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/147.jpg)
Speed up array/hash
Performance parity is within reach, largely by squeezing more waste
out of the generated code
![Page 148: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/148.jpg)
Speed up array/hash
To be notably faster than Perl 5 and friends, we need to do more
Can delay or even avoid Scalar
allocation - if we can better convey when we only need an r-value.
That's a tricky problem.
![Page 149: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/149.jpg)
Region JIT?
Currently, MoarVM is a method JIT with aggressive inlining
But our statistics model means we
could do region JIT, and it'd probably be a win for us
![Page 150: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/150.jpg)
Keep working at it
There's no shortcut to maturity
Need to continue analyzing things that are slow, understanding why,
and finding solutions
![Page 151: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/151.jpg)
The ultimate goal here, is that performance joins
with the many other reasons that one might
choose to use Perl 6
![Page 152: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/152.jpg)
It's hard work. It's challenging.
![Page 153: Perl 6 Performance Update - Jonathan Worthington · 2019-08-17 · Perl 6 Performance Update Jonathan Worthington | Edument . The challenge of running Perl 6 fast The optimizations](https://reader030.fdocuments.us/reader030/viewer/2022041021/5ed0b4780725b354e0667d2f/html5/thumbnails/153.jpg)
It's hard work. It's challenging.
But it's in our grasp.