Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fast-path for common case in MapIter on JVM.

  • Loading branch information...
commit 5a1e607503a34cbb92374234c98e68dcab58ae91 1 parent 11b5dae
@jnthn jnthn authored
Showing with 58 additions and 28 deletions.
  1. +58 −28 src/core/MapIter.pm
View
86 src/core/MapIter.pm
@@ -124,36 +124,66 @@ my class MapIter is Iterator {
#?if !parrot
my int $state = 1;
my int $itmp;
+ my Mu $items := $!items;
my Mu $args := nqp::list();
- nqp::while(($state && nqp::elems($rpa) < $count), nqp::handle(
- nqp::stmts(
- nqp::if(nqp::iseq_i($state, 1), nqp::stmts(
- ($itmp = nqp::elems($!items)),
- nqp::unless($itmp >= $argc, nqp::stmts(
- ($itmp = $argc - $itmp),
- nqp::if($!listiter, $!listiter.reify($itmp))
+ my Mu $arg;
+ if $argc == 1 && !$NEXT {
+ # Fast path case: only 1 argument for each block, no NEXT phaser.
+ nqp::while(($state && nqp::elems($rpa) < $count), nqp::handle(
+ nqp::stmts(
+ nqp::if(nqp::iseq_i($state, 1), nqp::stmts(
+ nqp::unless(nqp::elems($items), nqp::stmts(
+ nqp::if($!listiter, $!listiter.reify(1))
+ )),
+ nqp::if($items,
+ nqp::stmts(($arg := nqp::shift($items)), $state = 2),
+ $state = 0)
)),
- nqp::setelems($args, 0),
- nqp::p6shiftpush($args, $!items, $argc),
- nqp::if($args, $state = 2, $state = 0)
- )),
- nqp::if(nqp::iseq_i($state, 2), nqp::stmts(
- nqp::push($rpa, nqp::p6invokeflat($block, $args)),
- $state = 3
- )),
- nqp::if(nqp::iseq_i($state, 3), nqp::stmts(
- nqp::if($NEXT, $block.fire_phasers('NEXT')),
- ($state = 1)
- ))
- ),
- 'LAST', nqp::stmts(
- ($!items := Any),
- ($!listiter := Any),
- ($state = 0)
- ),
- 'REDO', $state = 2,
- 'NEXT', $state = 3
- ));
+ nqp::if(nqp::iseq_i($state, 2), nqp::stmts(
+ nqp::push($rpa, $block($arg)),
+ $state = 1
+ ))
+ ),
+ 'LAST', nqp::stmts(
+ ($!items := Any),
+ ($!listiter := Any),
+ ($state = 0)
+ ),
+ 'REDO', $state = 2,
+ 'NEXT', $state = 1
+ ));
+ }
+ else {
+ nqp::while(($state && nqp::elems($rpa) < $count), nqp::handle(
+ nqp::stmts(
+ nqp::if(nqp::iseq_i($state, 1), nqp::stmts(
+ ($itmp = nqp::elems($items)),
+ nqp::unless($itmp >= $argc, nqp::stmts(
+ ($itmp = $argc - $itmp),
+ nqp::if($!listiter, $!listiter.reify($itmp))
+ )),
+ nqp::setelems($args, 0),
+ nqp::p6shiftpush($args, $items, $argc),
+ nqp::if($args, $state = 2, $state = 0)
+ )),
+ nqp::if(nqp::iseq_i($state, 2), nqp::stmts(
+ nqp::push($rpa, nqp::p6invokeflat($block, $args)),
+ $state = 3
+ )),
+ nqp::if(nqp::iseq_i($state, 3), nqp::stmts(
+ nqp::if($NEXT, $block.fire_phasers('NEXT')),
+ ($state = 1)
+ ))
+ ),
+ 'LAST', nqp::stmts(
+ ($!items := Any),
+ ($!listiter := Any),
+ ($state = 0)
+ ),
+ 'REDO', $state = 2,
+ 'NEXT', $state = 3
+ ));
+ }
#?endif
if $!items || $!listiter {
Please sign in to comment.
Something went wrong with that request. Please try again.