oop intermediate

How to make a Moose class immutable for performance in Perl?

Question

How to make a Moose class immutable for performance in Perl?

Making a Moose class immutable is a well-known technique to boost the performance of object construction and destruction in Perl's Moose object system. By default, Moose performs meta-object protocol introspection on each object instantiation, which adds overhead. Calling __PACKAGE__->meta->make_immutable finalizes the class's meta-object, enabling internal optimizations such as inlining constructor and destructor methods. This can make object creation several times faster for classes with many attributes.

Why You Should Make Your Moose Class Immutable

  • Speed: Immutable classes have optimized constructors (new) and destructors (DESTROY), reducing the overhead of attribute accessors.
  • Stability: Freezing the meta-object protocol reduces runtime changes, helping catch accidental meta changes.
  • Best Practice: It’s widely recommended for production code that uses Moose classes.

Making classes immutable is especially impactful if you create many object instances or require high performance in tight loops.

How To Make a Moose Class Immutable

Simply add __PACKAGE__->meta->make_immutable; near the end of your package (usually just before 1;). This call finalizes the class's meta-object and optimizes methods accordingly.

Example: Moose Class with Immutability

use strict;
use warnings;
use Moose;

# Define a simple Moose class
package Person;
use Moose;

has 'name' => (
    is  => 'rw',
    isa => 'Str',
);

has 'age' => (
    is  => 'rw',
    isa => 'Int',
);

# Make the class immutable to boost performance
__PACKAGE__->meta->make_immutable;

package main;

# Create a new Person object
my $person = Person->new(name => 'Alice', age => 30);

print "Name: ", $person->name, "\n";
print "Age: ", $person->age, "\n";

Explaining Key Concepts

  • __PACKAGE__ is a special Perl token representing the current package name, here Person.
  • ->meta returns the class’s Moose meta-object.
  • make_immutable finalizes and optimizes the class's meta-object.
  • Without immutability, Moose dynamically processes metaclasses for each object creation, causing overhead.

Common Pitfalls and Gotchas

  • Order matters: Call make_immutable after defining all attributes, methods, roles, and overrides but before the package returns true (i.e., before the last 1;).
  • Immutable classes cannot be modified: After calling make_immutable, you cannot add attributes or roles to the class. Add everything first.
  • Immutable is optional but recommended: Moose classes work fine without immutability, but performance will be worse.
  • Compatibility: make_immutable has been available since early Moose versions and works consistently in Perl 5.10+ environments.

Summary

To optimize Moose class performance, especially when creating many instances, add __PACKAGE__->meta->make_immutable near the end of your class declaration. This finalizes the meta-object protocol, allowing Moose to inline constructors and destructors and eliminate extra overhead.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 200ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Name: Alice
Age: 30
STDERR
(empty)

Was this helpful?

Related Questions