From: gfx Date: Tue, 1 Sep 2009 04:03:08 +0000 (+0900) Subject: Make meta instance's attributes lazy, which reduces a thouthand calls of subroutines X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=302a7de8ab95d6046f56e7709d0d291cd9af35e3;p=gitmo%2FClass-MOP.git Make meta instance's attributes lazy, which reduces a thouthand calls of subroutines --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index de32b1a..de6b177 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -389,7 +389,6 @@ sub _create_meta_instance { my $instance = $self->instance_metaclass->new( associated_metaclass => $self, - attributes => [ $self->get_all_attributes() ], ); $self->add_meta_instance_dependencies() diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index 1482554..c205d27 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -4,7 +4,7 @@ package Class::MOP::Instance; use strict; use warnings; -use Scalar::Util 'weaken', 'blessed'; +use Scalar::Util 'weaken'; our $VERSION = '0.93'; $VERSION = eval $VERSION; @@ -13,30 +13,18 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Object'; sub BUILDARGS { - my ($class, @args) = @_; - - if ( @args == 1 ) { - unshift @args, "associated_metaclass"; - } elsif ( @args >= 2 && blessed($args[0]) && $args[0]->isa("Class::MOP::Class") ) { - # compat mode - my ( $meta, @attrs ) = @args; - @args = ( associated_metaclass => $meta, attributes => \@attrs ); - } + my $class = shift; - my %options = @args; - # FIXME lazy_build - $options{slots} ||= [ map { $_->slots } @{ $options{attributes} || [] } ]; - $options{slot_hash} = { map { $_ => undef } @{ $options{slots} } }; # FIXME lazy_build + unshift @_, 'associated_metaclass' if @_ % 2; - return \%options; + return { @_ }; } sub new { - my $class = shift; - my $options = $class->BUILDARGS(@_); + my $class = shift; + my $params = $class->BUILDARGS(@_); - # FIXME replace with a proper constructor - my $instance = $class->_new(%$options); + my $instance = $class->_new($params); # FIXME weak_ref => 1, weaken($instance->{'associated_metaclass'}); @@ -84,19 +72,23 @@ sub clone_instance { # operations on meta instance -sub get_all_slots { +sub get_all_attributes { my $self = shift; - return @{$self->{'slots'}}; + # lazy build + return @{ $self->{attributes} ||= [ $self->associated_metaclass->get_all_attributes ] }; } -sub get_all_attributes { +sub get_all_slots { my $self = shift; - return @{$self->{attributes}}; + # lazy build + return @{ $self->{slots} ||= [ map{ $_->slots } $self->get_all_attributes ] }; } sub is_valid_slot { my ($self, $slot_name) = @_; - exists $self->{'slot_hash'}->{$slot_name}; + # lazy build + $self->{slot_hash} ||= { map{ $_ => undef } $self->get_all_slots }; + return exists $self->{slot_hash}->{$slot_name}; } # operations on created instances