[PATCH v2 10/10] perl: nano-optimize by replacing Cwd::cwd() with Cwd::getcwd()
- From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
- Date
- May 20, 2021, 08:19 UTC
- Message-ID
- <patch-10.10-97455f993d5-20210520T081826Z-avarab@gmail.com>
- In-Reply-To
- <cover-00.10-00000000000-20210520T081826Z-avarab@gmail.com>
It has been pointed out[1] that cwd() invokes "pwd(1)" while getcwd() is a Perl-native XS function. For what we're using these for we can use getcwd().
The performance difference is miniscule, we're saving on the order of a millisecond or so, see [2] below for the benchmark. I don't think this matters in practice for optimizing git-send-email or perl execution (unlike the patches leading up to this one).
But let's do it regardless of that, if only so we don't have to think about this as a low-hanging fruit anymore.
1. https://lore.kernel.org/git/20210512180517.GA11354@dcvr/
2.
$ perl -MBenchmark=:all -MCwd -wE 'cmpthese(10000, { getcwd => sub { getcwd }, cwd => sub { cwd }, pwd => sub { system "pwd >/dev/null" }})'
(warning: too few iterations for a reliable count)
Rate pwd cwd getcwd
pwd 982/s -- -48% -100%
cwd 1890/s 92% -- -100%
getcwd 10000000000000000000/s 1018000000000000000% 529000000000000064% -Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> --- git-send-email.perl | 2 +- perl/Git.pm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/git-send-email.perl b/git-send-email.perl index 28b9a20a7f2..ff3008bfa34 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -1981,7 +1981,7 @@ sub validate_patch { require Cwd; my $target = Cwd::abs_path($fn); # The hook needs a correct cwd and GIT_DIR. - my $cwd_save = Cwd::cwd(); + my $cwd_save = Cwd::getcwd(); chdir($repo->wc_path() or $repo->repo_path()) or die("chdir: $!"); local $ENV{"GIT_DIR"} = $repo->repo_path(); diff --git a/perl/Git.pm b/perl/Git.pm index a9020d0d01f..358a5f5625e 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -405,7 +405,7 @@ sub command_bidi_pipe { if ($self) { shift; require Cwd; - $cwd_save = Cwd::cwd(); + $cwd_save = Cwd::getcwd(); _setup_git_cmd_env($self); } require IPC::Open2;
-- 2.32.0.rc0.405.g5d387561bb3