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
From:
Matthew Horsfall
Date:
January 24, 2014 23:31
Subject:
[perl #121077] [PATCH] Optimise 'my $x; my $y' into 'my ($x, $y)'
Message ID:
rt-4.0.18-31456-1390606284-721.121077-75-0@perl.org
# New Ticket Created by  Matthew Horsfall 
# Please include the string:  [perl #121077]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=121077 >


This is a bug report for perl from WolfSage@gmail.com,
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.

Brings:

  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.

Specifically:

  --- 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 ~/bench.pl
   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 Benchmark.pm'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
B::Concise.

Cheers,

-- Matthew Horsfall (alh)

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=low
---
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:

  Platform:
    osname=linux, osvers=2.6.42-37-generic,
archname=x86_64-linux-gnu-thread-multi
    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
-Dsitelib=/usr/local/share/perl/5.14.2
-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 -Dlibperl=libperl.so.5.14.2 -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
  Compiler:
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBIAN
-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    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',
lseeksize=8
    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, libperl=libperl.so.5.14.2
    gnulibc_version='2.15'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -O2 -g -L/usr/local/lib
-fstack-protector'

Locally applied patches:


---
@INC for perl 5.14.2:
    /etc/perl
    /usr/local/lib/perl/5.14.2
    /usr/local/share/perl/5.14.2
    /usr/lib/perl5
    /usr/share/perl5
    /usr/lib/perl/5.14
    /usr/share/perl/5.14
    /usr/local/lib/site_perl
    .

---
Environment for perl 5.14.2:
    HOME=/home/mhorsfall
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/mhorsfall/perl5/perlbrew/bin:/home/mhorsfall/bin:/home/mhorsfall/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
    PERLBREW_BASHRC_VERSION=0.66
    PERLBREW_HOME=/home/mhorsfall/.perlbrew
    PERLBREW_ROOT=/home/mhorsfall/perl5/perlbrew
    PERL_BADLANG (unset)
    SHELL=/bin/bash

Thread Previous | Thread Next


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