8 has foo => (is => 'rw');
11 package MooseImmutable;
13 has foo => (is => 'rw');
14 __PACKAGE__->meta->make_immutable();
17 package MooseImmutable::NoConstructor;
19 has foo => (is => 'rw');
20 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
23 package ClassAccessorFast;
26 use base 'Class::Accessor::Fast';
27 __PACKAGE__->mk_accessors(qw(foo));
30 use Benchmark qw(cmpthese);
31 use Benchmark ':hireswallclock';
33 my $moose = PlainMoose->new;
34 my $moose_immut = MooseImmutable->new;
35 my $moose_immut_no_const = MooseImmutable::NoConstructor->new;
36 my $caf = ClassAccessorFast->new;
38 my $acc_rounds = 100_000;
39 my $ins_rounds = 100_000;
42 cmpthese($acc_rounds, {
43 Moose => sub { $moose->foo(23) },
44 MooseImmutable => sub { $moose_immut->foo(23) },
45 MooseImmutableNoConstructor => sub { $moose_immut_no_const->foo(23) },
46 ClassAccessorFast => sub { $caf->foo(23) },
50 cmpthese($acc_rounds, {
51 Moose => sub { $moose->foo },
52 MooseImmutable => sub { $moose_immut->foo },
53 MooseImmutableNoConstructor => sub { $moose_immut_no_const->foo },
54 ClassAccessorFast => sub { $caf->foo },
57 my (@moose, @moose_immut, @moose_immut_no_const, @caf_stall);
59 cmpthese($ins_rounds, {
60 Moose => sub { push @moose, PlainMoose->new(foo => 23) },
61 MooseImmutable => sub { push @moose_immut, MooseImmutable->new(foo => 23) },
62 MooseImmutableNoConstructor => sub { push @moose_immut_no_const, MooseImmutable::NoConstructor->new(foo => 23) },
63 ClassAccessorFast => sub { push @caf_stall, ClassAccessorFast->new({foo => 23}) },
66 my ( $moose_idx, $moose_immut_idx, $moose_immut_no_const_idx, $caf_idx ) = ( 0, 0, 0, 0 );
67 print "\nDESTRUCTION\n";
68 cmpthese($ins_rounds, {
70 $moose[$moose_idx] = undef;
73 MooseImmutable => sub {
74 $moose_immut[$moose_immut_idx] = undef;
77 MooseImmutableNoConstructor => sub {
78 $moose_immut_no_const[$moose_immut_no_const_idx] = undef;
79 $moose_immut_no_const_idx++;
81 ClassAccessorFast => sub {
82 $caf_stall[$caf_idx] = undef;