- Tests for this bug. (t0m)
* Moose::Util
- - Change resolve_metaclass alias to use the new
+ - Change resolve_metaclass alias to use the new
load_first_existing_class function. This makes it a lot
simpler, and also around 5 times faster. (t0m)
- - Add caching to resolve_metatrait_alias, which gives an
- order of magnitude speedup to things which repeatedly call
- the does method. E.g. MooseX::Storage (t0m)
+ - Add caching to resolve_metaclass_alias, which gives an order
+ of magnitude speedup to things which repeatedly call the
+ Moose::Meta::Attribute->does method. E.g. MooseX::Storage
+ (t0m)
0.59 Tue October 14, 2008
* Moose
};
}
-{ my %cache;
- sub resolve_metatrait_alias {
- my ( $type, $metaclass_name ) = @_;
-
- return $cache{$type}{$metaclass_name} if $cache{$type}{$metaclass_name};
-
- my $class = resolve_metaclass_alias( @_, trait => 1 );
- $cache{$type}{$metaclass_name} = $class if $class;
-
- return $class;
- }
+sub resolve_metatrait_alias {
+ my ( $type, $metaclass_name ) = @_;
+
+ return resolve_metaclass_alias( @_, trait => 1 );
}
-sub resolve_metaclass_alias {
- my ( $type, $metaclass_name, %options ) = @_;
+{
+ my %cache;
- my $possible_full_name = 'Moose::Meta::' . $type . '::Custom::' . ( $options{trait} ? "Trait::" : "" ) . $metaclass_name;
- my $loaded_class = Class::MOP::load_first_existing_class($possible_full_name, $metaclass_name);
+ sub resolve_metaclass_alias {
+ my ( $type, $metaclass_name, %options ) = @_;
- $loaded_class->can('register_implementation')
- ? $loaded_class->register_implementation
- : $loaded_class;
+ my $cache_key = $type . q{ } . ( $options{trait} ? '-Trait' : '' );
+ return $cache{$cache_key}{$metaclass_name}
+ if $cache{$cache_key}{$metaclass_name};
+
+ my $possible_full_name
+ = 'Moose::Meta::'
+ . $type
+ . '::Custom::'
+ . ( $options{trait} ? "Trait::" : "" )
+ . $metaclass_name;
+
+ my $loaded_class = Class::MOP::load_first_existing_class(
+ $possible_full_name,
+ $metaclass_name
+ );
+
+ return $cache{$cache_key}{$metaclass_name}
+ = $loaded_class->can('register_implementation')
+ ? $loaded_class->register_implementation
+ : $loaded_class;
+ }
}
sub add_method_modifier {
--- /dev/null
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 16;
+
+use Moose::Util qw( resolve_metaclass_alias resolve_metatrait_alias );
+
+use lib 't/lib';
+
+# Doing each test twice is intended to make sure that the caching
+# doesn't break name resolution. It doesn't actually test that
+# anything is cached.
+is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Foo' ),
+ 'Moose::Meta::Attribute::Custom::Foo',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Foo' ),
+ 'Moose::Meta::Attribute::Custom::Foo',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo second time' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Foo' ),
+ 'Moose::Meta::Attribute::Custom::Foo',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo via alias (Foo)' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Foo' ),
+ 'Moose::Meta::Attribute::Custom::Foo',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Foo via alias (Foo) a second time' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Bar' ),
+ 'My::Bar',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Bar' ),
+ 'My::Bar',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar a second time' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Bar' ),
+ 'My::Bar',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar via alias (Bar)' );
+
+is( resolve_metaclass_alias( 'Attribute', 'Bar' ),
+ 'My::Bar',
+ 'resolve_metaclass_alias finds Moose::Meta::Attribute::Custom::Bar as My::Bar via alias (Bar) a second time' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Foo' ),
+ 'Moose::Meta::Attribute::Custom::Trait::Foo',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Foo' ),
+ 'Moose::Meta::Attribute::Custom::Trait::Foo',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo second time' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Foo' ),
+ 'Moose::Meta::Attribute::Custom::Trait::Foo',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo via alias (Foo)' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Foo' ),
+ 'Moose::Meta::Attribute::Custom::Trait::Foo',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Foo via alias (Foo) a second time' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Bar' ),
+ 'My::Trait::Bar',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Moose::Meta::Attribute::Custom::Trait::Bar' ),
+ 'My::Trait::Bar',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar a second time' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Bar' ),
+ 'My::Trait::Bar',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar via alias (Bar)' );
+
+is( resolve_metatrait_alias( 'Attribute', 'Bar' ),
+ 'My::Trait::Bar',
+ 'resolve_metatrait_alias finds Moose::Meta::Attribute::Custom::Trait::Bar as My::Trait::Bar via alias (Bar) a second time' );