Always make_immutable (unless you have a very good reason not to)

One hears that __PACKAGE__->meta->make_immutable is a must all your Moose classes. ( edit, ht ribusushi: never immutate your moo classes, it'll inflate them into Moose classes automatically thus defeating the point). Here's a stark reminder of why.

I was doing some benchmarks on a web application that I've been moving from mod_perl to plack. Here's the performance of 350 requests forking off in batches of 7 before I remembered to make_immutable:

$ time perl 002-hammer_lightly.t 
# Total number of requests = 350
real    1m11.095s
user    0m3.003s
sys 0m1.336s

And after:

$ time perl 002-hammer_lightly.t 
# Total number of requests = 350

real    0m17.075s
user    0m2.202s
sys 0m1.160s

One line of code, nearly 5-fold performance improvement. And despite the shim upon shim I had to make to move the thing off modperl, it seems to be two to three times faster than the modperl app (using an apples to pears comparison method anyway).

1 Comment

Note that for Moo it is not required and a nop (though you can just leave the call in when porting from Moose). :)

Leave a comment

About kd

user-pic Australian perl hacker. Lead author of the Definitive Guide to Catalyst. Dabbles in javascript, social science and statistical analysis.