I recently downloaded Moose. Experimentally, I rewrote an existing module in Moose. It seems to be convenient way to avoid writing lots of repetitive code. I ran the tests of the module, and I noticed it was a bit delayed. I profiled the code with -d:DProf and it seems that just including the line
no Moose;
in the code increases the running time by about 0.25 seconds (on my computer). Is this typical? Am I doing something wrong, did I misinstall it, or should we really expect this much delay?
Yes, there’s a bit of a penalty to using Moose. However, it’s only a startup penalty, not at runtime; if you wrote everything properly, then things will be quite fast at runtime.
Did you also include this line:
in all your classes when you
no Moose;? Calling this method will make it (runtime) faster (at the expense of startup time). In particular, object construction and destruction are effectively “inlined” in your class, and no longer invoke the meta API. It is strongly recommended that you make your classes immutable. It makes your code much faster, with a small compile-time cost. This will be especially noticeable when creating many objects.12
However, sometimes this cost is still too much.
If you’re using Moose inside a script, or in some other way where the compilation time is a significant fraction of your overall usage time, try doing
s/Moose/Moo/g— if you don’t use MooseX modules, you can likely switch to Moo, whose goal is to be faster (at startup) while retaining 90% of the flexibility of Moose.Since you are working with a web application, have you considered using Plack/PSGI?
1From the docs of make_immutable, in Moose::Cookbook::Basics::Recipe7
2See also Stevan Little’s article: Why make_immutable is recommended for Moose classes