From: Tokuhiro Matsuno Date: Mon, 2 Mar 2009 01:36:44 +0000 (+0000) Subject: micro optimization for method modifiers. X-Git-Tag: 0.19~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=4f5b44a07ba66f7a5997e6531d38a5fbf2cb3593;hp=abfdffe0146e788b3b808398fb075231163c3948 micro optimization for method modifiers. do not eval everytime! benchmark result is here: Rate orig fastest orig 4950/s -- -92% fastest 62500/s 1162% -- --- diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index b9179eb..07b54e7 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -187,25 +187,36 @@ sub attribute_metaclass { "Mouse::Meta::Class" } sub _install_modifier { my ( $self, $into, $type, $name, $code ) = @_; - if (eval "require Class::Method::Modifiers::Fast; 1") { - Class::Method::Modifiers::Fast::_install_modifier( - $into, - $type, - $name, - $code - ); - } - elsif (eval "require Class::Method::Modifiers; 1") { - Class::Method::Modifiers::_install_modifier( - $into, - $type, - $name, - $code - ); - } - else { - Carp::croak("Method modifiers require the use of Class::Method::Modifiers. Please install it from CPAN and file a bug report with this application."); + + # which is modifer class available? + my $modifier_class = do { + if (eval "require Class::Method::Modifiers::Fast; 1") { + 'Class::Method::Modifiers::Fast'; + } elsif (eval "require Class::Method::Modifiers; 1") { + 'Class::Method::Modifiers'; + } else { + Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application."); + } + }; + my $modifier = $modifier_class->can('_install_modifier'); + + # replace this method itself :) + { + no strict 'refs'; + no warnings 'redefine'; + *{__PACKAGE__ . '::_install_modifier'} = sub { + my ( $self, $into, $type, $name, $code ) = @_; + $modifier->( + $into, + $type, + $name, + $code + ); + }; } + + # call me. for first time. + $self->_install_modifier( $into, $type, $name, $code ); } sub add_before_method_modifier {