cache the full method map where possible
[gitmo/Class-MOP.git] / lib / Class / MOP / Mixin / HasMethods.pm
index d3ceaa8..d08d273 100644 (file)
@@ -127,9 +127,10 @@ sub get_method {
 sub _get_maybe_raw_method {
     my ( $self, $method_name ) = @_;
 
-    my $method_map = $self->_method_map;
-    my $map_entry  = $method_map->{$method_name};
-    my $code       = $self->get_package_symbol(
+    my $map_entry = $self->_method_map->{$method_name};
+    return $map_entry if blessed $map_entry;
+
+    my $code = $self->get_package_symbol(
         {
             name  => $method_name,
             sigil => '&',
@@ -137,13 +138,6 @@ sub _get_maybe_raw_method {
         }
     );
 
-    # The !$code case seems to happen in some weird cases where methods
-    # modifiers are added via roles or some other such bizareness. Honestly, I
-    # don't totally understand this, but returning the entry works, and keeps
-    # various MX modules from blowing up. - DR
-    return $map_entry
-        if blessed $map_entry && ( !$code || $map_entry->body == $code );
-
     unless ($map_entry) {
         return unless $code && $self->_code_is_mine($code);
     }
@@ -173,17 +167,7 @@ sub remove_method {
 sub get_method_list {
     my $self = shift;
 
-    my $namespace = $self->namespace;
-
-    # Constants may show up as some sort of non-GLOB reference in the
-    # namespace hash ref, depending on the Perl version.
-    return grep {
-        defined $namespace->{$_}
-            && ( ref( \$namespace->{$_} ) ne 'GLOB'
-            || *{ $namespace->{$_} }{CODE} )
-            && $self->has_method($_)
-        }
-        keys %{$namespace};
+    return keys %{ $self->_full_method_map };
 }
 
 # This should probably be what get_method_list actually does, instead of just
@@ -192,15 +176,7 @@ sub get_method_list {
 sub _get_local_methods {
     my $self = shift;
 
-    my $namespace = $self->namespace;
-
-    return map { $self->get_method($_) }
-        grep {
-        defined $namespace->{$_}
-            && ( ref $namespace->{$_}
-            || *{ $namespace->{$_} }{CODE} )
-        }
-        keys %{$namespace};
+    return values %{ $self->_full_method_map };
 }
 
 sub _restore_metamethods_from {
@@ -225,6 +201,21 @@ sub update_package_cache_flag {
     $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
 }
 
+sub _full_method_map {
+    my $self = shift;
+
+    my $pkg_gen = Class::MOP::check_package_cache_flag($self->name);
+
+    if (($self->{_package_cache_flag_full} || -1) != $pkg_gen) {
+        # forcibly reify all method map entries
+        $self->get_method($_)
+            for $self->list_all_package_symbols('CODE');
+        $self->{_package_cache_flag_full} = $pkg_gen;
+    }
+
+    return $self->_method_map;
+}
+
 1;
 
 __END__