Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PERF] Large diff between HLL and nqp performance in code example #1328

Closed
zoffixznet opened this issue Dec 22, 2017 · 1 comment
Closed

Comments

@zoffixznet
Copy link
Contributor

zoffixznet commented Dec 22, 2017

Noticed a large difference between nqp version (3s) and HLL version (42s) of this code. Filing in case there are potential optimization opportunities waiting to be discovered.

With this input saved as day_05.input file

This runs in 42s and if you change all the .AT-POS to [], it goes up to 1m3s

my int @instructions = 'day_05.input'.IO.slurp.words.map: *.Int;
my int $index = 0;
my int $stop = @instructions.elems;
my int $iterations = 0;
my int $jump;

while $index < $stop {
    $jump = @instructions.AT-POS: $index;

    $jump >= 3 ?? --@instructions.AT-POS: $index
               !! ++@instructions.AT-POS: $index;

    $index = $index + $jump;
    ++$iterations;
}

say $iterations;

While this runs in 3s:

use nqp;

nqp::stmts(
  (my $ins := nqp::getattr('day_05.input'.IO.slurp.words.map(*.Int).eager.List,
    List, '$!reified')),
  (my int $index),
  (my int $stop = nqp::elems($ins)),
  (my int $jump),
  (my int $iterations),
  nqp::while(
    nqp::islt_i($index, $stop),
    nqp::stmts(
      ($jump = nqp::atpos($ins,$index)),
      nqp::if(
        nqp::isge_i($jump,3),
        nqp::bindpos($ins,$index,nqp::sub_i(nqp::atpos($ins,$index),1)),
        nqp::bindpos($ins,$index,nqp::add_i(nqp::atpos($ins,$index),1))),
      ($index = nqp::add_i($index,$jump)),
      ($iterations = nqp::add_i($iterations, 1)))),
  nqp::say($iterations));
clarkema added a commit to clarkema/advent-of-code that referenced this issue Dec 22, 2017
Ran through the Perl 6 implementation of day 05 part 2 with the
help of #perl6, improving the performance from 1m20s to 0m13s.

Resulted in two new Rakudo issues for future improvements:

rakudo/rakudo#1328
rakudo/rakudo#1329
@zoffixznet
Copy link
Contributor Author

zoffixznet commented Jan 22, 2018

Don't see anything odd about it in --target=optimize or the profile. The biggest culprit is AT-POS, which is JITed (but not OSRed, which is already mentioned on #1421)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant