Don't copy "use autodie" in every module

You pay a constant price in your app's starting performance for each time you use autodie;.

Here's a quick benchmark:

$ time perl -E 'say "package X$_; use autodie qw(:all);" for 1..100;' | perl

real	0m1.482s
user	0m1.431s
sys	0m0.047s

Compare with Moose:

$ time perl -E 'say "package X$_; use Moose;" for 1..100;' | perl

real	0m0.343s
user	0m0.328s
sys	0m0.016s

It doesn't get much better without qw(:all):

$ time perl -E 'say "package X$_; use autodie;" for 1..100;' | perl

real	0m1.212s
user	0m1.169s
sys	0m0.047s

But it gets significantly better if you import only a small number of functions:

$ time perl -E 'say "package X$_; use autodie qw(open close);" for 1..100;' | perl

real	0m0.175s
user	0m0.166s
sys	0m0.011s

Basically, you pay for each function you import, once per function instance, in each module, again and again. That's different from, for example, Moose, where 99% of importing performance hit is on first use, when perl compiles all the code, and then each subsequent import() is almost free. Due to this, if your app has many modules, autodie can easily become the biggest bottleneck in its starting performance.

So, it's a bad idea to add use autodie qw(:all) thoughtlessly to your boilerplate, in addition to use strict; use warnings; use 5.0xx;. If you do need to use autodie, it might be a good idea to explicitly list all functions you want to replace.

PS: I don't know why it should be that way. I know autodie 2.18 does more caching and is significantly faster than previous versions, but it still doesn't cache much, apparently.

PPS: This post was brought to you by questhub, as usual :)

Leave a comment

About Vyacheslav Matyukhin

user-pic I wrote Ubic. I worked at Yandex for many years, and now i'm building my own startup questhub.io (formerly PlayPerl). I'm also working on Flux, streaming data processing framework. CPAN ID: MMCLERIC.