Skip to content

Commit

Permalink
Give Seq its own .join, 25% faster than List.join
Browse files Browse the repository at this point in the history
- it won't try to reify stuff first
- makes constructs like .map().join faster with less memory pressure
- tested with Seq.new(^10 .iterator).join
  • Loading branch information
lizmat committed Jan 6, 2017
1 parent 823f0f7 commit 3c52aa0
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/Seq.pm
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ my class Seq is Cool does Iterable does PositionalBindFailover {
self.cache.perl ~ '.Seq';
}

method join(Seq:D: $separator = '' --> Str) {
nqp::if(
(my $iterator := self.iterator).is-lazy,
'...',
nqp::stmts(
(my $strings := nqp::list_s),
nqp::until(
nqp::eqaddr((my $pulled := $iterator.pull-one),IterationEnd),
nqp::push_s($strings,nqp::unbox_s(
nqp::if(
nqp::isconcrete($pulled) && nqp::istype($pulled,Str),
$pulled,
nqp::if(
nqp::can($pulled,'Str'),
$pulled.Str,
nqp::box_s($pulled,Str)
)
)
))
),
nqp::box_s(nqp::join(nqp::unbox_s($separator.Str),$strings),Str)
)
)
}

method sink() {
self.iterator.sink-all if $!iter.DEFINITE;
Nil
Expand Down

0 comments on commit 3c52aa0

Please sign in to comment.