Skip to content
Permalink
Browse files

Add IterationBuffer.List method

- because Lists are nowadays built with $!reified being an IterationBuffer
- it seemed handy to have
- it makes a lot of code easier to read
- and creates smaller candidates that potentially inline better
- and the new call should inline pretty quickly

And apply this to the setting where appropriate.
  • Loading branch information...
lizmat committed Oct 9, 2018
1 parent 35f3d83 commit 8c10da6abd917fabfa833d786e63988fb7fc2a9c
Showing with 41 additions and 81 deletions.
  1. +6 βˆ’19 src/core/Any-iterable-methods.pm6
  2. +2 βˆ’3 src/core/Array.pm6
  3. +7 βˆ’3 src/core/IterationBuffer.pm6
  4. +1 βˆ’5 src/core/Main.pm6
  5. +4 βˆ’11 src/core/Map.pm6
  6. +1 βˆ’1 src/core/Mu.pm6
  7. +2 βˆ’2 src/core/Parameter.pm6
  8. +10 βˆ’26 src/core/Rakudo/Iterator.pm6
  9. +1 βˆ’1 src/core/ShapedArray.pm6
  10. +6 βˆ’9 src/core/Str.pm6
  11. +1 βˆ’1 src/core/native_array.pm6
@@ -1451,14 +1451,12 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
multi method sort() {
nqp::if(
nqp::eqaddr(
self.iterator.push-until-lazy(my $list := IterationBuffer.new),
self.iterator.push-until-lazy(my \buf := IterationBuffer.new),
IterationEnd
),
Seq.new(
Rakudo::Iterator.ReifiedList(
Rakudo::Sorting.MERGESORT-REIFIED-LIST(
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$list)
)
Rakudo::Sorting.MERGESORT-REIFIED-LIST(buf.List)
)
),
X::Cannot::Lazy.new(:action<sort>).throw
@@ -1468,7 +1466,7 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
nqp::stmts(
nqp::unless(
nqp::eqaddr(
self.iterator.push-until-lazy(my $list := IterationBuffer.new),
self.iterator.push-until-lazy(my \buf := IterationBuffer.new),
IterationEnd
),
X::Cannot::Lazy.new(:action<sort>).throw
@@ -1477,22 +1475,11 @@ Did you mean to add a stub (\{...\}) or did you mean to .classify?"
Rakudo::Iterator.ReifiedList(
nqp::if(
nqp::eqaddr(&by,&infix:<cmp>),
Rakudo::Sorting.MERGESORT-REIFIED-LIST(
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$list)
),
Rakudo::Sorting.MERGESORT-REIFIED-LIST(buf.List),
nqp::if(
&by.count < 2,
Rakudo::Sorting.MERGESORT-REIFIED-LIST-AS(
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$list),
&by
),
Rakudo::Sorting.MERGESORT-REIFIED-LIST-WITH(
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$list),
&by
)
Rakudo::Sorting.MERGESORT-REIFIED-LIST-AS( buf.List,&by),
Rakudo::Sorting.MERGESORT-REIFIED-LIST-WITH(buf.List,&by)
)
)
)
@@ -428,8 +428,7 @@ my class Array { # declared in BOOTSTRAP
nqp::isconcrete(my $reified := nqp::getattr(self,List,'$!reified')),
nqp::if(
$view, # assume no change in array
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$reified),
$reified.List,
nqp::stmts( # make cow copy
(my int $elems = nqp::elems($reified)),
(my $cow := nqp::setelems(nqp::create(IterationBuffer),$elems)),
@@ -438,7 +437,7 @@ my class Array { # declared in BOOTSTRAP
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos($cow,$i,nqp::ifnull(nqp::decont(nqp::atpos($reified,$i)),Nil)),
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$cow)
$cow.List
)
),
nqp::create(List) # was empty, is empty
@@ -39,12 +39,16 @@ my class IterationBuffer {
nqp::bindpos(self, $pos, value)
}

# For maintainability mainly, and possibly for creating smaller, more
# inlineable candidates
method List(IterationBuffer:D:) {
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',self)
}

# For core debugging purposes only: basically warp the IterationBuffer
# into a full-fledged List and .perl that. We don't care that it will
# not round-trip.
multi method perl(IterationBuffer:D:) {
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',self).perl
}
multi method perl(IterationBuffer:D:) { self.List.perl }
}

#?if jvm
@@ -64,11 +64,7 @@ my sub MAIN_HELPER($IN-as-ARGSFILES, $retval = 0) {
}
}

Capture.new(
list => nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$positional),
hash => %named
)
Capture.new( list => $positional.List, hash => %named )
}

# Generate $?USAGE string (default usage info for MAIN)
@@ -107,10 +107,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
)
}

method List() {
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',self.IterationBuffer)
}
method List() { self.IterationBuffer.List }

multi method head(Map:D:) {
nqp::if(
@@ -129,9 +126,7 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
Seq.new(
Rakudo::Iterator.ReifiedList(
Rakudo::Sorting.MERGESORT-REIFIED-LIST-AS(
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',self.IterationBuffer
),
self.IterationBuffer.List,
{ nqp::getattr(nqp::decont($^a),Pair,'$!key') }
)
)
@@ -203,10 +198,8 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
}
method iterator(Map:D:) { Iterate.new(self) }

method list(Map:D:) {
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',self.IterationBuffer)
}
method list(Map:D:) { self.List }

multi method pairs(Map:D:) { Seq.new(self.iterator) }
multi method keys(Map:D:) { Seq.new(Rakudo::Iterator.Mappy-keys(self)) }
multi method values(Map:D:) { Seq.new(Rakudo::Iterator.Mappy-values(self)) }
@@ -843,7 +843,7 @@ Perhaps it can be found at https://docs.perl6.org/type/$name"
$meth = ($obj.^submethod_table){name} if !$meth && $i == 0;
nqp::push($results,$meth(SELF, |c)) if $meth;
}
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$results)
$results.List
}

method dispatch:<hyper>(Mu \SELF: $nodality, Str $meth-name, |c) {
@@ -118,7 +118,7 @@ my class Parameter { # declared in BOOTSTRAP
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos($buf,$i,nqp::atpos_s(@!named_names,$i))
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$buf)
$buf.List
),
nqp::create(List)
)
@@ -185,7 +185,7 @@ my class Parameter { # declared in BOOTSTRAP
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos($buf,$i,nqp::atpos_s(@!type_captures,$i))
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$buf)
$buf.List
),
nqp::create(List)
)
@@ -184,7 +184,7 @@ class Rakudo::Iterator {
nqp::isle_i(($i = nqp::add_i($i,1)),$!maxdim),
nqp::bindpos($buffer,$i,nqp::atpos_i($!dims,$i))
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$buffer)
$buffer.List
)
}

@@ -560,9 +560,7 @@ class Rakudo::Iterator {
&& ($!is-exhausted = 1) # set the flag
&& ($!complete || nqp::not_i(nqp::elems(reified))),
IterationEnd,
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',reified
)
reified.List
)
)
)
@@ -775,10 +773,7 @@ class Rakudo::Iterator {
nqp::if(
$!b,
nqp::clone($!combination),
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',
nqp::clone($!combination)
)
nqp::clone($!combination).List
)
),
IterationEnd
@@ -919,8 +914,7 @@ class Rakudo::Iterator {
nqp::stmts(

# set up result of this pull
(my $result := nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',nqp::clone($!next))),
(my $result := nqp::clone($!next).List),

# start working on next result
nqp::unless(
@@ -2592,8 +2586,7 @@ class Rakudo::Iterator {
nqp::if(
$!b,
$permuted,
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$permuted)
$permuted.List
)
),
IterationEnd
@@ -3112,10 +3105,7 @@ class Rakudo::Iterator {
nqp::if(
nqp::islt_i($gap,0),
nqp::stmts( # keep some for next
(my $result := nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',
nqp::clone($!buffer)
)),
(my $result := nqp::clone($!buffer).List),
nqp::if(
nqp::islt_i(nqp::elems($!buffer),$elems),
nqp::setelems($!buffer,0), # was :partial, now done
@@ -3131,10 +3121,7 @@ class Rakudo::Iterator {
nqp::if(
nqp::isle_i(nqp::elems($!buffer),$elems),
nqp::stmts( # whole buffer ok
($result := nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',
$!buffer
)),
($result := $!buffer.List),
($!buffer := nqp::create(IterationBuffer))
),
nqp::stmts( # partial buffer ok
@@ -3221,8 +3208,7 @@ class Rakudo::Iterator {
),
nqp::if(
nqp::elems($buf),
nqp::p6bindattrinvres( # at least one not exhausted
nqp::create(List),List,'$!reified',$buf),
$buf.List,
nqp::stmts( # we're done
#?if jvm
($!iters := Mu),
@@ -3344,8 +3330,7 @@ class Rakudo::Iterator {
)
)
),
nqp::p6bindattrinvres( # what we found
nqp::create(List),List,'$!reified',$buf)
$buf.List # what we found
),
IterationEnd # done iterating
)
@@ -4052,8 +4037,7 @@ class Rakudo::Iterator {
#?endif
IterationEnd
),
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',buf)
buf.List
)
)
)
@@ -473,7 +473,7 @@
multi method List(::?CLASS:D:) {
my \buf := nqp::create(IterationBuffer);
self.iterator.push-all(buf);
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',buf)
buf.List
}

multi method Array(::?CLASS:D:) {
@@ -207,7 +207,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
($pos = nqp::add_i($i,$add))
)
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$indices)
$indices.List
)
}
multi method indices(Str:D: Cool:D $needle, Cool:D $start, *%pars) {self.indices: $needle.Str, $start.Int, |%pars}
@@ -229,7 +229,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
($pos = nqp::add_i($i,$add))
)
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$indices)
$indices.List
)
)
)
@@ -880,8 +880,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
),
nqp::push($matches,$pulled)
),
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$matches)
$matches.List
),
nqp::stmts( # upto the max index
(my int $todo = $max - $min + 1),
@@ -896,8 +895,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
),
nqp::if(
nqp::iseq_i($i,$todo),
nqp::p6bindattrinvres( # found all values
nqp::create(List),List,'$!reified',$matches),
$matches.List, # found all values
Empty # no match, since not all values
)
)
@@ -968,8 +966,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
),
nqp::if(
nqp::elems($matches) >= $min,
nqp::p6bindattrinvres(
nqp::create(List),List,'$!reified',$matches),
$matches.List,
()
)
))
@@ -2256,7 +2253,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
Words.new(self).push-all(my $words := nqp::create(IterationBuffer));
nqp::elems($words) == 1
?? nqp::shift($words)
!! nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$words)
!! $words.List
}

proto method encode(|) {*}
@@ -1620,7 +1620,7 @@ my class array does Iterable {
nqp::islt_i(($i = nqp::add_i($i,1)),$dims),
nqp::bindpos($odims,$i,nqp::atpos_i($idims,$i))
),
nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$odims)
$odims.List
)
}

0 comments on commit 8c10da6

Please sign in to comment.
You can’t perform that action at this time.