Skip to content

Commit

Permalink
Favour substr(foo,...) over foo.substr(...)
Browse files Browse the repository at this point in the history
as substr() is 13% faster than .substr()
  • Loading branch information
lizmat committed Jan 16, 2015
1 parent 1b417d8 commit 46e92b2
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/core/Backtrace.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ my class Backtrace::Frame {
method is-hidden(Backtrace::Frame:D:) { $!code.?is_hidden_from_backtrace }
method is-routine(Backtrace::Frame:D:) { nqp::istype($!code,Routine) }
method is-setting(Backtrace::Frame:D:) {
$!file.chars > 12 && $!file.substr(*-12) eq 'CORE.setting'
$!file.chars > 12 && substr($!file,*-12) eq 'CORE.setting'
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ my class Backtrace is List {
$file eq 'gen/moar/stage2/NQPHLL.nqp' ||
$file eq 'gen\\moar\\stage2\\NQPHLL.nqp';
my $subname = nqp::p6box_s(nqp::getcodename($sub));
$subname = '<anon>' if $subname.substr(0, 6) eq '_block';
$subname = '<anon>' if substr($subname,0, 6) eq '_block';
$new.push: Backtrace::Frame.new(
:line($line.Int),
:$file,
Expand Down
2 changes: 1 addition & 1 deletion src/core/Block.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ my class Block { # declared in BOOTSTRAP

multi method perl(Block:D:) {
my $perl = '-> ';
$perl ~= self.signature().perl.substr(1); # lose colon prefix
$perl ~= substr(self.signature().perl,1); # lose colon prefix
$perl ~= ' { #`(' ~ self.WHICH ~ ') ... }';
$perl
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/Buf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ my role Blob[::T = uint8] does Positional[T] does Stringy is repr('VMArray') is
my @bytes = self.list;
my @fields;
for $template.comb(/<[a..zA..Z]>[\d+|'*']?/) -> $unit {
my $directive = $unit.substr(0, 1);
my $amount = $unit.substr(1);
my $directive = substr($unit,0,1);
my $amount = substr($unit,1);
my $pa = $amount eq '' ?? 1 !!
$amount eq '*' ?? @bytes.elems !! +$amount;

Expand Down Expand Up @@ -286,8 +286,8 @@ constant buf64 = Buf[uint64];
multi sub pack(Str $template, *@items) {
my @bytes;
for $template.comb(/<[a..zA..Z]>[\d+|'*']?/) -> $unit {
my $directive = $unit.substr(0, 1);
my $amount = $unit.substr(1);
my $directive = substr($unit,0,1);
my $amount = substr($unit,1);

given $directive {
when 'A' {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1216,9 +1216,9 @@ my class X::Str::Numeric is Exception {
method source-indicator {
constant marker = chr(0x23CF);
join '', "in '",
$.source.substr(0, $.pos),
substr($.source,0, $.pos),
marker,
$.source.substr($.pos),
substr($.source,$.pos),
"' (indicated by ",
marker,
")",
Expand Down
10 changes: 5 additions & 5 deletions src/core/IO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ my role IO {
# optimizing targets.

sub MAKE-ABSOLUTE-PATH($path,$abspath) {
if $path.ord == 47 { # 4x faster $path.substr(0,1) eq "/"
if $path.ord == 47 { # 4x faster substr($path,0,1) eq "/"
return $path;
}
elsif $path.substr(1,1) eq ':' { # assume C: something
if $path.substr(2,1) eq "/" { # assume C:/ like prefix
elsif substr($path,1,1) eq ':' { # assume C: something
if substr($path,2,1) eq "/" { # assume C:/ like prefix
return $path;
}
elsif $abspath.substr(0,2) ne $path.substr(0,2) {
elsif substr($abspath,0,2) ne substr($path,0,2) {
die "Can not set relative dir from different roots";
}
else {
return $abspath ~ $path.substr(2);
return $abspath ~ substr($path,2);
}
}
else { # assume relative path
Expand Down
10 changes: 5 additions & 5 deletions src/core/IO/Path.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ my class IO::Path is Cool {
}

method abspath() {
$!abspath //= $!path.substr(0,1) eq '-'
$!abspath //= substr($!path,0,1) eq '-'
?? ''
!! $!SPEC.rel2abs($!path,$!CWD);
}
Expand Down Expand Up @@ -366,10 +366,10 @@ my class IO::Path is Cool {
$Str
?? $absolute
?? take $abspath-sep ~ $elem
!! take ($abspath-sep ~ $elem).substr($cwd_chars + 1)
!! take substr($abspath-sep ~ $elem,$cwd_chars + 1)
!! $absolute
?? take IO::Path.new-from-absolute-path($abspath-sep ~ $elem,:$!SPEC,:$CWD)
!! take ($abspath-sep ~ $elem).substr($cwd_chars + 1).IO(:$!SPEC,:$CWD);
!! take substr($abspath-sep ~ $elem,$cwd_chars + 1).IO(:$!SPEC,:$CWD);
}
}
#?endif
Expand All @@ -391,10 +391,10 @@ my class IO::Path is Cool {
#?endif
$Str
?? !$absolute && !$.is-absolute
?? take $elem.substr($cwd_chars + 1)
?? take substr($elem,$cwd_chars + 1)
!! take $elem
!! !$absolute && !$.is-absolute
?? take $elem.substr($cwd_chars + 1).IO(:$!SPEC,:$CWD)
?? take substr($elem,$cwd_chars + 1).IO(:$!SPEC,:$CWD)
!! take IO::Path.new-from-absolute-path($elem,:$!SPEC,:$CWD);
}
#?if moar
Expand Down
6 changes: 3 additions & 3 deletions src/core/IO/Socket.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ my role IO::Socket does IO {

my $rec;
if $!buffer.chars > $chars {
$rec = $!buffer.substr(0, $chars);
$!buffer = $!buffer.substr($chars);
$rec = substr($!buffer,0,$chars);
$!buffer = substr($!buffer,$chars);
} else {
$rec = $!buffer;
$!buffer = '';
Expand Down Expand Up @@ -59,7 +59,7 @@ my role IO::Socket does IO {
} else {
my $rec = nqp::decode(nqp::decont($!buffer), 'utf8');
if $rec.chars > $chars {
$rec = $rec.substr(0, $chars);
$rec = substr($rec,0,$chars);
my $used = $rec.encode('utf8').elems;
$!buffer = $!buffer.subbuf($used)
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/core/IO/Spec/Unix.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ my class IO::Spec::Unix is IO::Spec {
my int $index = nqp::rindex($str,'/');
nqp::p6bool($index == -1)
?? path
!! path.substr( nqp::box_i($index + 1,Int) );
!! substr(path,nqp::box_i($index + 1,Int) );
}

method extension(\path) {
my str $str = nqp::unbox_s(path);
my int $index = nqp::rindex($str,'.');
nqp::p6bool($index == -1)
?? ''
!! path.substr( nqp::box_i($index + 1,Int) );
!! substr(path,nqp::box_i($index + 1,Int) );
}

method tmpdir {
Expand Down Expand Up @@ -111,8 +111,8 @@ my class IO::Spec::Unix is IO::Spec {
method catpath( $, $dirname, $file ) {
$dirname ne ''
&& $file ne ''
&& $dirname.substr( *-1 ) ne '/'
&& $file.substr( 0, 1 ) ne '/'
&& substr($dirname, *-1 ) ne '/'
&& substr($file, 0, 1 ) ne '/'
?? $dirname ~ '/' ~ $file
!! $dirname ~ $file
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/IO/Spec/Win32.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ my class IO::Spec::Win32 is IO::Spec::Unix {
nqp::p6bool($indexf == -1 && $indexb == -1)
?? path
!! $indexf > $indexb
?? path.substr( nqp::box_i($indexf + 1,Int) )
!! path.substr( nqp::box_i($indexb + 1,Int) );
?? substr(path,nqp::box_i($indexf + 1,Int) )
!! substr(path,nqp::box_i($indexb + 1,Int) );
}

method tmpdir {
Expand Down
2 changes: 1 addition & 1 deletion src/core/Kernel.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Kernel does Systemic {
}

for Signal.^enum_value_list -> $signal {
my $name = $signal.key.substr(3);
my $name = substr($signal.key,3);
if @names.first-index( * eq $name ) -> $index {
@!signals[$index] = $signal;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Main.pm
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ my sub MAIN_HELPER($retval = 0) is hidden_from_backtrace {
else {
my $constraints = $param.constraint_list.map(*.gist).join(' ');
my $simple-const = $constraints && $constraints !~~ /^_block/;
$argument = $param.name ?? '<' ~ $param.name.substr(1) ~ '>' !!
$argument = $param.name ?? '<' ~ substr($param.name,1) ~ '>' !!
$simple-const ?? $constraints !!
'<' ~ $param.type.^name ~ '>' ;

Expand Down
10 changes: 5 additions & 5 deletions src/core/Match.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ my class Match is Capture is Cool {
method ast(Match:D:) { $!made }

multi method Str(Match:D:) {
$!to > $!from ?? $!orig.substr($!from, $!to-$!from) !! ''
$!to > $!from ?? substr($!orig,$!from,$!to-$!from) !! ''
}
multi method Numeric(Match:D:) {
self.Str.Numeric
Expand All @@ -29,10 +29,10 @@ my class Match is Capture is Cool {
multi method ACCEPTS(Match:D: Any $) { self }

method prematch(Match:D:) {
$!orig.substr(0, $!from);
substr($!orig,0,$!from);
}
method postmatch(Match:D:) {
$!orig.substr($!to)
substr($!orig,$!to)
}

method caps(Match:D:) {
Expand All @@ -52,12 +52,12 @@ my class Match is Capture is Cool {
gather {
for self.caps {
if .value.from > $prev {
take '~' => $!orig.substr($prev, .value.from - $prev)
take '~' => substr($!orig,$prev, .value.from - $prev)
}
take $_;
$prev = .value.to;
}
take '~' => $!orig.substr($prev, $!to - $prev) if $prev < $!to;
take '~' => substr($!orig,$prev, $!to - $prev) if $prev < $!to;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/Mu.pm
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ my class Mu { # declared in BOOTSTRAP
multi method perl(Mu:D:) {
my @attrs;
for self.^attributes().grep: { .has_accessor } -> $attr {
my $name := $attr.Str.substr(2);
my $name := substr($attr.Str,2);
@attrs.push: $name
~ ' => '
~ self."$name"().perl
Expand Down Expand Up @@ -547,7 +547,7 @@ my class Mu { # declared in BOOTSTRAP
nqp::bindattr($cloned, $package, $name, nqp::clone($attr_val.VAR))
if nqp::iscont($attr_val);
}
my $acc_name := $name.substr(2);
my $acc_name := substr($name,2);
if $attr.has-accessor && %twiddles.exists_key($acc_name) {
nqp::getattr($cloned, $package, $name) = %twiddles{$acc_name};
}
Expand All @@ -559,7 +559,7 @@ my class Mu { # declared in BOOTSTRAP
my %attrs;
for self.^attributes -> $attr {
if $attr.has-accessor {
my $name = $attr.name.substr(2);
my $name = substr($attr.name,2);
unless %attrs.exists_key($name) {
%attrs{$name} = self."$name"();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Routine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ my class Routine { # declared in BOOTSTRAP
if self.name() -> $n {
$perl ~= " $n";
}
$perl ~= ' ' ~ self.signature().perl.substr(1); # lose colon prefix
$perl ~= ' ' ~ substr(self.signature().perl,1); # lose colon prefix
$perl ~= ' { #`(' ~ self.WHICH ~ ') ... }';
$perl
}
Expand Down
22 changes: 11 additions & 11 deletions src/core/Str.pm
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ my class Str does Stringy { # declared in BOOTSTRAP
#?if parrot
my $icu = $*VM.config<has_icu>;
for ^self.chars -> $i {
my $ch = self.substr($i, 1);
my $ch = substr(self,$i, 1);
$result ~= %esc{$ch}
// ( ((!$icu && $ch.ord >= 256)
|| nqp::iscclass( nqp::const::CCLASS_PRINTING,
nqp::unbox_s($ch), 0))
#?endif
#?if !parrot
for ^self.chars -> $i {
my $ch = self.substr($i, 1);
my $ch = substr(self,$i, 1);
$result ~= %esc{$ch}
// (nqp::iscclass( nqp::const::CCLASS_PRINTING,
nqp::unbox_s($ch), 0)
Expand Down Expand Up @@ -652,7 +652,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
my $result = '';
for @matches -> $m {
try $caller_dollar_slash = $m if $SET_DOLLAR_SLASH;
$result ~= self.substr($prev, $m.from - $prev);
$result ~= substr(self,$prev, $m.from - $prev);

my $real_replacement = ~(nqp::istype($replacement,Callable)
?? ($replacement.count == 0 ?? $replacement() !! $replacement($m))
Expand All @@ -663,7 +663,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
$prev = $m.to;
}
my $last = @matches[@matches-1];
$result ~= self.substr($last.to);
$result ~= substr(self,$last.to);
$self = $result;
$global ?? (@matches,).list !! @matches[0];
}
Expand All @@ -683,7 +683,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
my $result = '';
for @matches -> $m {
try $caller_dollar_slash = $m if $SET_DOLLAR_SLASH;
$result ~= self.substr($prev, $m.from - $prev);
$result ~= substr(self,$prev, $m.from - $prev);

my $real_replacement = ~(nqp::istype($replacement,Callable)
?? ($replacement.count == 0 ?? $replacement() !! $replacement($m))
Expand All @@ -694,7 +694,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
$prev = $m.to;
}
my $last = @matches.pop;
$result ~= self.substr($last.to);
$result ~= substr(self,$last.to);
$result;
}

Expand Down Expand Up @@ -804,15 +804,15 @@ my class Str does Stringy { # declared in BOOTSTRAP
if ($all) {
my $elems = +@matches;
map {
my $value = self.substr($prev-pos, .from - $prev-pos);
my $value = substr(self,$prev-pos, .from - $prev-pos);
$prev-pos = .to;
# we don't want the dummy object
--$elems ?? ($value, $_) !! $value;
}, @matches;
}
else {
map {
my $value = self.substr($prev-pos, .from - $prev-pos);
my $value = substr(self,$prev-pos, .from - $prev-pos);
$prev-pos = .to;
$value;
}, @matches;
Expand Down Expand Up @@ -1092,7 +1092,7 @@ my class Str does Stringy { # declared in BOOTSTRAP

proto method increment_index(|) {*}
multi method increment_index(Regex $s) {
$!source.substr($!index) ~~ $s;
substr($!source,$!index) ~~ $s;
$!index = $!next_match + $/.chars;
}

Expand All @@ -1107,7 +1107,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
@!substitutions = @!substitutions.grep: {self.triage_substitution($_) }

$!unsubstituted_text # = nqp::substr(nqp::unbox_s($!source), $!index,
= $!source.substr($!index, $!next_match - $!index);
= substr($!source,$!index, $!next_match - $!index);
if defined $!next_substitution {
my $result = $!next_substitution.value;
$!substituted_text
Expand Down Expand Up @@ -1382,7 +1382,7 @@ multi sub UNBASE(Int:D $base, Cool:D $num) is hidden_from_backtrace {
X::Numeric::Confused.new(:what($num)).throw;
}
multi sub UNBASE(Int:D $base, Str:D $str) is hidden_from_backtrace {
my Str $prefix = $str.substr(0, 2);
my Str $prefix = substr($str,0, 2);
if $base <= 10 && $prefix eq any(<0x 0d 0o 0b>)
or $base <= 24 && $prefix eq any <0o 0x>
or $base <= 33 && $prefix eq '0x' {
Expand Down
2 changes: 1 addition & 1 deletion src/core/Version.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Version {
}
self.bless(
:parts(@parts),
:plus(?$s.chars && $s.substr(*-1) eq '+'),
:plus(?$s.chars && substr($s,*-1) eq '+'),
);
};

Expand Down
Loading

0 comments on commit 46e92b2

Please sign in to comment.