Skip to content

Commit

Permalink
RakuAST: some nano optimizations
Browse files Browse the repository at this point in the history
- use native ints where possible
- don't bother initializing native ints if the value is 0
- start loop counters at 0, instead of -1 + mandatory increment
- use prefix increment instead of postfix increment
  • Loading branch information
lizmat committed Feb 6, 2024
1 parent c03f42a commit 9478d5c
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 64 deletions.
15 changes: 8 additions & 7 deletions src/Raku/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {

# collect the if and all of the elsifs / orwiths
my @elsifs;
my $index := 0;
my int $index;
for @*IF-PARTS {
@elsifs.push:
Nodify('Statement',$_).new:
Expand Down Expand Up @@ -1221,15 +1221,16 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
sub intify($sign, $digits, $from) {
my $Int := $*LITERALS.int-type;
my $value := nqp::box_i(0, $Int);
my int $i := -1;
my int $chars := nqp::chars($digits);
my int $i;

while ++$i < $chars {
while $i < $chars {
$value := nqp::add_I(
nqp::mul_I($value, nqp::box_i(10, $Int), $Int),
nqp::box_i(nqp::index($from, nqp::substr($digits,$i,1)), $Int),
$Int
);
++$i;
}
$sign eq '' || $sign eq '¯' ?? nqp::neg_I($value,$Int) !! $value
}
Expand Down Expand Up @@ -2843,7 +2844,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {

method signature($/) {
my @parameters;
my int $param_idx := 0;
my int $param_idx;
for $<parameter> {
my $param := $_.ast;
my $sep := @*SEPS[$param_idx];
Expand All @@ -2857,7 +2858,7 @@ class Raku::Actions is HLL::Actions does Raku::CommonActions {
$param.set-invocant(1);
}
@parameters.push($param);
$param_idx := $param_idx + 1;
++$param_idx;
}
my $returns;
if $<typename> {
Expand Down Expand Up @@ -3919,7 +3920,7 @@ class Raku::RegexActions is HLL::Actions does Raku::CommonActions {
method assertion:sym<[>($/) {
my @elems := $<cclass_elem>;
my @asts;
my int $i := 0;
my int $i;
my int $n := nqp::elems(@elems);
while $i < $n {
my $sign := @elems[$i]<sign>;
Expand All @@ -3928,7 +3929,7 @@ class Raku::RegexActions is HLL::Actions does Raku::CommonActions {
$sign.panic('Missing + or - between character class elements')
}
@asts.push(@elems[$i].ast);
$i++;
++$i;
}
self.attach: $/, Nodify('Regex', 'Assertion', 'CharClass').new(|@asts);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Raku/Grammar.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ role Raku::Common {
$string eq nqp::chr(nqp::ord($string))
}
else {
my int $i := -1;
while ++$i < $chars
my int $i;
while $i < $chars
&& nqp::eqat($string,nqp::chr(nqp::ord($string,$i)),$i) {
++$i;
}
$i == $chars
}
Expand Down Expand Up @@ -358,11 +359,12 @@ role Raku::Common {
my int $actualchars := nqp::chars($ws);
my int $indent := $actualchars;
my int $tabstop := $*R.resolve-lexical('$?TABSTOP').compile-time-value;
my int $checkidx := -1;
while ++$checkidx < $actualchars {
my int $checkidx;
while $checkidx < $actualchars {
if nqp::eqat($ws, "\t", $checkidx) {
$indent := $indent + ($tabstop - 1);
}
++$checkidx;
}
$heredoc.set-indent($indent);
$heredoc.trim();
Expand Down Expand Up @@ -1469,7 +1471,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
<.block-loop><.kok>
:s''
[
:my $exprs := 0;
:my int $exprs;
'('
[
<e1=.EXPR>? { $exprs := 1 if $<e1> }
Expand Down Expand Up @@ -3044,7 +3046,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
unless $*LANG.pragma('p5isms') {
if $name eq 'say' || $name eq 'put' || $name eq 'print' {
my $al := $<args><arglist>;
my int $ok := 0;
my int $ok;
$ok := 1 unless $al<EXPR> eq '';
$ok := 1 if $<args><semiarglist>;
unless $ok {
Expand Down Expand Up @@ -4908,7 +4910,7 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
# Work out what default precedence we want, or if it's more special
# than just an operator.
my %prec;
my $is-operator := 0;
my int $is-operator;
my @parts := nqp::split(' ', $opname);
# Sanity checks
Expand Down
25 changes: 13 additions & 12 deletions src/Raku/ast/code.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ class RakuAST::Code
# lexpads, if they have any.
my @coderefs := $comp.backend.compunit_coderefs($precomp);
my int $num-subs := nqp::elems(@coderefs);
my int $i := -1;
my int $i;
my $result;
while ++$i < $num-subs {
while $i < $num-subs {
my $subid := nqp::getcodecuid(@coderefs[$i]);

# un-stub code objects for blocks we just compiled:
Expand Down Expand Up @@ -309,6 +309,7 @@ class RakuAST::Code
# asked to compile.
$result := @coderefs[$i];
}
++$i;
}
$result
}
Expand Down Expand Up @@ -569,22 +570,22 @@ class RakuAST::PlaceholderParameterOwner
for self.IMPL-PLACEHOLDER-MAP() {
my $placeholder := $_.value[0];
if nqp::istype($placeholder, RakuAST::VarDeclaration::Placeholder::Positional) {
my int $insert-at := 0;
my int $insert-at;
my str $desigil-insert := nqp::substr($placeholder.lexical-name, 1);
while $insert-at < nqp::elems(@positionals) {
my str $desigil-cur := nqp::substr(@positionals[$insert-at].lexical-name, 1);
last if $desigil-insert lt $desigil-cur;
$insert-at++;
++$insert-at;
}
nqp::splice(@positionals, [$placeholder], $insert-at, 0);
}
elsif nqp::istype($placeholder, RakuAST::VarDeclaration::Placeholder::Named) {
my int $insert-at := 0;
my int $insert-at;
my str $desigil-insert := nqp::substr($placeholder.lexical-name, 1);
while $insert-at < nqp::elems(@nameds) {
my str $desigil-cur := nqp::substr(@nameds[$insert-at].lexical-name, 1);
last if $desigil-insert lt $desigil-cur;
$insert-at++;
++$insert-at;
}
nqp::splice(@nameds, [$placeholder], $insert-at, 0);
}
Expand Down Expand Up @@ -1578,9 +1579,9 @@ class RakuAST::Routine
}

method PRODUCE-IMPLICIT-DECLARATIONS() {
my $slash := 1;
my $exclamation-mark := 1;
my $underscore := 1;
my int $slash := 1;
my int $exclamation-mark := 1;
my int $underscore := 1;
my @declarations;
if $!signature {
$!signature.IMPL-ENSURE-IMPLICITS;
Expand Down Expand Up @@ -2427,7 +2428,7 @@ class RakuAST::QuotedRegex
my $match-qast := QAST::Op.new(
:op('callmethod'), :name('match'), $topic, $closure
);
my int $is-multiple-match := 0;
my int $is-multiple-match;
for self.IMPL-UNWRAP-LIST(self.adverbs) {
my str $norm := self.IMPL-NORMALIZE-ADVERB($_.key);
if self.IMPL-IS-POSITION-ADVERB($norm) {
Expand Down Expand Up @@ -2601,8 +2602,8 @@ class RakuAST::Substitution
my $match-lookup := $slash.IMPL-TO-QAST($context);
my int $samespace := $!samespace;
my int $sigspace := $samespace;
my int $samecase := 0;
my int $samemark := 0;
my int $samecase;
my int $samemark;
for self.IMPL-UNWRAP-LIST(self.adverbs) {
my str $norm := self.IMPL-NORMALIZE-ADVERB($_.key);
if self.IMPL-IS-POSITION-ADVERB($norm) {
Expand Down
6 changes: 3 additions & 3 deletions src/Raku/ast/expressions.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,13 @@ class RakuAST::Infix
|| $!operator eq 'with' || $!operator eq 'without'
) {
my $thunky := self.properties.thunky;
my $i := 0;
my int $i;
for @operands {
my $type := nqp::substr($thunky, $i, $i + 1);
if $type && $type ne '.' {
self.IMPL-THUNK-ARGUMENT($resolver, $context, $_, $type);
}
$i++ if $i < nqp::chars($thunky) - 1;
++$i if $i < nqp::chars($thunky) - 1;
}
}
}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ class RakuAST::WhateverApplicable
my $index := -1;
for @operands {
my $operand := $_;
$index++; # it needs to count every time so that we write into the appropriate slot in @operands
++$index; # it needs to count every time so that we write into the appropriate slot in @operands
if $child-curries-whatevercode
&&
((nqp::istype($operand, RakuAST::WhateverApplicable)
Expand Down
7 changes: 2 additions & 5 deletions src/Raku/ast/literals.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,9 @@ class RakuAST::QuotedString
}
}

my int $return-list := 0;
my int $return-list;
for $!processors {
if $_ eq 'words' {
$return-list := 1;
}
elsif $_ eq 'quotewords' {
if $_ eq 'words' || $_ eq 'quotewords' {
$return-list := 1;
}
elsif $_ eq 'val' && nqp::elems(@parts) == 1 {
Expand Down
4 changes: 2 additions & 2 deletions src/Raku/ast/name.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class RakuAST::Name
$PseudoStash-lookup.IMPL-TO-QAST($context)
);
}
my $first := 1;
my int $first := 1;
for $!parts {
if $first { # don't call .WHO on the pseudo package itself, index into it instead
$first := 0;
Expand All @@ -251,7 +251,7 @@ class RakuAST::Name
method IMPL-QAST-PACKAGE-LOOKUP(RakuAST::IMPL::QASTContext $context, Mu $start-package, RakuAST::Declaration :$lexical, str :$sigil, Bool :$global-fallback) {
my $result := QAST::WVal.new(:value($start-package));
my $final := $!parts[nqp::elems($!parts) - 1];
my int $first := 0;
my int $first;
if nqp::istype($!parts[0], RakuAST::Name::Part::Simple) && $!parts[0].name eq 'GLOBAL' {
$result := QAST::Op.new(:op<getcurhllsym>, QAST::SVal.new(:value<GLOBAL>));
$first := 1;
Expand Down
6 changes: 3 additions & 3 deletions src/Raku/ast/nqp.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RakuAST::Nqp
# We want to make use of nqp ops as simple as possible, so
# we automatically convert common types to their RakuAST
# equivalents.
my int $i := 0;
my int $i;
my int $n := nqp::elems($args);
while $i < $n {
my $arg := $args[$i];
Expand Down Expand Up @@ -72,15 +72,15 @@ class RakuAST::Nqp
# know which are which, but if we're writing out an `nqp::op`
# just assume that they should all be unboxed; most situations
# will see the dispatch op generated anyway.
my int $i := 0;
my int $i;
my int $n := nqp::elems($call.list);
while $i < $n {
my $arg := $call[$i];
if nqp::istype($arg, QAST::Want)
&& ($arg[1] eq 'Ss' || $arg[1] eq 'Ii') {
$call[$i] := $arg[2];
}
$i++;
++$i;
}
$call
}
Expand Down
8 changes: 4 additions & 4 deletions src/Raku/ast/origins.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ class RakuAST::Origin::Source {

method SETUP-LINE-POSITIONS() {
nqp::bindattr(self, RakuAST::Origin::Source, '$!line-ends', []);
my int $nl-pos := 0;
my int $nl-pos;
my int $total := nqp::chars($!orig);
while ($nl-pos := nqp::findcclass(nqp::const::CCLASS_NEWLINE, $!orig, $nl-pos, $total)) < $total {
my $ord := nqp::ord($!orig, $nl-pos);
my int $ord := nqp::ord($!orig, $nl-pos);
nqp::push($!line-ends, ++$nl-pos);
# Treat \r\n as a single logical newline. Note that NFG
# implementations, we should check it really is a lone \r,
Expand Down Expand Up @@ -133,7 +133,7 @@ class RakuAST::Origin::Source {

method original-line-column(int $pos) {
my @line-ends := $!line-ends;
my int $lo := 0;
my int $lo;
my int $hi := nqp::elems(@line-ends);
my int $line;
while $lo < $hi {
Expand All @@ -159,7 +159,7 @@ class RakuAST::Origin::Source {
my $orig-line := @orig-line-col[0];
my $column := @orig-line-col[1];
my int $hi := nqp::elems($!line-file);
my int $lo := 0;
my int $lo;
my int $idx;
while $lo < $hi {
$idx := nqp::div_i($lo + $hi, 2);
Expand Down
5 changes: 3 additions & 2 deletions src/Raku/ast/pair.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ class RakuAST::ColonPairish {
else {
my $new := '';
my int $e := nqp::chars($v);
my int $i := -1;
while ++$i < $e {
my int $i;
while $i < $e {
my $ch := nqp::substr($v,$i,1);
$new := $new ~ '\\' if $ch eq '<' || $ch eq '>';
$new := $new ~ $ch;
++$i;
}
'<' ~ $new ~ '>';
}
Expand Down
10 changes: 5 additions & 5 deletions src/Raku/ast/resolver.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ class RakuAST::Resolver {
}
else {
my $new := '';
my int $i := 0;
my int $i;
my int $e := nqp::chars($v);
while $i < $e {
my $ch := nqp::substr($v,$i,1);
Expand Down Expand Up @@ -1032,11 +1032,11 @@ class RakuAST::Resolver::Compile
}

method make_levenshtein_evaluator($orig_name, @candidates) {
my $find-count := 0;
my $try-count := 0;
my int $find-count;
my int $try-count;
my &inner := my sub ($name) {
# difference in length is a good lower bound.
$try-count := $try-count + 1;
++$try-count;
return 0 if $find-count > 20 || $try-count > 1000;
my $parlen := nqp::chars($orig_name);
my $lendiff := nqp::chars($name) - $parlen;
Expand All @@ -1051,7 +1051,7 @@ class RakuAST::Resolver::Compile
if $target != -1 {
my $name-str := nqp::box_s($name, Str);
nqp::push($target, $name-str);
$find-count := $find-count + 1;
++$find-count;
}
1;
}
Expand Down
Loading

0 comments on commit 9478d5c

Please sign in to comment.