Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use nqp::iterators instead of Rakudo Lists for calculating
Signature.count/.arity.  Also, once we've calculated them, keep them
around for the next time they're request (very common for blocks
used in map and other iterations).
  • Loading branch information
pmichaud committed Jun 23, 2011
1 parent 761fa93 commit 96fdaf8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/Perl6/Metamodel/BOOTSTRAP.pm
Expand Up @@ -183,12 +183,16 @@ sub scalar_attr($name, $type) {
# class Signature is Cool {
# has $!params;
# has $!returns;
# has $!arity;
# has $!count;
# ... # Uncomposed
# }
my stub Signature metaclass Perl6::Metamodel::ClassHOW { ... };
Signature.HOW.add_parent(Signature, Cool);
Signature.HOW.add_attribute(Signature, BOOTSTRAPATTR.new(:name<$!params>, :type(Mu)));
Signature.HOW.add_attribute(Signature, BOOTSTRAPATTR.new(:name<$!returns>, :type(Mu)));
Signature.HOW.add_attribute(Signature, BOOTSTRAPATTR.new(:name<$!arity>, :type(Mu)));
Signature.HOW.add_attribute(Signature, BOOTSTRAPATTR.new(:name<$!count>, :type(Mu)));
Signature.HOW.add_method(Signature, 'is_generic', sub ($self) {
# If any parameter is generic, so are we.
my @params := pir::getattribute__PPPs($self, Signature, '$!params');
Expand Down
55 changes: 27 additions & 28 deletions src/core/Signature.pm
@@ -1,38 +1,37 @@
my class Signature {
# declared in BOOTSTRAP.pm:
# is Cool; # parent class
# has $!params; # RPA of parameters
# has $!returns; # return type
# has $!arity; # cached arity
# has $!count; # cached count

method arity() {
my $params := self.params;
my $i := 0;
my $elems := $params.elems;
my $arity := 0;
while $i < $elems {
if $params[$i].positional && !$params[$i].optional {
$arity := $arity + 1;
$i := $i + 1;
}
else {
$i := $elems;
}
}
$arity
self.count if nqp::isnull($!arity) || !$!arity.defined;
$!arity;
}

method count() {
my $params := self.params;
my $i := 0;
my $elems := $params.elems;
my $arity := 0;
while $i < $elems {
if $params[$i].positional {
$arity := $arity + 1;
$i := $i + 1;
}
else {
$i := $elems;
if nqp::isnull($!count) || !$!count.defined {
# calculate the count and arity -- we keep them
# cached for when we're called the next time.
my $count = 0;
my $arity = 0;
my Mu $iter := nqp::iterator($!params);
my $param;
while $iter {
$param := nqp::shift($iter);
if $param.positional {
$count++;
$arity++ unless $param.optional;
}
}
nqp::bindattr(self, Signature, '$!arity', $arity);
nqp::bindattr(self, Signature, '$!count', $count);
}
$arity
$!count
}

method params() {
pir::perl6_list_from_rpa__PPPP(List, pir::clone__PP($!params), Mu);
}
Expand Down

0 comments on commit 96fdaf8

Please sign in to comment.