develooper Front page | perl.perl5.porters | Postings from January 2014

[perl #121077] [PATCH] Optimise 'my $x; my $y' into 'my ($x, $y)'

Thread Previous | Thread Next
Matthew Horsfall
January 24, 2014 23:31
[perl #121077] [PATCH] Optimise 'my $x; my $y' into 'my ($x, $y)'
Message ID:
# New Ticket Created by  Matthew Horsfall 
# Please include the string:  [perl #121077]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
generated with the help of perlbug 1.39 running under perl 5.14.2.

[Please describe your issue here]

First pass at optimisation for 'my $x; my $y;' -> 'my ($x, $y)'.

This changes padop -> nextate -> padop -> nexstate into a padrange -> nextstate,
which allows further padrange optimisations to occur.


  sub { my $x; my $y; return 1; }

Up to speed with:

  sub { my ($x, $y); return 1; }

For comparison, here's vanilla blead:

  $ blead/bin/perl5.19.9 -MO=Concise,-exec -e 'my $x; my %y; my @z; print "hi"'
  1  <0> enter
  2  <;> nextstate(main 1 -e:1) v:{
  3  <0> padsv[$x:1,4] vM/LVINTRO
  4  <;> nextstate(main 2 -e:1) v:{
  5  <0> padhv[%y:2,4] vM/LVINTRO
  6  <;> nextstate(main 3 -e:1) v:{
  7  <0> padav[@z:3,4] vM/LVINTRO
  8  <;> nextstate(main 4 -e:1) v:{
  9  <0> pushmark s
  a  <$> const(PV "hi") s
  b  <@> print vK
  c  <@> leave[1 ref] vKP/REFC
  -e syntax OK

Here's with the patch:

  $ padopperl/bin/perl5.19.9 -MO=Concise,-exec -e 'my $x; my %y; my
@z; print "hi"'
  1  <0> enter
  2  <;> nextstate(main 1 -e:1) v:{
  3  <0> padrange[$x:1,4; %y:2,4; @z:3,4] v/LVINTRO,3
  4  <;> nextstate(main 4 -e:1) v:{
  5  <0> pushmark s
  6  <$> const(PV "hi") s
  7  <@> print vK
  8  <@> leave[1 ref] vKP/REFC
  -e syntax OK

And here's vanilla blead with 'my ($x, %y, @z); print "hi"':

  $ blead/bin/perl5.19.9 -MO=Concise,-exec -e 'my ($x, %y, @z); print "hi"'
  1  <0> enter
  2  <;> nextstate(main 1 -e:1) v:{
  3  <0> padrange[$x:1,2; %y:1,2; @z:1,2] vM/LVINTRO,3
  4  <;> nextstate(main 2 -e:1) v:{
  5  <0> pushmark s
  6  <$> const(PV "hi") s
  7  <@> print vK
  8  <@> leave[1 ref] vKP/REFC
  -e syntax OK

There's two differences here:

  The ranges of the padops [$x:1,4 ...] vs [$x1,2 ...], though I'm not
sure that matters.

  The flag 'M' (OPf_MOD) on vanilla blead's padrange doesn't show up
on my patched version.
  I'm not sure if I should be setting this here, how to detect when to
set it, etc...

Attached are a few files I used to benchmark the changes, and I'm
mostly happy with the results.


  --- blead.txt   2014-01-24 09:06:47.853365045 -0500
  +++ padops.txt  2014-01-24 09:06:54.749364968 -0500
  @@ -1,51 +1,51 @@
   mhorsfall@tworivers:~/perl-1$ time ./perl -Ilib ~/
   Benchmark: timing 40000000 iterations of multiple, single...
  -  multiple:  2 wallclock secs ( 2.20 usr +  0.00 sys =  2.20 CPU) @
18181818.18/s (n=40000000)
  -    single:  2 wallclock secs ( 1.01 usr +  0.00 sys =  1.01 CPU) @
39603960.40/s (n=40000000)
  +  multiple:  1 wallclock secs ( 1.14 usr +  0.00 sys =  1.14 CPU) @
35087719.30/s (n=40000000)
  +    single:  1 wallclock secs ( 1.18 usr +  0.00 sys =  1.18 CPU) @
33898305.08/s (n=40000000)

  -real    0m25.807s
  -user    0m25.748s
  -sys     0m0.004s
  +real    0m22.227s
  +user    0m22.144s
  +sys     0m0.028s

"my $a; my $b; return 1;" (multiple) goes from 18,181,818/s to
35,087,719/s, and overall run time
of the tests goes from 25.8s to 22.s.

What I find concerning is that "my ($a, $b); return 1;" appears to
perform slightly worse; even though
the runtime of the entire test goes down 1 second.

I'm not sure if the optimisation is affecting's ability
to judge or this is actually
slowing down the latter case some how.

If this is accepted in some form, I'll write the appropriate tests for


-- Matthew Horsfall (alh)

[Please do not change anything below this line]
Site configuration information for perl 5.14.2:

Configured by Debian Project at Mon Mar 18 19:16:26 UTC 2013.

Summary of my perl5 (revision 5 version 14 subversion 2) configuration:

    osname=linux, osvers=2.6.42-37-generic,
    uname='linux batsu 2.6.42-37-generic #58-ubuntu smp thu jan 24
15:28:10 utc 2013 x86_64 x86_64 x86_64 gnulinux '
    config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN
-Dcccdlflags=-fPIC -Darchname=x86_64-linux-gnu -Dprefix=/usr
-Dprivlib=/usr/share/perl/5.14 -Darchlib=/usr/lib/perl/5.14
-Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5
-Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local
-Dsitearch=/usr/local/lib/perl/5.14.2 -Dman1dir=/usr/share/man/man1
-Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1
-Dsiteman3dir=/usr/local/man/man3 -Duse64bitint -Dman1ext=1
-Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh
-Ud_ualarm -Uusesfio -Uusenm -Ui_libutil -DDEBUGGING=-g -Doptimize=-O2
-Duseshrplib -des'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN
-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include
    optimize='-O2 -g',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fno-strict-aliasing
-pipe -fstack-protector -I/usr/local/include'
    ccversion='', gccversion='4.6.3', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t',
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
    libpth=/usr/local/lib /lib/x86_64-linux-gnu /lib/../lib
/usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib /usr/lib
    libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
    perllibs=-ldl -lm -lpthread -lc -lcrypt
    libc=, so=so, useshrplib=true,
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -g -L/usr/local/lib

Locally applied patches:

@INC for perl 5.14.2:

Environment for perl 5.14.2:
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About