Navigation Menu

Skip to content

Commit

Permalink
Use low-level code for List!fill
Browse files Browse the repository at this point in the history
4x speed on iteration microbenchmark, down to 260 us/iter
  • Loading branch information
sorear committed Sep 9, 2010
1 parent e093ea4 commit 8df43ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
37 changes: 20 additions & 17 deletions lib/SAFE.setting
Expand Up @@ -548,23 +548,26 @@ my class List is Cool {
(prog (rawcall (cast VarDeque (rawcall (cast DynObject (@ (pos 0))) GetSlot (s rest))) UnshiftN (unbox 'Variable[]' (@ (pos 1)))) (null Variable))
} }
method !fill($nr) {
while count-items(self) < $nr && has-iterators(self) {
if $!flat && first-rest-flattens(self) {
unshift-iterator(self, shift-iterator(self).iterator);
} else {
my $v := shift-iterator(self);
if $v.^isa(Iterator) {
unshift-iterator-n(self, $v.reify);
} else {
push-item(self, self!elem($v));
}
}
}
count-items(self) >= $nr;
}
method !fill($nr) { Q:CgOp {
(letn nr (cast Int32 (unbox Double (@ {$nr})))
items (cast VarDeque (getslot (s items) (@ {self})))
rest (cast VarDeque (getslot (s rest) (@ {self})))
flat (unbox Boolean (@ {$!flat.Bool}))
v (null Variable)
ItMo (rawcall (@ {Iterator}) GetMO)
(whileloop 0 0
(ternary (< (rawcall (l items) Count) (l nr))
(!= (rawcall (l rest) Count) (i 0)) (b 0))
(prog
(l v (rawcall (l rest) Shift))
(ternary (ternary (l flat) (getfield islist (l v)) (b 0))
(rawcall (l rest) Unshift (methodcall (l v) iterator))
(ternary (rawcall (@ (l v)) Isa (l ItMo))
(rawcall (l rest) UnshiftN (unbox 'Variable[]'
(@ (methodcall (l v) reify))))
(rawcall (l items) Push (methodcall {self} !elem (l v)))))))
(box Bool (>= (rawcall (l items) Count) (l nr))))
} }
method Bool() { self!fill(1) }
method shift() {
Expand Down
6 changes: 6 additions & 0 deletions src/CgOp.pm
Expand Up @@ -258,6 +258,9 @@ use warnings;
$self->zyg->[0] = $self->zyg->[0]->cps_convert(1);
$self->zyg->[$_] = $self->zyg->[$_]->cps_convert($nv) for (1,2);

$self->zyg->[0] = CgOp::Primitive->new(op => ['result'],
zyg => [$self->zyg->[0]]) if $self->zyg->[0]->cps_type == 1;

if ($nv) {
$self->zyg->[$_] = ($self->zyg->[$_]->cps_type == 1) ?
$self->zyg->[$_] : CgOp::Primitive->new(op => ['set_result'],
Expand Down Expand Up @@ -300,6 +303,9 @@ use warnings;
$self->zyg->[0] = $self->zyg->[0]->cps_convert(1);
$self->zyg->[1] = $self->zyg->[1]->cps_convert(0);

$self->zyg->[0] = CgOp::Primitive->new(op => ['result'],
zyg => [$self->zyg->[0]]) if $self->zyg->[0]->cps_type == 1;

$self->cps_type(1);
$self;
}
Expand Down
5 changes: 4 additions & 1 deletion src/CodeGen.pm
Expand Up @@ -271,7 +271,10 @@ use 5.010;

sub _popn {
my ($self, $nr) = @_;
Carp::confess "Stack underflow" if $nr > @{ $self->stacktype };
if ($nr > @{ $self->stacktype }) {
print for @{ $self->buffer };
Carp::confess "stack undeflow";
}
(splice @{ $self->stackterm }, -$nr, $nr),
(splice @{ $self->stacktype }, -$nr, $nr);
}
Expand Down

0 comments on commit 8df43ce

Please sign in to comment.