develooper Front page | perl.perl5.porters | Postings from July 2011

Make static or typed method calls lookup faster by 4%

Reini Urban
July 29, 2011 00:58
Make static or typed method calls lookup faster by 4%
Message ID:
I fixed Doug MacEachern old method call optimize patch and added more

The old patch was posted at
and explained in greater detail here:

Class->subname(args...) =>

const PV => "Class"
    args ...
method_named => PV "subname"

can be optimized if &Class::subname is defined. Otherwise we'd need to
ensure a compile-time
const @ISA with some sort of new package attribute. :locked for subs
and packages is free.
Only a package attribute make sense here really.

The optimization would get rid of the run-time stash lookup for the
string "Class", use the
string as first arg to entersub, and the method_named op can be
replaced by gv => &Class::subname.

const PV => "Class"
    args ...
gv => *Class::subname

Static or typed method calls almost as fast as function calls, and no
ops have to be added.

I independently came to the same optimization idea, and then found Doug's patch.
New patch:
The benchmark is at

My current patch works, but should not be the final version, because I
want to find
some perl-space methods to define "locked packages", such as Moose
immutable classes.
There's also a -Dv debugging line it.

Currently only Class->subname(args...) can be optimized if
&Class::subname is defined.
And as a second case typed method calls:
  my Class $obj; $obj->subname(args);

Currently all strict::import, strict::unimport, warnings... methods
are translated.

If method is not defined in Class, but in a child class, we need to ensure that
1. @Class::ISA is readonly ("const"), and
2. all child classes until the found class will not add such a method
at runtime. "locked package" or const stash. No stash key may be

Internally I think the best way to store this is the SVf_READONLY flag
for the @ISA, and the stashes.
I never saw READONLY stashes though.

Externally I proposed several alternatives in my blog entry.

package NAME :const; our @ISA :const = (); is my current favorite,
because the :const attribute fits
best to the optional semantics for "locked" packages, and is closest to C.

Readonly::Array  our @ISA => (); is a bit too verbose for me.
Same for
use constant our @ISA => (); but this looks better.

Readonly::Hash  %Class::; or package Class :const; should protect from
adding keys, new methods.
Reini Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About