scope makes slower!mouse's constructer is faster than moose with this commit :)
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
index 4b5e099..37e2505 100644 (file)
@@ -7,13 +7,14 @@ sub generate_constructor_method_inline {
 
     my @attrs = $meta->compute_all_applicable_attributes;
     my $buildall = $class->_generate_BUILDALL($meta);
-    my $buildargs = $class->_generate_BUILDARGS();
+    my $buildargs = $class->_generate_BUILDARGS($meta);
     my $processattrs = $class->_generate_processattrs($meta, \@attrs);
+    my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs;
 
     my $code = <<"...";
     sub {
         my \$class = shift;
-        my \$args = $buildargs;
+        $buildargs;
         my \$instance = bless {}, \$class;
         $processattrs;
         $buildall;
@@ -22,6 +23,7 @@ sub generate_constructor_method_inline {
 ...
 
     local $@;
+    # warn $code;
     my $res = eval $code;
     die $@ if $@;
     $res;
@@ -42,28 +44,37 @@ sub _generate_processattrs {
             $code .= "if (exists \$args->{'$from'}) {\n";
 
             if ($attr->should_coerce && $attr->type_constraint) {
-                $code .= "my \$value = Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});";
+                $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
             }
             else {
-                $code .= "my \$value = \$args->{'$from'};";
+                $code .= "my \$value = \$args->{'$from'};\n";
             }
 
             if ($attr->has_type_constraint) {
-                $code .= "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
-                $code .= "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
+                if ($attr->type_constraint->{_compiled_type_constraint}) {
+                    $code .= "unless (\$compiled_constraints[$index](\$value)) {";
+                } else {
+                    $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
+                }
+                $code .= "
+                        \$attrs[$index]->verify_type_constraint_error(
+                            '$key', \$_, \$attrs[$index]->type_constraint
+                        )
+                    }
+                ";
             }
 
-            $code .= "\$instance->{'$key'} = \$value;";
+            $code .= "\$instance->{'$key'} = \$value;\n";
 
             if ($attr->is_weak_ref) {
-                $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
+                $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
             }
 
             if ($attr->has_trigger) {
-                $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );";
+                $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value );\n";
             }
 
-            $code .= "} else {";
+            $code .= "\n} else {\n";
         }
 
         if ($attr->has_default || $attr->has_builder) {
@@ -74,7 +85,7 @@ sub _generate_processattrs {
                 $code .= "my \$value = ";
 
                 if ($attr->should_coerce && $attr->type_constraint) {
-                    $code .= "Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, ";
+                    $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
                 }
 
                     if ($attr->has_builder) {
@@ -94,29 +105,32 @@ sub _generate_processattrs {
                     }
 
                 if ($attr->should_coerce) {
-                    $code .= ");";
+                    $code .= ");\n";
                 }
                 else {
-                    $code .= ";";
+                    $code .= ";\n";
                 }
 
                 if ($attr->has_type_constraint) {
-                    $code .= "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
-                    $code .= "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
+                    $code .= "{
+                        unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
+                            \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
+                        }
+                    }";
                 }
 
-                $code .= "\$instance->{'$key'} = \$value;";
+                $code .= "\$instance->{'$key'} = \$value;\n";
 
                 if ($attr->is_weak_ref) {
-                    $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
+                    $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
                 }
             }
         }
         elsif ($attr->is_required) {
-            $code .= qq{Carp::confess("Attribute ($key) is required");};
+            $code .= "Carp::confess('Attribute ($key) is required');";
         }
 
-        $code .= "}" if defined $attr->init_arg;
+        $code .= "}\n" if defined $attr->init_arg;
 
         push @res, $code;
     }
@@ -125,22 +139,23 @@ sub _generate_processattrs {
 }
 
 sub _generate_BUILDARGS {
-    <<'...';
-    do {
+    my $self = shift;
+    my $meta = shift;
+
+    if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
+        return 'my $args = $class->BUILDARGS(@_)';
+    }
+
+    return <<'...';
+        my $args;
         if ( scalar @_ == 1 ) {
-            if ( defined $_[0] ) {
-                ( ref( $_[0] ) eq 'HASH' )
+            ( ref( $_[0] ) eq 'HASH' )
                 || Carp::confess "Single parameters to new() must be a HASH ref";
-                +{ %{ $_[0] } };
-            }
-            else {
-                +{};
-            }
+            $args = +{ %{ $_[0] } };
         }
         else {
-            +{@_};
+            $args = +{@_};
         }
-    };
 ...
 }
 
@@ -152,6 +167,7 @@ sub _generate_BUILDALL {
     push @code, q{no strict 'refs';};
     push @code, q{no warnings 'once';};
     no strict 'refs';
+    no warnings 'once';
     for my $klass ($meta->linearized_isa) {
         if (*{ $klass . '::BUILD' }{CODE}) {
             push  @code, qq{${klass}::BUILD(\$instance, \$args);};