small fixes & optimizations in Meta::Instance
Yuval Kogman [Sun, 10 Aug 2008 19:31:27 +0000 (19:31 +0000)]
lib/Class/MOP.pm
lib/Class/MOP/Instance.pm

index 1d0ea46..6c82116 100644 (file)
@@ -699,19 +699,36 @@ Class::MOP::Method::Constructor->meta->add_method('new' => sub {
 # included for completeness
 
 Class::MOP::Instance->meta->add_attribute(
-    Class::MOP::Attribute->new('associated_metaclass')
+    Class::MOP::Attribute->new('associated_metaclass',
+        reader   => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
+    ),
 );
 
 Class::MOP::Instance->meta->add_attribute(
-    Class::MOP::Attribute->new('attributes')
+    Class::MOP::Attribute->new('_class_name',
+        init_arg => undef,
+        reader   => { _class_name => \&Class::MOP::Instance::_class_name },
+        #lazy     => 1, # not yet supported by Class::MOP but out our version does it anyway
+        #default  => sub { $_[0]->associated_metaclass->name },
+    ),
+);
+
+Class::MOP::Instance->meta->add_attribute(
+    Class::MOP::Attribute->new('attributes',
+        reader   => { attributes => \&Class::MOP::Instance::attributes },
+    ),
 );
 
 Class::MOP::Instance->meta->add_attribute(
-    Class::MOP::Attribute->new('slots')
+    Class::MOP::Attribute->new('slots',
+        reader   => { slots => \&Class::MOP::Instance::slots },
+    ),
 );
 
 Class::MOP::Instance->meta->add_attribute(
-    Class::MOP::Attribute->new('slot_hash')
+    Class::MOP::Attribute->new('slot_hash',
+        reader   => { slot_hash => \&Class::MOP::Instance::slot_hash },
+    ),
 );
 
 
index 8b70fdd..1875668 100644 (file)
@@ -47,9 +47,9 @@ sub new {
         # assumption,.. but you can
         # never tell <:)
         'associated_metaclass' => $options->{associated_metaclass},
-        'attributes' => $options->{attributes},
-        'slots'      => $options->{slots},
-        'slot_hash'  => $options->{slot_hash},
+        'attributes'           => $options->{attributes},
+        'slots'                => $options->{slots},
+        'slot_hash'            => $options->{slot_hash},
     } => $class;
 
     # FIXME weak_ref => 1,
@@ -58,7 +58,9 @@ sub new {
     return $instance;
 }
 
-sub associated_metaclass { (shift)->{'associated_metaclass'} }
+sub _class_name { $_[0]->{_class_name} ||= $_[0]->associated_metaclass->name }
+
+sub associated_metaclass { $_[0]{'associated_metaclass'} }
 
 sub create_instance {
     my $self = shift;
@@ -67,7 +69,7 @@ sub create_instance {
 
 sub bless_instance_structure {
     my ($self, $instance_structure) = @_;
-    bless $instance_structure, $self->associated_metaclass->name;
+    bless $instance_structure, $self->_class_name;
 }
 
 sub clone_instance {