Skip to content

Commit 5a544a4

Browse files
avargitster
authored andcommitted
perl: lazily load some common Git.pm setup code
Instead of unconditionally requiring modules such as File::Spec, let's only load them when needed. This speeds up code that only needs a subset of the features Git.pm provides. This brings a plain invocation of "git send-email" down from 52/37 loaded modules under NO_GETTEXT=[|Y] to 39/18, and it now takes ~60-~70ms instead of ~80-~90ms. The runtime of t9001-send-email.sh test is down to ~13s from ~15s. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f4dc943 commit 5a544a4

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

perl/Git.pm

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ use 5.008;
1111
use strict;
1212
use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : ();
1313

14-
use File::Temp ();
15-
use File::Spec ();
16-
1714
BEGIN {
1815

1916
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
@@ -103,12 +100,9 @@ increase notwithstanding).
103100
=cut
104101

105102

106-
use Carp qw(carp croak); # but croak is bad - throw instead
103+
sub carp { require Carp; goto &Carp::carp }
104+
sub croak { require Carp; goto &Carp::croak }
107105
use Git::LoadCPAN::Error qw(:try);
108-
use Cwd qw(abs_path cwd);
109-
use IPC::Open2 qw(open2);
110-
use Fcntl qw(SEEK_SET SEEK_CUR);
111-
use Time::Local qw(timegm);
112106
}
113107

114108

@@ -191,13 +185,15 @@ sub repository {
191185
$dir = undef;
192186
};
193187

188+
require Cwd;
194189
if ($dir) {
190+
require File::Spec;
195191
File::Spec->file_name_is_absolute($dir) or $dir = $opts{Directory} . '/' . $dir;
196-
$opts{Repository} = abs_path($dir);
192+
$opts{Repository} = Cwd::abs_path($dir);
197193

198194
# If --git-dir went ok, this shouldn't die either.
199195
my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
200-
$dir = abs_path($opts{Directory}) . '/';
196+
$dir = Cwd::abs_path($opts{Directory}) . '/';
201197
if ($prefix) {
202198
if (substr($dir, -length($prefix)) ne $prefix) {
203199
throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix");
@@ -223,7 +219,7 @@ sub repository {
223219
throw Error::Simple("fatal: Not a git repository: $dir");
224220
}
225221

226-
$opts{Repository} = abs_path($dir);
222+
$opts{Repository} = Cwd::abs_path($dir);
227223
}
228224

229225
delete $opts{Directory};
@@ -408,10 +404,12 @@ sub command_bidi_pipe {
408404
my $cwd_save = undef;
409405
if ($self) {
410406
shift;
411-
$cwd_save = cwd();
407+
require Cwd;
408+
$cwd_save = Cwd::cwd();
412409
_setup_git_cmd_env($self);
413410
}
414-
$pid = open2($in, $out, 'git', @_);
411+
require IPC::Open2;
412+
$pid = IPC::Open2::open2($in, $out, 'git', @_);
415413
chdir($cwd_save) if $cwd_save;
416414
return ($pid, $in, $out, join(' ', @_));
417415
}
@@ -538,7 +536,8 @@ sub get_tz_offset {
538536
my $t = shift || time;
539537
my @t = localtime($t);
540538
$t[5] += 1900;
541-
my $gm = timegm(@t);
539+
require Time::Local;
540+
my $gm = Time::Local::timegm(@t);
542541
my $sign = qw( + + - )[ $gm <=> $t ];
543542
return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
544543
}
@@ -1340,6 +1339,7 @@ sub _temp_cache {
13401339
my $n = $name;
13411340
$n =~ s/\W/_/g; # no strange chars
13421341

1342+
require File::Temp;
13431343
($$temp_fd, $fname) = File::Temp::tempfile(
13441344
"Git_${n}_XXXXXX", UNLINK => 1, DIR => $tmpdir,
13451345
) or throw Error::Simple("couldn't open new temp file");
@@ -1362,9 +1362,9 @@ sub temp_reset {
13621362

13631363
truncate $temp_fd, 0
13641364
or throw Error::Simple("couldn't truncate file");
1365-
sysseek($temp_fd, 0, SEEK_SET) and seek($temp_fd, 0, SEEK_SET)
1365+
sysseek($temp_fd, 0, Fcntl::SEEK_SET()) and seek($temp_fd, 0, Fcntl::SEEK_SET())
13661366
or throw Error::Simple("couldn't seek to beginning of file");
1367-
sysseek($temp_fd, 0, SEEK_CUR) == 0 and tell($temp_fd) == 0
1367+
sysseek($temp_fd, 0, Fcntl::SEEK_CUR()) == 0 and tell($temp_fd) == 0
13681368
or throw Error::Simple("expected file position to be reset");
13691369
}
13701370

0 commit comments

Comments
 (0)