Moose Immutable
[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
11BEGIN {
12 subtype 'Foo' => as 'Object' => where { blessed($_) && $_->isa('Foo') };
13
14 coerce 'Foo'
15 => from 'ArrayRef'
16 => via { Foo->new(@{$_}) };
17}
18
19{
20 package Foo;
21 use Moose;
22}
23
24{
25 package Foo::Normal;
26 use Moose;
27
28 has 'default' => (is => 'rw', default => 10);
29 has 'default_sub' => (is => 'rw', default => sub { [] });
30 has 'lazy' => (is => 'rw', default => 10, lazy => 1);
31 has 'required' => (is => 'rw', required => 1);
32 has 'weak_ref' => (is => 'rw', weak_ref => 1);
33 has 'type_constraint' => (is => 'rw', isa => 'Foo');
34 has 'coercion' => (is => 'rw', isa => 'Foo', coerce => 1);
35
36}
37
38{
39 package Foo::Immutable;
40 use Moose;
41
42 has 'default' => (is => 'rw', default => 10);
43 has 'default_sub' => (is => 'rw', default => sub { [] });
44 has 'lazy' => (is => 'rw', default => 10, lazy => 1);
45 has 'required' => (is => 'rw', required => 1);
46 has 'weak_ref' => (is => 'rw', weak_ref => 1);
47 has 'type_constraint' => (is => 'rw', isa => 'Foo');
48 has 'coercion' => (is => 'rw', isa => 'Foo', coerce => 1);
49
50 sub BUILD {
51 # ...
52 }
53
54 Foo::Immutable->meta->make_immutable(debug => 1);
55}
56
57#__END__
58
59my $foo = Foo->new;
60
61cmpthese(500,
62 {
63 'normal' => sub {
64 Foo::Normal->new(
65 required => 'BAR',
66 type_constraint => $foo,
67 #coercion => [],
68 );
69 },
70 'immutable' => sub {
71 Foo::Immutable->new(
72 required => 'BAR',
73 type_constraint => $foo,
74 #coercion => [],
75 );
76 },
77 }
78);