From: Stevan Little Date: Tue, 27 May 2008 20:08:21 +0000 (+0000) Subject: Some package speedups X-Git-Tag: 0_64~49 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ae234dc6e9fff0fbe75a405a4de51fd08281e003;p=gitmo%2FClass-MOP.git Some package speedups --- diff --git a/Changes b/Changes index 0f94ac5..880fcfa 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,18 @@ Revision history for Perl extension Class-MOP. +0.57 + * Class::MOP::Class + - made get_method_map use list_all_package_symbols + instead of manually grabbing each symbol + + * Class::MOP::Package + - made {get, has}_package_symbol not call + &namespace so much + - added get_all_package_symbols to fetch + a HASH of items based on a type filter + similar to list_all_package_symbols + - added tests for this + 0.56 Saturday, May 24, 2008 * Class::MOP - we now get the &check_package_cache_flag diff --git a/README b/README index c458ed8..4930280 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Class::MOP version 0.55 +Class::MOP version 0.57 =========================== See the individual module documentation for more information diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index be3b9d8..30acf21 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -16,7 +16,7 @@ use Class::MOP::Method; use Class::MOP::Immutable; BEGIN { - our $VERSION = '0.56'; + our $VERSION = '0.57'; our $AUTHORITY = 'cpan:STEVAN'; *IS_RUNNING_ON_5_10 = ($] < 5.009_005) diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index d4d391b..48dd0b6 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -11,7 +11,7 @@ use Class::MOP::Method::Wrapped; use Carp 'confess'; use Scalar::Util 'blessed', 'reftype', 'weaken'; -our $VERSION = '0.31'; +our $VERSION = '0.32'; our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Module'; @@ -312,8 +312,10 @@ sub get_method_map { my $class_name = $self->name; my $method_metaclass = $self->method_metaclass; - foreach my $symbol ($self->list_all_package_symbols('CODE')) { - my $code = $self->get_package_symbol('&' . $symbol); + my %all_code = $self->get_all_package_symbols('CODE'); + + foreach my $symbol (keys %all_code) { + my $code = $all_code{$symbol}; next if exists $map->{$symbol} && defined $map->{$symbol} && diff --git a/lib/Class/MOP/Package.pm b/lib/Class/MOP/Package.pm index dd43d6b..8d3a014 100644 --- a/lib/Class/MOP/Package.pm +++ b/lib/Class/MOP/Package.pm @@ -7,7 +7,7 @@ use warnings; use Scalar::Util 'blessed'; use Carp 'confess'; -our $VERSION = '0.08'; +our $VERSION = '0.09'; our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Object'; @@ -107,7 +107,9 @@ sub has_package_symbol { my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); - return 0 unless exists $self->namespace->{$name}; + my $namespace = $self->namespace; + + return 0 unless exists $namespace->{$name}; # FIXME: # For some really stupid reason @@ -118,15 +120,15 @@ sub has_package_symbol { # if you put \undef in your scalar # then this is broken. - if (ref($self->namespace->{$name}) eq 'SCALAR') { + if (ref($namespace->{$name}) eq 'SCALAR') { return ($type eq 'CODE' ? 1 : 0); } elsif ($type eq 'SCALAR') { - my $val = *{$self->namespace->{$name}}{$type}; + my $val = *{$namespace->{$name}}{$type}; return defined(${$val}) ? 1 : 0; } else { - defined(*{$self->namespace->{$name}}{$type}) ? 1 : 0; + defined(*{$namespace->{$name}}{$type}) ? 1 : 0; } } @@ -135,10 +137,12 @@ sub get_package_symbol { my ($name, $sigil, $type) = $self->_deconstruct_variable_name($variable); + my $namespace = $self->namespace; + $self->add_package_symbol($variable) - unless exists $self->namespace->{$name}; + unless exists $namespace->{$name}; - if (ref($self->namespace->{$name}) eq 'SCALAR') { + if (ref($namespace->{$name}) eq 'SCALAR') { if ($type eq 'CODE') { no strict 'refs'; return \&{$self->name.'::'.$name}; @@ -148,7 +152,7 @@ sub get_package_symbol { } } else { - return *{$self->namespace->{$name}}{$type}; + return *{$namespace->{$name}}{$type}; } } @@ -209,6 +213,26 @@ sub list_all_package_symbols { } keys %{$namespace}; } +sub get_all_package_symbols { + my ($self, $type_filter) = @_; + return %{$self->namespace} unless defined $type_filter; + # NOTE: + # or we can filter based on + # type (SCALAR|ARRAY|HASH|CODE) + my $namespace = $self->namespace; + no strict 'refs'; + return map { + $_ => (ref($namespace->{$_}) eq 'SCALAR' + ? ($type_filter eq 'CODE' ? \&{$self->name . '::' . $_} : undef) + : *{$namespace->{$_}}{$type_filter}) + } grep { + (ref($namespace->{$_}) + ? (ref($namespace->{$_}) eq 'SCALAR' && $type_filter eq 'CODE') + : (ref(\$namespace->{$_}) eq 'GLOB' + && defined(*{$namespace->{$_}}{$type_filter}))); + } keys %{$namespace}; +} + 1; __END__ @@ -284,6 +308,11 @@ the package. By passing a C<$type_filter>, you can limit the list to only those which match the filter (either SCALAR, ARRAY, HASH or CODE). +=item B + +Works exactly like C but returns a HASH of +name => thing mapping instead of just an ARRAY of names. + =back =head1 AUTHORS diff --git a/t/010_self_introspection.t b/t/010_self_introspection.t index d321c63..e440c99 100644 --- a/t/010_self_introspection.t +++ b/t/010_self_introspection.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 199; +use Test::More tests => 201; use Test::Exception; BEGIN { @@ -35,7 +35,7 @@ my @class_mop_package_methods = qw( namespace add_package_symbol get_package_symbol has_package_symbol remove_package_symbol - list_all_package_symbols remove_package_glob + list_all_package_symbols get_all_package_symbols remove_package_glob _deconstruct_variable_name ); diff --git a/t/080_meta_package.t b/t/080_meta_package.t index a300af9..4a6f2f5 100644 --- a/t/080_meta_package.t +++ b/t/080_meta_package.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 88; +use Test::More tests => 92; use Test::Exception; BEGIN { @@ -226,6 +226,31 @@ is(Foo->meta->get_package_symbol('@foo'), $ARRAY, '... got the right values for ok(defined(*{"Foo::foo"}{ARRAY}), '... the @foo slot has NOT been removed'); } +# get_all_package_symbols + +{ + my %syms = Foo->meta->get_all_package_symbols; + + is_deeply( + [ sort keys %syms ], + [ sort Foo->meta->list_all_package_symbols ], + '... the fetched symbols are the same as the listed ones' + ); +} + +{ + my %syms = Foo->meta->get_all_package_symbols('CODE'); + + is_deeply( + [ sort keys %syms ], + [ sort Foo->meta->list_all_package_symbols('CODE') ], + '... the fetched symbols are the same as the listed ones' + ); + + foreach my $symbol (keys %syms) { + is($syms{$symbol}, Foo->meta->get_package_symbol('&' . $symbol), '... got the right symbol'); + } +} # check some errors