add the C<parents> method to TypeConstraint, which is just an alias to C<parent>...
[gitmo/Moose.git] / benchmarks / type_constraints.pl
index 7b1469b..8af49ef 100644 (file)
@@ -5,6 +5,14 @@ use warnings;
 
 use Benchmark qw[cmpthese];
 
+=pod
+
+This benchmark compares the overhead of a 
+auto-created type constraint vs. none at 
+all vs. a custom-created type.
+
+=cut
+
 {
     package Foo;
     use Moose;
@@ -12,21 +20,32 @@ use Benchmark qw[cmpthese];
     
     has 'baz' => (is => 'rw');
     has 'bar' => (is => 'rw', isa => 'Foo');
-    has 'boo' => (is => 'rw', isa => type 'CustomFoo' => where { blessed($_) && $_->isa('Foo') });
+}
+
+{
+    package Bar;
+    
+    sub new { bless {} => __PACKAGE__ }
+    sub bar { 
+        my $self = shift;
+        $self->{bar} = shift if @_;
+        $self->{bar};
+    }
 }
 
 my $foo = Foo->new;
+my $bar = Bar->new;
 
 cmpthese(200_000, 
     {
+        'hand coded' => sub {
+            $bar->bar($bar);
+        },
         'w/out_constraint' => sub {
             $foo->baz($foo);
         },
         'w_constraint' => sub {
             $foo->bar($foo);            
-        },
-        'w_custom_constraint' => sub {
-            $foo->boo($foo);            
         },        
     }
 );