6 Moose::Cookbook::Recipe7 - Making Moose fast with immutable
13 has 'x' => (isa => 'Int', is => 'ro');
14 has 'y' => (isa => 'Int', is => 'rw');
16 __PACKAGE__->meta->make_immutable;
20 The Moose metaclass API provides a method C<make_immutable()>. At a
21 high level, this calling this method does two things to your
22 class. One, it makes it faster. In particular, object construction and
23 accessors are effectively "inlined" in your class, and no longer go
24 through the meta-object system.
26 Second, you can no longer make changes via the metaclass API such as
27 adding attributes. In practice, this won't be a problem, as you don't
28 usually need to do this at runtime after first loading the class.
30 =head2 Immutabilization and C<new()>
32 If you override C<new()> in your class, then the immutabilization code
33 will not be able to provide an optimized constructor for your
34 class. Instead, consider providing a C<BUILD()> method. You can
35 probably do the same thing in a C<BUILD()> method.
37 Alternately, if you really need to provide a different C<new()>, you
38 can also provide your own immutabilization method.
40 Discussing this is beyond the scope of this recipe, however.
44 We strongly recommend you make your classes immutable. It makes your
45 code much faster, basically for free. This will be especially
46 noticeable when creating many objects or calling accessors frequently.
50 Dave Rolsky E<lt>autarch@urth.orgE<gt>
52 =head1 COPYRIGHT AND LICENSE
54 Copyright 2006-2008 by Infinity Interactive, Inc.
56 L<http://www.iinteractive.com>
58 This library is free software; you can redistribute it and/or modify
59 it under the same terms as Perl itself.