Skip to content

Commit

Permalink
Optimize ADD-ITERATOR-TO-BAG a few percent
Browse files Browse the repository at this point in the history
For the case where keys already exist in the Bag.  This same optimization
(using nqp::ifnull(nqp::atkey instead of nqp::if(nqp::existskey...nqp::atkey)
could probably be done for *all* the methods in Rakudo/QuantHash

Spotted by MasterDuke++
  • Loading branch information
lizmat committed Mar 16, 2021
1 parent d525dbe commit 7c0a556
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/core.c/Rakudo/QuantHash.pm6
Expand Up @@ -576,21 +576,30 @@ my class Rakudo::QuantHash {
}

method ADD-ITERATOR-TO-BAG(\elems, Mu \iterator, Mu \type) {
my $pair;
nqp::until(
nqp::eqaddr(
(my \pulled := nqp::decont(iterator.pull-one)),
IterationEnd
),
nqp::if(
nqp::existskey(elems,(my \which := pulled.WHICH)),
nqp::stmts(
(my \pair := nqp::atkey(elems,which)),
nqp::bindattr(pair,Pair,'$!value',
nqp::add_i(nqp::getattr(pair,Pair,'$!value'),1)
)
nqp::bindattr($pair,Pair,'$!value',nqp::add_i(
nqp::getattr(
($pair := nqp::ifnull(
nqp::atkey(elems,(my \which := pulled.WHICH)),
nqp::if(
nqp::istype(pulled,type),
nqp::bindkey(elems,which,Pair.new(pulled,0)),
X::TypeCheck::Binding.new(
got => pulled,
expected => type
).throw
)
)),
Pair,
'$!value'
),
self.BIND-TO-TYPED-BAG(elems, which, pulled, 1, type)
)
1
))
);
elems
}
Expand Down

2 comments on commit 7c0a556

@usev6
Copy link
Contributor

@usev6 usev6 commented on 7c0a556 Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this breaks things on the JVM backend. I didn't fully investigate yet, but basic things stopped to work:

$ ./rakudo-j --ll-exception -e 'dd bag(42)'
java.lang.RuntimeException: No such attribute '$!value' for this object
  in ADD-ITERATOR-TO-BAG (gen/jvm/CORE.c.setting:10734)
  in create-from-iterator (gen/jvm/CORE.c.setting:60275)
  in dispatch:<!> (gen/jvm/CORE.c.setting:2083)
  in new (gen/jvm/CORE.c.setting:60288)
  in new (gen/jvm/CORE.c.setting:1175)
  in bag (gen/jvm/CORE.c.setting:61844)
  in <unit> (-e:1)
  in <unit-outer> (-e:1)
  in eval (gen/jvm/stage2/NQPHLL.nqp:1216)
  in <anon> (gen/jvm/stage2/NQPHLL.nqp:1328)
  in command_eval (gen/jvm/stage2/NQPHLL.nqp:1325)
  in command_eval (gen/jvm/Compiler.nqp:119)
  in command_line (gen/jvm/stage2/NQPHLL.nqp:1309)
  in MAIN (gen/jvm/rakudo.nqp:133)
  in <mainline> (gen/jvm/rakudo.nqp:121)
  in <anon> (gen/jvm/rakudo.nqp)

Reverting this commit makes the code work again.

@usev6
Copy link
Contributor

@usev6 usev6 commented on 7c0a556 Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an issue with a golfed version of the failing code: #4272

Please sign in to comment.