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

Make static or typed method calls lookup faster by 4%

From:
Reini Urban
Date:
July 29, 2011 00:58
Subject:
Make static or typed method calls lookup faster by 4%
Message ID:
CAHiT=DFcQwgmEtiYMXmWm3uJyz0GUsajAn3SmUkNoiEsMK8i4w@mail.gmail.com
I fixed Doug MacEachern old method call optimize patch and added more
documentation.
See http://blogs.perl.org/users/rurban/2011/06/how-perl-calls-subs-and-methods.html

The old patch was posted at
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-06/msg00441.html
and explained in greater detail here:
http://www.perl.com/pub/2000/06/dougpatch.html

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

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

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.

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

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: https://github.com/rurban/perl/commit/7a305aae0b335
The benchmark is at https://gist.github.com/1113238

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
added.

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



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About