From: Stevan Little Date: Fri, 3 Nov 2006 22:51:42 +0000 (+0000) Subject: no more inlining, and its not actually any slower, at least not from naive benchmarks X-Git-Tag: 0_16~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=946289d1aeb348f55dfc8993fc57a3dcb4b76bc0;p=gitmo%2FMoose.git no more inlining, and its not actually any slower, at least not from naive benchmarks --- diff --git a/Changes b/Changes index ef0d9fb..3f1ffe0 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,10 @@ Revision history for Perl extension Moose 0.15 + ++ NOTE ++ + This version of Moose *must* have Class::MOP 0.36 in order + to work correctly. A number of small internal tweaks have + been made in order to be compatible with that release. * Moose::Util::TypeConstraints - added &unimport so that you can clean out @@ -11,17 +15,43 @@ Revision history for Perl extension Moose - fixed minor issue which occasionally comes up during global destruction (thanks omega) - + - moved Moose::Meta::Method::Overriden into + its own file. + + * Moose::Meta::Role + - moved Moose::Meta::Role::Method into + its own file. + * Moose::Meta::Attribute - changed how we do type checks so that we reduce the overall cost, but still retain correctness. + *** API CHANGE *** + - moved accessor generation methods to + Moose::Meta::Method::Accessor to + conform to the API changes from + Class::MOP 0.36 * Moose::Meta::TypeConstraint - changed how constraints are compiled so that we do less recursion and more iteration. This makes the type check faster :) + - moved Moose::Meta::TypeConstraint::Union + into its own file + + * Moose::Meta::Method::Accessor + - created this from methods formerly found in + Moose::Meta::Attribute + + * Moose::Meta::Role::Method + - moved this from Moose::Meta::Role + + * Moose::Meta::Method::Overriden + - moved this from Moose::Meta::Class + + * Moose::Meta::TypeConstraint::Union + - moved this from Moose::Meta::TypeConstraint 0.14 Mon. Oct. 9, 2006 diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index b9bcb6d..95cbe11 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -229,21 +229,82 @@ sub initialize_instance_slot { ## Slot management -#sub set_value { -# my ($self, $instance, $value) = @_; -#} -# -#sub get_value { -# my ($self, $instance) = @_; -#} -# -#sub has_value { -# my ($self, $instance) = @_; -#} -# -#sub clear_value { -# my ($self, $instance) = @_; -#} +sub set_value { + my ($self, $instance, $value) = @_; + + my $attr_name = $self->name; + + if ($self->is_required) { + defined($value) + || confess "Attribute ($attr_name) is required, so cannot be set to undef"; + } + + if ($self->has_type_constraint) { + + my $type_constraint = $self->type_constraint; + + if ($self->should_coerce) { + $value = $type_constraint->coerce($value); + } + defined($type_constraint->_compiled_type_constraint->($value)) + || confess "Attribute ($attr_name) does not pass the type constraint (" + . $type_constraint->name . ") with " . (defined($value) ? ("'" . $value . "'") : "undef") + if defined($value); + } + + my $meta_instance = Class::MOP::Class->initialize(blessed($instance)) + ->get_meta_instance; + + $meta_instance->set_slot_value($instance, $attr_name, $value); + + if (ref $value && $self->is_weak_ref) { + $meta_instance->weaken_slot_value($instance, $attr_name); + } + + if ($self->has_trigger) { + $self->trigger->($instance, $value, $self); + } +} + +sub get_value { + my ($self, $instance) = @_; + + if ($self->is_lazy) { + unless ($self->has_value($instance)) { + if ($self->has_default) { + my $default = $self->default($instance); + $self->set_value($instance, $default); + } + else { + $self->set_value($instance, undef); + } + } + } + + if ($self->should_auto_deref) { + + my $type_constraint = $self->type_constraint; + + if ($type_constraint->is_a_type_of('ArrayRef')) { + my $rv = $self->SUPER::get_value($instance); + return unless defined $rv; + return wantarray ? @{ $rv } : $rv; + } + elsif ($type_constraint->is_a_type_of('HashRef')) { + my $rv = $self->SUPER::get_value($instance); + return unless defined $rv; + return wantarray ? %{ $rv } : $rv; + } + else { + confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'"; + } + + } + else { + + return $self->SUPER::get_value($instance); + } +} ## installing accessors @@ -398,6 +459,10 @@ will behave just as L does. =item B +=item B + +=item B + =back =head2 Additional Moose features diff --git a/lib/Moose/Meta/Method/Accessor.pm b/lib/Moose/Meta/Method/Accessor.pm index e353af2..0891d6b 100644 --- a/lib/Moose/Meta/Method/Accessor.pm +++ b/lib/Moose/Meta/Method/Accessor.pm @@ -13,9 +13,9 @@ our $VERSION = '0.01'; use base 'Moose::Meta::Method', 'Class::MOP::Method::Accessor'; -## generators +## Inline method generators -sub generate_accessor_method { +sub generate_accessor_method_inline { my $self = shift; my $attr = $self->associated_attribute; my $attr_name = $attr->name; @@ -47,7 +47,7 @@ sub generate_accessor_method { return $sub; } -sub generate_writer_method { +sub generate_writer_method_inline { my $self = shift; my $attr = $self->associated_attribute; my $attr_name = $attr->name; @@ -73,7 +73,7 @@ sub generate_writer_method { return $sub; } -sub generate_reader_method { +sub generate_reader_method_inline { my $self = shift; my $attr = $self->associated_attribute; my $attr_name = $attr->name; @@ -88,26 +88,6 @@ sub generate_reader_method { return $sub; } -#sub generate_predicate_method { -# my $self = shift; -# my $attr = $self->associated_attribute; -# my $attr_name = $attr->name; -#} -# -#sub generate_clearer_method { -# my $self = shift; -# my $attr = $self->associated_attribute; -# my $attr_name = $attr->name; -#} - -## Inline methods - -*generate_accessor_method_inline = \&generate_accessor_method; -*generate_reader_method_inline = \&generate_reader_method; -*generate_writer_method_inline = \&generate_writer_method; -#*generate_predicate_method_inline = \&generate_predicate_method; -#*generate_clearer_method_inline = \&generate_clearer_method; - ## ... private helpers sub _inline_check_constraint { diff --git a/t/300_immutable_moose.t b/t/300_immutable_moose.t index eb126bd..ff82f81 100644 --- a/t/300_immutable_moose.t +++ b/t/300_immutable_moose.t @@ -3,9 +3,15 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 1; use Test::Exception; BEGIN { use_ok('Moose'); } + +=pod + +Nothing here yet, but soon :) + +=cut