Benchmarking MooseX::Method::Signatures v.s. plain subs on Hailo
I'd heard that MooseX::Method::Signatures incurred a substantial runtime performance hit. I wanted to test if that was true so I tried converting Hailo to use it. The result: Yes, it's slow:
Here's how long it takes (real time) to train Hailo with a new brain, the command is:
time hailo --brain megahal.brn --train /tmp/fortune.trn --no-progress
The results:
- Without MX::MS: 11.37s
- With MX::MS: 52.09s
- With MX::MS + no type checking: 52.84s
Interestingly If I don't do any type checking, I.e. just:
method foo ($one, $two) { ... }
Instead of just:
method foo (Str $one, Str $two) { ... }
MX::MS doesn't become any faster, even though it looks like it also has to check that the argument list is of a certain length and that none of the arguments are undef
.
These tests can be reproduced by going to hailo's repository on github and checking out either the master, mx-ms and mx-ms-no-validate branches. Check out version f9786122a08 of master for the most accurate comparison.
Note that Hailo makes a lot of method calls so it's a somewhat biased comparison compared some other programs, but in the future I'm going to be careful before I use some of the more sugary MooseX modules.
Also it's unfair to compare 100% sugar everywhere with 0% sugar everywhere. those 12s would probably go to 13s instead of 50s if I had used MX::MS for all but the most hot code in the application, although in the case of Hailo hot code is a pretty large part of the application compared to some others.
There are also modules like Method::Signatures::Simple which have a much smaller performance hit but give you the sugar, but not the type checking.
It's pretty clear though that to get sugar + type checking + speed we're going to have to hack up perl's guts to support that in some way.
I think a better benchmark would be to compare MX::MS to MooseX::Params::Validate.
I actually wrote a small benchmark script for that purpose. What I found was that when type checks passed, MX::PV was about 3-4x as fast as MX::MS, for the same type constraints. In the case where the type checks failed, it was less than 2x the speed.
I need to write this up in my blog some time.
You could be intersted in this: https://blogs.perl.org/users/komarov/2010/01/undeclarepl-moosexdeclare---moose.html