Skip Menu |
Report information
Id: 132306
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: alex.jakimenko [at] gmail.com
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: [PERF] subroutine parameters, even if unused, make stuff slower ( f1($a, $, $, $, $, $) vs f2($a, $b, $c, $d, $e, $f) )
Download (untitled) / with headers
text/plain 736b
Code: sub f1($a, $, $, $, $, $) { 1 }; my $s; $s += f1($_, $_, $_, $_, $_, $_) for ^1_000_000; say now - BEGIN now Result: 0.43209886 Code: sub f2($a, $b1, $b2, $b3, $b4, $b5) { 1 }; my $s; $s += f2($_, $_, $_, $_, $_, $_) for ^1_000_000; say now - BEGIN now Result: 0.6635439 None of the params are used but still replacing them with just $ makes things run faster. The difference is measurable and you can increase the number of loops to observe it even better. This ticket is motivated by a pull request which used that observation to speed things up in rakudo: https://github.com/rakudo/rakudo/pull/1196 This is Rakudo version 2017.09-375-ga0f29e0df built on MoarVM version 2017.09.1-594-gb9d3f6da implementing Perl 6.c.
Oh, I guess it applies to methods as well.

On 2017-10-14 20:10:15, alex.jakimenko@gmail.com wrote:
Show quoted text
> Code:
> sub f1($a, $, $, $, $, $) { 1 };
> my $s;
> $s += f1($_, $_, $_, $_, $_, $_) for ^1_000_000;
> say now - BEGIN now
>
> Result:
> 0.43209886
>
>
> Code:
> sub f2($a, $b1, $b2, $b3, $b4, $b5) { 1 };
> my $s;
> $s += f2($_, $_, $_, $_, $_, $_) for ^1_000_000;
> say now - BEGIN now
>
> Result:
> 0.6635439
>
>
> None of the params are used but still replacing them with just $ makes
> things run faster.
>
> The difference is measurable and you can increase the number of loops
> to observe it even better.
>
>
> This ticket is motivated by a pull request which used that observation
> to speed things up in rakudo:
> https://github.com/rakudo/rakudo/pull/1196
>
>
> This is Rakudo version 2017.09-375-ga0f29e0df built on MoarVM version
> 2017.09.1-594-gb9d3f6da
> implementing Perl 6.c.


FWIW I made a throwaway script that looks for unused params, and there are many of these in rakudo sources. Of course, most of these cases are not in hot paths, but the overall performance benefit may be very noticeable.

There are also cases like this: https://github.com/rakudo/rakudo/blob/nom/src/core/IO/Socket/Async.pm#L313-L315
where named arg is present in the signature but does absolutely nothing (or so it seems?).

Dunno if I should post the results. There are many false-positives and I guess many cases are not caught at all, so a proper approach is needed if we really want to replace all these with $.

On 2017-10-14 20:12:49, alex.jakimenko@gmail.com wrote:
Show quoted text
> Oh, I guess it applies to methods as well.
>
> On 2017-10-14 20:10:15, alex.jakimenko@gmail.com wrote:
> > Code:
> > sub f1($a, $, $, $, $, $) { 1 };
> > my $s;
> > $s += f1($_, $_, $_, $_, $_, $_) for ^1_000_000;
> > say now - BEGIN now
> >
> > Result:
> > 0.43209886
> >
> >
> > Code:
> > sub f2($a, $b1, $b2, $b3, $b4, $b5) { 1 };
> > my $s;
> > $s += f2($_, $_, $_, $_, $_, $_) for ^1_000_000;
> > say now - BEGIN now
> >
> > Result:
> > 0.6635439
> >
> >
> > None of the params are used but still replacing them with just $ makes
> > things run faster.
> >
> > The difference is measurable and you can increase the number of loops
> > to observe it even better.
> >
> >
> > This ticket is motivated by a pull request which used that observation
> > to speed things up in rakudo:
> > https://github.com/rakudo/rakudo/pull/1196
> >
> >
> > This is Rakudo version 2017.09-375-ga0f29e0df built on MoarVM version
> > 2017.09.1-594-gb9d3f6da
> > implementing Perl 6.c.


And not only parameters, but unused variables also:
https://github.com/rakudo/rakudo/blob/ebb0521bd259e9f81e4b127527534090969f398e/src/core/native_array.pm#L1399

On 2017-10-14 20:53:03, alex.jakimenko@gmail.com wrote:
Show quoted text
> FWIW I made a throwaway script that looks for unused params, and there
> are many
> of these in rakudo sources. Of course, most of these cases are not in
> hot
> paths, but the overall performance benefit may be very noticeable.
>
> There are also cases like this:
> https://github.com/rakudo/rakudo/blob/nom/src/core/IO/Socket/Async.pm#L313-
> L315
> where named arg is present in the signature but does absolutely
> nothing (or so
> it seems?).
>
> Dunno if I should post the results. There are many false-positives and
> I guess
> many cases are not caught at all, so a proper approach is needed if we
> really
> want to replace all these with $.
>
> On 2017-10-14 20:12:49, alex.jakimenko@gmail.com wrote:
> > Oh, I guess it applies to methods as well.
> >
> > On 2017-10-14 20:10:15, alex.jakimenko@gmail.com wrote:
> > > Code:
> > > sub f1($a, $, $, $, $, $) { 1 };
> > > my $s;
> > > $s += f1($_, $_, $_, $_, $_, $_) for ^1_000_000;
> > > say now - BEGIN now
> > >
> > > Result:
> > > 0.43209886
> > >
> > >
> > > Code:
> > > sub f2($a, $b1, $b2, $b3, $b4, $b5) { 1 };
> > > my $s;
> > > $s += f2($_, $_, $_, $_, $_, $_) for ^1_000_000;
> > > say now - BEGIN now
> > >
> > > Result:
> > > 0.6635439
> > >
> > >
> > > None of the params are used but still replacing them with just $
> > > makes
> > > things run faster.
> > >
> > > The difference is measurable and you can increase the number of
> > > loops
> > > to observe it even better.
> > >
> > >
> > > This ticket is motivated by a pull request which used that
> > > observation
> > > to speed things up in rakudo:
> > > https://github.com/rakudo/rakudo/pull/1196
> > >
> > >
> > > This is Rakudo version 2017.09-375-ga0f29e0df built on MoarVM
> > > version
> > > 2017.09.1-594-gb9d3f6da
> > > implementing Perl 6.c.


RT-Send-CC: perl6-compiler [...] perl.org
On Sat, 14 Oct 2017 20:53:03 -0700, alex.jakimenko@gmail.com wrote: Show quoted text
> FWIW I made a throwaway script that looks for unused params, and there > are many > of these in rakudo sources. Of course, most of these cases are not in > hot > paths, but the overall performance benefit may be very noticeable. > > There are also cases like this: > https://github.com/rakudo/rakudo/blob/nom/src/core/IO/Socket/Async.pm#L313- > L315 > where named arg is present in the signature but does absolutely > nothing (or so > it seems?). > > Dunno if I should post the results. There are many false-positives and > I guess > many cases are not caught at all, so a proper approach is needed if we > really > want to replace all these with $.
Wouldn't a warning about unused variables be more appropriate than silently rewriting them to `$`? Their names can be introspected and used for something by they user. We actually do do that in core (sub MAIN USAGE generation). That Proc::Async line you point out actually has a bug in it and a warning would've detected it.
Yes, it would be awesome to have warnings for unused params and variables.


On 2018-01-14 12:16:08, cpan@zoffix.com wrote:
Show quoted text
> On Sat, 14 Oct 2017 20:53:03 -0700, alex.jakimenko@gmail.com wrote:
> > FWIW I made a throwaway script that looks for unused params, and
> > there
> > are many
> > of these in rakudo sources. Of course, most of these cases are not in
> > hot
> > paths, but the overall performance benefit may be very noticeable.
> >
> > There are also cases like this:
> > https://github.com/rakudo/rakudo/blob/nom/src/core/IO/Socket/Async.pm#L313-
> > L315
> > where named arg is present in the signature but does absolutely
> > nothing (or so
> > it seems?).
> >
> > Dunno if I should post the results. There are many false-positives
> > and
> > I guess
> > many cases are not caught at all, so a proper approach is needed if
> > we
> > really
> > want to replace all these with $.
>
> Wouldn't a warning about unused variables be more appropriate
> than silently rewriting them to `$`? Their names can be introspected
> and used for
> something by they user. We actually do do that in core (sub MAIN USAGE
> generation).
>
> That Proc::Async line you point out actually has a bug in it and a
> warning would've detected it.




This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org