Sunday, January 31, 2010

Diet Moose

[Spanish source]

Do you think that Moose is too heavy for your applications?

Compiling Moose objects can take considerable time during application startup, this could give the impression that programs that use it are slow, however the compilation only happens when loading the program, and depending on the application Moose may not be as heavy as it looks.

One example is a Catalyst application (in versions newer than 5.8), when it starts it should compile all objects made with Moose and you may notice the difference with previous versions, but as Catalyst is to be run for days or months startup time generally does not matter.

If the application you want to develop is a command that executes a task and terminates quickly then Moose is perhaps too heavy for you, especially when the application may be executed repeatedly via other commands like xargs(1) or find(1).

For such cases where the application's starting time will not be well amortized with execution, there is a solution in the CPAN: Mouse.

Mouse is a highly optimized Moose replacement that allows the vast majority of the features of Moose, but is much lighter because it is developed in XS (ie C) and omits some features to speedup execution.

According to the manual page of Mouse, Moose only fails 1% of the tests, which makes Mouse very upward compatible with Moose, but the test suite runs 400% faster with Mouse and in my experience I can not tell the difference between using Mouse and things like Class::Accessor::Fast, and although the latter may be somewhat faster (something I have not formally tested), having available an OOP system like Moose is definitely worth it.

Using Mouse is very simple, you just need to replace Moose by Mouse, everything else is the same as with Moose.


  1. There's a little more to the difference than only failing 1% of the Moose tests.

    Since Mouse eliminates the MOP entirely, it is not extensible in the same way as Moose. This means that you can't use any MooseX modules with Mouse.

    Some MooseX modules have been reimplemented as MouseX modules, but I don't know if this is really possible for every MooseX, and I suspect it's certainly harder for many of them.

  2. Yeah, it's not that Mouse fails 1% of Moose's tests, it's that Moose fails 1% of Mouse's tests. That's a big difference. It means Mouse is compatible, whereas your phrasing makes it seem that Mouse is complete.

    Also I recommend using Any::Moose instead of Mouse directly, since that ultimately defers the decision of which to use to the user.

  3. Dave: Since most of the code does not use MOP, I think Mouse is a very valid alternative for small applications and tools, because I still get a lot of features.

    Sartak: you're right I have edited the paragraph to make it clear that Mouse is just upwards compatible with Moose, and I was willing to talk about Any::Moose in my next article.