Skip to content

Commit

Permalink
Make my @A = ^100; my int @b = @A about 9x as fast
Browse files Browse the repository at this point in the history
- create a separate List:D source candidate that can use nqp::atpos
- also for native str and num
  • Loading branch information
lizmat committed Apr 8, 2018
1 parent c015f08 commit b5318e6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
69 changes: 57 additions & 12 deletions src/core/native_array.pm6
Expand Up @@ -57,7 +57,7 @@ my class array does Iterable {

my role strarray[::T] does Positional[T] is array_type(T) {
#- start of generated part of strarray role -----------------------------------
#- Generated on 2018-04-08T23:06:46+02:00 by tools/build/makeNATIVE_ARRAY.pl6
#- Generated on 2018-04-08T23:34:26+02:00 by tools/build/makeNATIVE_ARRAY.pl6
#- PLEASE DON'T CHANGE ANYTHING BELOW THIS LINE

multi method AT-POS(strarray:D: int $idx) is raw {
Expand Down Expand Up @@ -96,14 +96,29 @@ my class array does Iterable {
nqp::setelems(self,nqp::elems(values));
nqp::splice(self,values,0,nqp::elems(values))
}
multi method STORE(strarray:D: List:D \values) {
my int $elems = values.elems; # reifies
my $reified := nqp::getattr(values,List,'$!reified');
nqp::setelems(self, $elems);

my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_s(self, $i,
nqp::unbox_s(nqp::atpos($reified,$i)))
);
self
}
multi method STORE(strarray:D: @values) {
my int $elems = @values.elems;
nqp::setelems(self, $elems);

my int $i = -1;
nqp::bindpos_s(self, $i,
nqp::unbox_s(@values.AT-POS($i)))
while nqp::islt_i($i = nqp::add_i($i,1),$elems);
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_s(self, $i,
nqp::unbox_s(@values.AT-POS($i)))
);
self
}

Expand Down Expand Up @@ -413,7 +428,7 @@ my class array does Iterable {

my role intarray[::T] does Positional[T] is array_type(T) {
#- start of generated part of intarray role -----------------------------------
#- Generated on 2018-04-08T23:06:46+02:00 by tools/build/makeNATIVE_ARRAY.pl6
#- Generated on 2018-04-08T23:34:26+02:00 by tools/build/makeNATIVE_ARRAY.pl6
#- PLEASE DON'T CHANGE ANYTHING BELOW THIS LINE

multi method AT-POS(intarray:D: int $idx) is raw {
Expand Down Expand Up @@ -452,14 +467,29 @@ my class array does Iterable {
nqp::setelems(self,nqp::elems(values));
nqp::splice(self,values,0,nqp::elems(values))
}
multi method STORE(intarray:D: List:D \values) {
my int $elems = values.elems; # reifies
my $reified := nqp::getattr(values,List,'$!reified');
nqp::setelems(self, $elems);

my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_i(self, $i,
nqp::unbox_i(nqp::atpos($reified,$i)))
);
self
}
multi method STORE(intarray:D: @values) {
my int $elems = @values.elems;
nqp::setelems(self, $elems);

my int $i = -1;
nqp::bindpos_i(self, $i,
nqp::unbox_i(@values.AT-POS($i)))
while nqp::islt_i($i = nqp::add_i($i,1),$elems);
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_i(self, $i,
nqp::unbox_i(@values.AT-POS($i)))
);
self
}

Expand Down Expand Up @@ -794,7 +824,7 @@ my class array does Iterable {

my role numarray[::T] does Positional[T] is array_type(T) {
#- start of generated part of numarray role -----------------------------------
#- Generated on 2018-04-08T23:06:46+02:00 by tools/build/makeNATIVE_ARRAY.pl6
#- Generated on 2018-04-08T23:34:26+02:00 by tools/build/makeNATIVE_ARRAY.pl6
#- PLEASE DON'T CHANGE ANYTHING BELOW THIS LINE

multi method AT-POS(numarray:D: int $idx) is raw {
Expand Down Expand Up @@ -833,14 +863,29 @@ my class array does Iterable {
nqp::setelems(self,nqp::elems(values));
nqp::splice(self,values,0,nqp::elems(values))
}
multi method STORE(numarray:D: List:D \values) {
my int $elems = values.elems; # reifies
my $reified := nqp::getattr(values,List,'$!reified');
nqp::setelems(self, $elems);

my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_n(self, $i,
nqp::unbox_n(nqp::atpos($reified,$i)))
);
self
}
multi method STORE(numarray:D: @values) {
my int $elems = @values.elems;
nqp::setelems(self, $elems);

my int $i = -1;
nqp::bindpos_n(self, $i,
nqp::unbox_n(@values.AT-POS($i)))
while nqp::islt_i($i = nqp::add_i($i,1),$elems);
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_n(self, $i,
nqp::unbox_n(@values.AT-POS($i)))
);
self
}

Expand Down
21 changes: 18 additions & 3 deletions tools/build/makeNATIVE_ARRAY.pl6
Expand Up @@ -83,14 +83,29 @@ for $*IN.lines -> $line {
nqp::setelems(self,nqp::elems(values));
nqp::splice(self,values,0,nqp::elems(values))
}
multi method STORE(#type#array:D: List:D \values) {
my int $elems = values.elems; # reifies
my $reified := nqp::getattr(values,List,'$!reified');
nqp::setelems(self, $elems);
my int $i = -1;
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_#postfix#(self, $i,
nqp::unbox_#postfix#(nqp::atpos($reified,$i)))

This comment has been minimized.

Copy link
@MasterDuke17

MasterDuke17 Apr 8, 2018

Contributor

Could the nqp::atpos be nqp::atpos_#postfix#?

This comment has been minimized.

Copy link
@MasterDuke17

MasterDuke17 Apr 8, 2018

Contributor

FWIW, I tried making that change and it did pass a spectest.

This comment has been minimized.

Copy link
@lizmat

lizmat Apr 9, 2018

Author Contributor

No, because this is the case where the generic List:D is processed. You need atpos for that. The candidate that has #type#array:D in its sig, is the one that would use the atpos_#postfix# version, but that one uses nqp::splice for performance, because it can.

This comment has been minimized.

Copy link
@MasterDuke17

MasterDuke17 Apr 9, 2018

Contributor

Right. Then this multi must not be getting exercised in roast.

);
self
}
multi method STORE(#type#array:D: @values) {
my int $elems = @values.elems;
nqp::setelems(self, $elems);
my int $i = -1;
nqp::bindpos_#postfix#(self, $i,
nqp::unbox_#postfix#(@values.AT-POS($i)))
while nqp::islt_i($i = nqp::add_i($i,1),$elems);
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
nqp::bindpos_#postfix#(self, $i,
nqp::unbox_#postfix#(@values.AT-POS($i)))
);
self
}
Expand Down

0 comments on commit b5318e6

Please sign in to comment.