Subject: | Patch to avoid modifying @_ unnecessarily |
Hi, I just finished applying optimizations similar to this to CPANPLUS,
and noticed that, for instance, 35% of the time spent in catdir() is in
"my $self = shift". The attached patch modifies several routines that
need not modify @_ to avoid doing so; this provides a performance
improvement when many thousands of calls are made (something I've
actually encountered recently). Tested under OS X (I don't have VMS
access, sorry).
- R.
diff -purd PathTools-3.01.orig/lib/File/Spec/Cygwin.pm PathTools-3.01/lib/File/Spec/Cygwin.pm
--- PathTools-3.01.orig/lib/File/Spec/Cygwin.pm Mon Sep 6 20:37:47 2004
+++ PathTools-3.01/lib/File/Spec/Cygwin.pm Tue Dec 7 02:03:47 2004
@@ -76,8 +76,7 @@ variables are tainted, they are not used
my $tmpdir;
sub tmpdir {
return $tmpdir if defined $tmpdir;
- my $self = shift;
- $tmpdir = $self->_tmpdir( $ENV{TMPDIR}, "/tmp", 'C:/temp' );
+ $tmpdir = $_[0]->_tmpdir( $ENV{TMPDIR}, "/tmp", 'C:/temp' );
}
=back
diff -purd PathTools-3.01.orig/lib/File/Spec/Mac.pm PathTools-3.01/lib/File/Spec/Mac.pm
--- PathTools-3.01.orig/lib/File/Spec/Mac.pm Mon Sep 6 20:37:47 2004
+++ PathTools-3.01/lib/File/Spec/Mac.pm Tue Dec 7 02:05:21 2004
@@ -373,8 +373,7 @@ directory on your startup volume.
my $tmpdir;
sub tmpdir {
return $tmpdir if defined $tmpdir;
- my $self = shift;
- $tmpdir = $self->_tmpdir( $ENV{TMPDIR} );
+ $tmpdir = $_[0]->_tmpdir( $ENV{TMPDIR} );
}
=item updir
@@ -642,7 +641,7 @@ Based on code written by Shigio Yamaguch
# maybe this should be done in canonpath() ?
sub _resolve_updirs {
- my $path = shift @_;
+ my $path = $_[0];
my $proceed;
# resolve any updirs, e.g. "HD:tmp::file" -> "HD:file"
diff -purd PathTools-3.01.orig/lib/File/Spec/OS2.pm PathTools-3.01/lib/File/Spec/OS2.pm
--- PathTools-3.01.orig/lib/File/Spec/OS2.pm Mon Sep 6 20:37:47 2004
+++ PathTools-3.01/lib/File/Spec/OS2.pm Tue Dec 7 02:06:05 2004
@@ -37,21 +37,19 @@ sub _cwd {
my $tmpdir;
sub tmpdir {
return $tmpdir if defined $tmpdir;
- my $self = shift;
- $tmpdir = $self->_tmpdir( @ENV{qw(TMPDIR TEMP TMP)},
+ $tmpdir = $_[0]->_tmpdir( @ENV{qw(TMPDIR TEMP TMP)},
'/tmp',
'/' );
}
sub catdir {
- my $self = shift;
- my @args = @_;
+ my @args = @_[1..$#_];
foreach (@args) {
tr[\\][/];
# append a backslash to each argument unless it has one there
$_ .= "/" unless m{/$};
}
- return $self->canonpath(join('', @args));
+ return $_[0]->canonpath(join('', @args));
}
sub canonpath {
diff -purd PathTools-3.01.orig/lib/File/Spec/Unix.pm PathTools-3.01/lib/File/Spec/Unix.pm
--- PathTools-3.01.orig/lib/File/Spec/Unix.pm Mon Sep 6 20:37:47 2004
+++ PathTools-3.01/lib/File/Spec/Unix.pm Tue Dec 7 02:10:51 2004
@@ -68,9 +68,7 @@ trailing slash :-)
=cut
sub catdir {
- my $self = shift;
-
- $self->canonpath(join('/', @_, '')); # '' because need a trailing '/'
+ $_[0]->canonpath(join('/', @_[1..$#_], '')); # '' because need a trailing '/'
}
=item catfile
@@ -151,8 +149,7 @@ sub _tmpdir {
sub tmpdir {
return $tmpdir if defined $tmpdir;
- my $self = shift;
- $tmpdir = $self->_tmpdir( $ENV{TMPDIR}, "/tmp" );
+ $tmpdir = $_[0]->_tmpdir( $ENV{TMPDIR}, "/tmp" );
}
=item updir
@@ -171,8 +168,7 @@ directory. (Does not strip symlinks, onl
=cut
sub no_upwards {
- my $self = shift;
- return grep(!/^\.{1,2}\Z(?!\n)/s, @_);
+ return grep(!/^\.{1,2}\Z(?!\n)/s, @_[1..$#_]);
}
=item case_tolerant
@@ -219,8 +215,7 @@ join is the same as catfile.
=cut
sub join {
- my $self = shift;
- return $self->catfile(@_);
+ return $_[0]->catfile(@_[1..$#_]);
}
=item splitpath
diff -purd PathTools-3.01.orig/lib/File/Spec/VMS.pm PathTools-3.01/lib/File/Spec/VMS.pm
--- PathTools-3.01.orig/lib/File/Spec/VMS.pm Mon Sep 6 20:37:47 2004
+++ PathTools-3.01/lib/File/Spec/VMS.pm Tue Dec 7 02:07:23 2004
@@ -297,8 +297,7 @@ is tainted, it is not used.
my $tmpdir;
sub tmpdir {
return $tmpdir if defined $tmpdir;
- my $self = shift;
- $tmpdir = $self->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
+ $tmpdir = $_[0]->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
}
=item updir (override)
diff -purd PathTools-3.01.orig/lib/File/Spec/Win32.pm PathTools-3.01/lib/File/Spec/Win32.pm
--- PathTools-3.01.orig/lib/File/Spec/Win32.pm Mon Sep 6 20:37:47 2004
+++ PathTools-3.01/lib/File/Spec/Win32.pm Tue Dec 7 02:07:53 2004
@@ -59,8 +59,7 @@ variables are tainted, they are not used
my $tmpdir;
sub tmpdir {
return $tmpdir if defined $tmpdir;
- my $self = shift;
- $tmpdir = $self->_tmpdir( @ENV{qw(TMPDIR TEMP TMP)},
+ $tmpdir = $_[0]->_tmpdir( @ENV{qw(TMPDIR TEMP TMP)},
'SYS:/temp',
'C:/temp',
'/tmp',
@@ -93,14 +92,13 @@ sub catfile {
}
sub catdir {
- my $self = shift;
- my @args = @_;
+ my @args = @_[1..$#_];
foreach (@args) {
tr[/][\\];
# append a backslash to each argument unless it has one there
$_ .= "\\" unless m{\\$};
}
- return $self->canonpath(join('', @args));
+ return $_[0]->canonpath(join('', @args));
}
sub path {