Skip Alien-Ditaa
[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);
d03bd989 26 has 'default_sub' => (is => 'rw', default => sub { [] });
5cf3dbcf 27 has 'lazy' => (is => 'rw', default => 10, lazy => 1);
d03bd989 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;
d03bd989 35
1f779926 36 extends 'Foo::Normal';
d03bd989 37
1f779926 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;
d03bd989 48
5cf3dbcf 49 has 'default' => (is => 'rw', default => 10);
d03bd989 50 has 'default_sub' => (is => 'rw', default => sub { [] });
5cf3dbcf 51 has 'lazy' => (is => 'rw', default => 10, lazy => 1);
d03bd989 52 has 'required' => (is => 'rw', required => 1);
53 has 'weak_ref' => (is => 'rw', weak_ref => 1);
54 has 'type_constraint' => (is => 'rw', isa => 'Foo');
5cf3dbcf 55 has 'coercion' => (is => 'rw', isa => 'Foo', coerce => 1);
d03bd989 56
cd51de1f 57 #sub BUILD {
58 # # ...
59 #}
d03bd989 60
1f779926 61 Foo::Immutable->meta->make_immutable(debug => 0);
d03bd989 62
1f779926 63 package Bar::Immutable;
64 use Moose;
d03bd989 65
66 extends 'Foo::Immutable';
67
1f779926 68 has 'default_w_type_constraint' => (
69 is => 'rw',
70 isa => 'Int',
71 default => 10,
d03bd989 72 );
73
74 Bar::Immutable->meta->make_immutable(debug => 0);
5cf3dbcf 75}
76
77#__END__
78
79my $foo = Foo->new;
80
d03bd989 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 => [],
d03bd989 96 weak_ref => {},
5cf3dbcf 97 );
98 },
99 }
100);