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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ævar Arnfjörð Bjarmason committed
May 28, 2021 at 11:23 UTC
5a544a4e11e2a08a813215c1b9cc80cc1555c7c2
1 file changed
+16
-16
perl/Git.pm
+16
-16
@@ -11,9 +11,6 @@ use 5.008;
11
use strict;
12
use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : ();
13
14
-use File::Temp ();
15
-use File::Spec ();
16
-
14
BEGIN {
15
16
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
@@ -103,12 +100,9 @@ increase notwithstanding).
100
=cut
101
102
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 }
105
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);
106
}
107
108
@@ -191,13 +185,15 @@ sub repository {
185
$dir = undef;
186
};
187
188
+ require Cwd;
189
if ($dir) {
190
+ require File::Spec;
191
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);
193
194
# If --git-dir went ok, this shouldn't die either.
195
my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
200
- $dir = abs_path($opts{Directory}) . '/';
196
+ $dir = Cwd::abs_path($opts{Directory}) . '/';
197
if ($prefix) {
198
if (substr($dir, -length($prefix)) ne $prefix) {
199
throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix");
@@ -223,7 +219,7 @@ sub repository {
219
throw Error::Simple("fatal: Not a git repository: $dir");
220
}
221
226
- $opts{Repository} = abs_path($dir);
222
+ $opts{Repository} = Cwd::abs_path($dir);
223
}
224
225
delete $opts{Directory};
@@ -408,10 +404,12 @@ sub command_bidi_pipe {
404
my $cwd_save = undef;
405
if ($self) {
406
shift;
411
- $cwd_save = cwd();
407
+ require Cwd;
408
+ $cwd_save = Cwd::cwd();
409
_setup_git_cmd_env($self);
410
}
414
- $pid = open2($in, $out, 'git', @_);
411
+ require IPC::Open2;
412
+ $pid = IPC::Open2::open2($in, $out, 'git', @_);
413
chdir($cwd_save) if $cwd_save;
414
return ($pid, $in, $out, join(' ', @_));
415
}
@@ -538,7 +536,8 @@ sub get_tz_offset {
536
my $t = shift || time;
537
my @t = localtime($t);
538
$t[5] += 1900;
541
- my $gm = timegm(@t);
539
+ require Time::Local;
540
+ my $gm = Time::Local::timegm(@t);
541
my $sign = qw( + + - )[ $gm <=> $t ];
542
return sprintf("%s%02d%02d", $sign, (gmtime(abs($t - $gm)))[2,1]);
543
}
@@ -1340,6 +1339,7 @@ sub _temp_cache {
1339
my $n = $name;
1340
$n =~ s/\W/_/g; # no strange chars
1341
1342
+ require File::Temp;
1343
($$temp_fd, $fname) = File::Temp::tempfile(
1344
"Git_${n}_XXXXXX", UNLINK => 1, DIR => $tmpdir,
1345
) or throw Error::Simple("couldn't open new temp file");
@@ -1362,9 +1362,9 @@ sub temp_reset {
1362
1363
truncate $temp_fd, 0
1364
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())
1366
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
1368
or throw Error::Simple("expected file position to be reset");
1369
}
1370