From f74481a7387de05f95b8ddfd690d8136fb12048f Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Tue, 24 Jan 2012 10:50:39 +0000 Subject: cache attributes per class, makes thinks faster MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hopefully… --- .../MultiValued/UglySerializationHelperRole.pm | 58 +++++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/lib/Data/MultiValued/UglySerializationHelperRole.pm b/lib/Data/MultiValued/UglySerializationHelperRole.pm index ebf783a..db5a4d8 100644 --- a/lib/Data/MultiValued/UglySerializationHelperRole.pm +++ b/lib/Data/MultiValued/UglySerializationHelperRole.pm @@ -60,10 +60,8 @@ sub new_in_place { my $self = bless $hash,$class; - for my $attr ($class->meta->get_all_attributes) { - if ($attr->does('Data::MultiValued::AttributeTrait')) { - $attr->_rebless_slot($self); - } + for my $attr (@{$class->_dmv_multi_attrs}) { + $attr->_rebless_slot($self); } return $self; } @@ -85,20 +83,54 @@ sub as_hash { my ($self) = @_; my %ret = %$self; - for my $attr ($self->meta->get_all_attributes) { - if ($attr->does('Data::MultiValued::AttributeTrait')) { - my $st = $attr->_as_hash($self); - if ($st) { - $ret{$attr->full_storage_slot} = $st; - } - else { - delete $ret{$attr->full_storage_slot}; - } + for my $attr (@{$self->_dmv_multi_attrs}) { + my $st = $attr->_as_hash($self); + if ($st) { + $ret{$attr->full_storage_slot} = $st; + } + else { + delete $ret{$attr->full_storage_slot}; } } return \%ret; } +{ +my ( + %all_attrs_for_class, + %tagged_attrs_for_class, + %ranged_attrs_for_class, + %multi_attrs_for_class, +); + +sub _dmv_all_attrs { + my ($class) = @_;$class=ref($class)||$class; + + return $all_attrs_for_class{$class} //= [$class->meta->get_all_attributes]; +} +sub _dmv_tagged_attrs { + my ($class) = @_;$class=ref($class)||$class; + + return $tagged_attrs_for_class{$class} //= + [ grep { $_->does('Data::MultiValued::AttributeTrait::Tags') } + @{$class->_dmv_all_attrs} ]; +} +sub _dmv_ranged_attrs { + my ($class) = @_;$class=ref($class)||$class; + + return $ranged_attrs_for_class{$class} //= + [ grep { $_->does('Data::MultiValued::AttributeTrait::Ranges') } + @{$class->_dmv_all_attrs} ]; +} +sub _dmv_multi_attrs { + my ($class) = @_;$class=ref($class)||$class; + + return $multi_attrs_for_class{$class} //= + [ grep { $_->does('Data::MultiValued::AttributeTrait') } + @{$class->_dmv_all_attrs} ]; +} +} + =head1 FINAL WARNING my $obj_clone = My::Class->new_in_place($obj->as_hash); -- cgit v1.2.3