Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make indirect type lookup 3x as fast
Should have a positive effect on bare startup time, since we do at
least 5 of these every time we start.
  • Loading branch information
lizmat committed Nov 20, 2016
1 parent 7f26e8b commit 939d273
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions src/core/operators.pm
Expand Up @@ -536,34 +536,60 @@ sub prefix:<let>(\cont) is raw {
cont
}

# not sure where this should go
# this implements the ::() indirect lookup
sub INDIRECT_NAME_LOOKUP($root, *@chunks) is raw {
# note that each part of @chunks itself can
# contain double colons. That's why joining and
# re-splitting is necessary
my Str $name = @chunks.join('::');
my @parts = $name.split('::');
my $first = @parts.shift;
if @parts && '$@%&'.index(substr($first,0, 1)).defined {
# move sigil from first to last chunk, because
# $Foo::Bar::baz is actually stored as Foo::Bar::$baz
my $last_idx = @parts.end;
@parts[$last_idx] = substr($first,0, 1) ~ @parts[$last_idx];
$first = substr($first,1);
if $first eq '' {
$first = @parts.shift;
$name = @chunks.join('::');
}
}
my Mu $thing := $root.EXISTS-KEY($first) ?? $root{$first} !!
GLOBAL::.EXISTS-KEY($first) ?? GLOBAL::{$first} !!
X::NoSuchSymbol.new(symbol => $name).fail;
for @parts {
X::NoSuchSymbol.new(symbol => $name).fail unless $thing.WHO.EXISTS-KEY($_);
$thing := $thing.WHO{$_};
}
$thing;
nqp::if(
# Note that each part of @chunks itself can contain double colons.
# That's why joining and re-splitting is necessary
nqp::elems(my $parts :=
nqp::split('::',my str $name = @chunks.join('::'))),
nqp::stmts(
(my str $first = nqp::shift($parts)),
nqp::if(
nqp::elems($parts),
nqp::stmts(
(my str $sigil = nqp::substr($first,0,1)),
nqp::if(
nqp::iseq_s($sigil,'$')
|| nqp::iseq_s($sigil,'@')
|| nqp::iseq_s($sigil,'%')
|| nqp::iseq_s($sigil,'&'),
nqp::stmts(
nqp::push($parts,
nqp::concat($sigil,nqp::unbox_s(nqp::pop($parts)))),
($first = nqp::substr($first,1))
)
),
nqp::unless(
$first,
nqp::stmts(
($first = nqp::shift($parts)),
($name = nqp::join("::",$parts)),
)
)
)
),
(my Mu $thing := nqp::if(
$root.EXISTS-KEY($first),
$root.AT-KEY($first),
nqp::if(
GLOBAL::.EXISTS-KEY($first),
GLOBAL::.AT-KEY($first),
X::NoSuchSymbol.new(symbol => $name).fail
)
)),
nqp::while(
nqp::elems($parts),
nqp::if(
$thing.WHO.EXISTS-KEY(my $part := nqp::shift($parts)),
($thing := $thing.WHO.AT-KEY($part)),
X::NoSuchSymbol.new(symbol => $name).fail
)
),
$thing
),
X::NoSuchSymbol.new(symbol => $name).fail
)
}

sub REQUIRE_IMPORT($compunit, *@syms) {
Expand Down

0 comments on commit 939d273

Please sign in to comment.