s/metaclass/__PACKAGE__->meta/
[gitmo/Moose.git] / benchmarks / immutable.pl
CommitLineData
5cf3dbcf 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Scalar::Util 'blessed';
7use Benchmark qw[cmpthese];
8
9use Moose::Util::TypeConstraints;
10
5cf3dbcf 11{
12 package Foo;
13 use Moose;
8ecb1fa0 14 Foo->meta->make_immutable(debug => 0);
5cf3dbcf 15}
16
8ecb1fa0 17coerce 'Foo'
18 => from 'ArrayRef'
19 => via { Foo->new(@{$_}) };
20
5cf3dbcf 21{
22 package Foo::Normal;
23 use Moose;
24
25 has 'default' => (is => 'rw', default => 10);
26 has 'default_sub' => (is => 'rw', default => sub { [] });
27 has 'lazy' => (is => 'rw', default => 10, lazy => 1);
28 has 'required' => (is => 'rw', required => 1);
29 has 'weak_ref' => (is => 'rw', weak_ref => 1);
30 has 'type_constraint' => (is => 'rw', isa => 'Foo');
31 has 'coercion' => (is => 'rw', isa => 'Foo', coerce => 1);
32
1f779926 33 package Bar::Normal;
34 use Moose;
35
36 extends 'Foo::Normal';
37
38 has 'default_w_type_constraint' => (
39 is => 'rw',
40 isa => 'Int',
41 default => 10,
42 );
5cf3dbcf 43}
44
45{
46 package Foo::Immutable;
47 use Moose;
48
49 has 'default' => (is => 'rw', default => 10);
50 has 'default_sub' => (is => 'rw', default => sub { [] });
51 has 'lazy' => (is => 'rw', default => 10, lazy => 1);
52 has 'required' => (is => 'rw', required => 1);
53 has 'weak_ref' => (is => 'rw', weak_ref => 1);
54 has 'type_constraint' => (is => 'rw', isa => 'Foo');
55 has 'coercion' => (is => 'rw', isa => 'Foo', coerce => 1);
56
cd51de1f 57 #sub BUILD {
58 # # ...
59 #}
5cf3dbcf 60
1f779926 61 Foo::Immutable->meta->make_immutable(debug => 0);
62
63 package Bar::Immutable;
64 use Moose;
65
66 extends 'Foo::Immutable';
67
68 has 'default_w_type_constraint' => (
69 is => 'rw',
70 isa => 'Int',
71 default => 10,
72 );
73
74 Bar::Immutable->meta->make_immutable(debug => 0);
5cf3dbcf 75}
76
77#__END__
78
79my $foo = Foo->new;
80
1f779926 81cmpthese(10_000,
5cf3dbcf 82 {
83 'normal' => sub {
84 Foo::Normal->new(
85 required => 'BAR',
86 type_constraint => $foo,
8ecb1fa0 87 coercion => [],
88 weak_ref => {},
5cf3dbcf 89 );
90 },
91 'immutable' => sub {
92 Foo::Immutable->new(
93 required => 'BAR',
94 type_constraint => $foo,
8ecb1fa0 95 coercion => [],
96 weak_ref => {},
5cf3dbcf 97 );
98 },
99 }
100);