From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id CEBCA1F654 for ; Thu, 6 Jun 2024 07:44:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1717659857; bh=SiFrrO5ivAznWtLeYYIHwAS5K3KlSMxTWYoqenEtU9c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=SIJgpxDn+7bP3b+jZbHtinfE/pbrzfGcqocY7XKgjxmVP7h8pjoritLpb0LAHCaDs DgGCGr5LB/obFlmnt7gdofJ2gWHEuapWd6j8OgZe5IqttrCDNh9zp4jKmapxHfRIvt 9RdXkQtF7xwqFarBcMa/D3fLMqAigrUtvjLkq46g= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 4/5] treewide: use cached git executable lookup Date: Thu, 6 Jun 2024 07:44:15 +0000 Message-ID: <20240606074416.3900983-5-e@80x24.org> In-Reply-To: <20240606074416.3900983-1-e@80x24.org> References: <20240606074416.3900983-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Repeated stat(2) syscalls are more expensive nowadays due to CPU vulnerability mitigations and this change also allows bypassing some heap allocations done by Perl. --- lib/PublicInbox/Admin.pm | 3 ++- lib/PublicInbox/Config.pm | 2 +- lib/PublicInbox/GitCredential.pm | 3 ++- lib/PublicInbox/Import.pm | 3 ++- lib/PublicInbox/LEI.pm | 3 ++- lib/PublicInbox/LeiBlob.pm | 4 ++-- lib/PublicInbox/LeiConfig.pm | 3 ++- lib/PublicInbox/LeiInput.pm | 4 +++- lib/PublicInbox/LeiMailDiff.pm | 3 ++- lib/PublicInbox/LeiSavedSearch.pm | 4 ++-- lib/PublicInbox/LeiViewText.pm | 3 ++- lib/PublicInbox/MailDiff.pm | 3 ++- lib/PublicInbox/MultiGit.pm | 6 ++++-- lib/PublicInbox/WwwCoderepo.pm | 5 +++-- 14 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/PublicInbox/Admin.pm b/lib/PublicInbox/Admin.pm index a2045b37..bb5d3653 100644 --- a/lib/PublicInbox/Admin.pm +++ b/lib/PublicInbox/Admin.pm @@ -11,6 +11,7 @@ use PublicInbox::Config; use PublicInbox::Inbox; use PublicInbox::Spawn qw(run_qx); use PublicInbox::Eml; +use PublicInbox::Git qw(git_exe); *rel2abs_collapsed = \&PublicInbox::Config::rel2abs_collapsed; sub setup_signals { @@ -77,7 +78,7 @@ sub resolve_git_dir { my $env; defined($pwd) && substr($cd // '/', 0, 1) ne '/' and $env->{PWD} = "$pwd/$cd"; - my $cmd = [ qw(git rev-parse --git-dir) ]; + my $cmd = [ git_exe, qw(rev-parse --git-dir) ]; my $dir = run_qx($cmd, $env, { -C => $cd }); die "error in @$cmd (cwd:${\($cd // '.')}): $?\n" if $?; chomp $dir; diff --git a/lib/PublicInbox/Config.pm b/lib/PublicInbox/Config.pm index e1843912..998fc25e 100644 --- a/lib/PublicInbox/Config.pm +++ b/lib/PublicInbox/Config.pm @@ -632,7 +632,7 @@ sub urlmatch { } elsif (($? >> 8) != 1) { $urlmatch_broken = 1; } elsif ($try_git) { # n.b. this takes cwd into account - $val = run_qx([qw(git config), @bool, + $val = run_qx([$cmd->[0], 'config', @bool, qw(-z --get-urlmatch), $key, $url]); undef $val if $?; } diff --git a/lib/PublicInbox/GitCredential.pm b/lib/PublicInbox/GitCredential.pm index bb225ff3..cf5a2213 100644 --- a/lib/PublicInbox/GitCredential.pm +++ b/lib/PublicInbox/GitCredential.pm @@ -4,13 +4,14 @@ # git-credential wrapper with built-in .netrc fallback package PublicInbox::GitCredential; use v5.12; +use PublicInbox::Git qw(git_exe); use PublicInbox::Spawn qw(popen_rd); use autodie qw(close pipe); sub run ($$;$) { my ($self, $op, $lei) = @_; my ($in_r, $in_w, $out_r); - my $cmd = [ qw(git credential), $op ]; + my $cmd = [ git_exe, 'credential', $op ]; pipe($in_r, $in_w); if ($lei) { # we'll die if disconnected: pipe($out_r, my $out_w); diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index fefc282a..2e193d46 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -8,6 +8,7 @@ package PublicInbox::Import; use v5.12; use parent qw(PublicInbox::Lock); +use PublicInbox::Git qw(git_exe); use PublicInbox::Spawn qw(run_die run_qx spawn); use PublicInbox::MID qw(mids mid2path); use PublicInbox::Address; @@ -24,7 +25,7 @@ use PublicInbox::IO qw(read_all); sub default_branch () { state $default_branch = do { - my $h = run_qx([qw(git config --global init.defaultBranch)], + my $h = run_qx([git_exe,qw(config --global init.defaultBranch)], { GIT_CONFIG => undef }); chomp $h; $h eq '' ? 'refs/heads/master' : "refs/heads/$h"; diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 309d290d..c5146428 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -22,6 +22,7 @@ use PublicInbox::Syscall qw(EPOLLIN); use PublicInbox::Spawn qw(run_wait popen_rd run_qx); use PublicInbox::Lock; use PublicInbox::Eml; +use PublicInbox::Git qw(git_exe); use PublicInbox::Import; use PublicInbox::ContentHash qw(git_sha); use PublicInbox::OnDestroy; @@ -1098,7 +1099,7 @@ sub path_to_fd { # caller needs to "-t $self->{1}" to check if tty sub start_pager { my ($self, $new_env) = @_; - chomp(my $pager = run_qx([qw(git var GIT_PAGER)])); + chomp(my $pager = run_qx([git_exe, qw(var GIT_PAGER)])); warn "`git var PAGER' error: \$?=$?" if $?; return if $pager eq 'cat' || $pager eq ''; $new_env //= {}; diff --git a/lib/PublicInbox/LeiBlob.pm b/lib/PublicInbox/LeiBlob.pm index 7b2ea434..31936c36 100644 --- a/lib/PublicInbox/LeiBlob.pm +++ b/lib/PublicInbox/LeiBlob.pm @@ -10,14 +10,14 @@ use parent qw(PublicInbox::IPC); use PublicInbox::Spawn qw(run_wait run_qx which); use PublicInbox::DS; use PublicInbox::Eml; -use PublicInbox::Git; +use PublicInbox::Git qw(git_exe); use PublicInbox::IO qw(read_all); sub get_git_dir ($$) { my ($lei, $d) = @_; return $d if -d "$d/objects" && -d "$d/refs" && -e "$d/HEAD"; - my $cmd = [ qw(git rev-parse --git-dir) ]; + my $cmd = [ git_exe, qw(rev-parse --git-dir) ]; my $opt = { '-C' => $d }; if (defined($lei->{opt}->{cwd})) { # --cwd used, report errors $opt->{2} = $lei->{2}; diff --git a/lib/PublicInbox/LeiConfig.pm b/lib/PublicInbox/LeiConfig.pm index a50ff2b6..ae12249f 100644 --- a/lib/PublicInbox/LeiConfig.pm +++ b/lib/PublicInbox/LeiConfig.pm @@ -3,6 +3,7 @@ package PublicInbox::LeiConfig; # subclassed by LeiEditSearch use v5.12; use PublicInbox::PktOp; +use PublicInbox::Git qw(git_exe); use Fcntl qw(SEEK_SET); use autodie qw(open seek); use PublicInbox::IO qw(read_all); @@ -11,7 +12,7 @@ sub cfg_do_edit ($;$) { my ($self, $reason) = @_; my $lei = $self->{lei}; $lei->pgr_err($reason) if defined $reason; - my $cmd = [ qw(git config --edit -f), $self->{-f} ]; + my $cmd = [ git_exe, qw(config --edit -f), $self->{-f} ]; my $env = { GIT_CONFIG => $self->{-f} }; $self->cfg_edit_begin if $self->can('cfg_edit_begin'); # run in script/lei foreground diff --git a/lib/PublicInbox/LeiInput.pm b/lib/PublicInbox/LeiInput.pm index c388f7dc..0a6aba82 100644 --- a/lib/PublicInbox/LeiInput.pm +++ b/lib/PublicInbox/LeiInput.pm @@ -5,6 +5,7 @@ package PublicInbox::LeiInput; use v5.12; use PublicInbox::DS; +use PublicInbox::Git qw(git_exe); use PublicInbox::Spawn qw(which popen_rd); use PublicInbox::InboxWritable qw(eml_from_path); @@ -252,7 +253,8 @@ sub input_path_url { each_ibx_eml($self, $esrch, @args); } elsif ($self->{missing_ok} && !-e $input) { # don't ->fail if ($lei->{cmd} eq 'p2q') { - my $fp = [ qw(git format-patch --stdout -1), $input ]; + my $fp = [ git_exe, qw(format-patch --stdout -1), + $input ]; my $rdr = { 2 => $lei->{2} }; my $fh = popen_rd($fp, undef, $rdr); eval { $self->input_fh('eml', $fh, $input, @args) }; diff --git a/lib/PublicInbox/LeiMailDiff.pm b/lib/PublicInbox/LeiMailDiff.pm index af6ecf82..270de507 100644 --- a/lib/PublicInbox/LeiMailDiff.pm +++ b/lib/PublicInbox/LeiMailDiff.pm @@ -7,13 +7,14 @@ package PublicInbox::LeiMailDiff; use v5.12; use parent qw(PublicInbox::IPC PublicInbox::LeiInput PublicInbox::MailDiff); use PublicInbox::Spawn qw(run_wait); +use PublicInbox::Git qw(git_exe); require PublicInbox::LeiRediff; sub diff_a ($$) { my ($self, $eml) = @_; my $dir = "$self->{tmp}/N".(++$self->{nr}); $self->dump_eml($dir, $eml); - my $cmd = [ qw(git diff --no-index) ]; + my $cmd = [ git_exe, qw(diff --no-index) ]; my $lei = $self->{lei}; PublicInbox::LeiRediff::_lei_diff_prepare($lei, $cmd); push @$cmd, qw(-- a), "N$self->{nr}"; diff --git a/lib/PublicInbox/LeiSavedSearch.pm b/lib/PublicInbox/LeiSavedSearch.pm index 1c8a1f73..c27b6f86 100644 --- a/lib/PublicInbox/LeiSavedSearch.pm +++ b/lib/PublicInbox/LeiSavedSearch.pm @@ -5,7 +5,7 @@ package PublicInbox::LeiSavedSearch; use v5.12; use parent qw(PublicInbox::Lock); -use PublicInbox::Git; +use PublicInbox::Git qw(git_exe); use PublicInbox::OverIdx; use PublicInbox::LeiSearch; use PublicInbox::Config; @@ -176,7 +176,7 @@ sub description { $_[0]->{qstr} } # for WWW sub cfg_set { # called by LeiXSearch my ($self, @args) = @_; my $lk = $self->lock_for_scope; # git-config doesn't wait - run_die([qw(git config -f), $self->{'-f'}, @args]); + run_die([git_exe, qw(config -f), $self->{'-f'}, @args]); } # drop-in for LeiDedupe API diff --git a/lib/PublicInbox/LeiViewText.pm b/lib/PublicInbox/LeiViewText.pm index c7d72c71..6510b19e 100644 --- a/lib/PublicInbox/LeiViewText.pm +++ b/lib/PublicInbox/LeiViewText.pm @@ -12,6 +12,7 @@ use PublicInbox::View; use PublicInbox::Hval; use PublicInbox::ViewDiff; use PublicInbox::Spawn qw(popen_rd); +use PublicInbox::Git qw(git_exe); use Term::ANSIColor; use POSIX (); use PublicInbox::Address; @@ -72,7 +73,7 @@ sub new { my $self = bless { %{$lei->{opt}}, -colored => \&uncolored }, $cls; $self->{-quote_reply} = 1 if $fmt eq 'reply'; return $self unless $self->{color} //= -t $lei->{1}; - my @cmd = qw(git config -z --includes -l); # reuse normal git config + my @cmd = (git_exe, qw(config -z --includes -l)); # reuse normal git cfg my $r = popen_rd(\@cmd, undef, { 2 => $lei->{2} }); my $cfg = PublicInbox::Config::config_fh_parse($r, "\0", "\n"); if (!$r->close) { diff --git a/lib/PublicInbox/MailDiff.pm b/lib/PublicInbox/MailDiff.pm index 125360fe..ce268bfb 100644 --- a/lib/PublicInbox/MailDiff.pm +++ b/lib/PublicInbox/MailDiff.pm @@ -7,6 +7,7 @@ use PublicInbox::ContentHash qw(content_digest); use PublicInbox::MsgIter qw(msg_part_text); use PublicInbox::ViewDiff qw(flush_diff); use PublicInbox::GitAsyncCat; +use PublicInbox::Git qw(git_exe); use PublicInbox::ContentDigestDbg; use PublicInbox::Qspawn; use PublicInbox::IO qw(write_file); @@ -81,7 +82,7 @@ sub do_diff { my $n = 'N'.(++$self->{nr}); my $dir = "$self->{tmp}/$n"; $self->dump_eml($dir, $eml); - my $cmd = [ qw(git diff --no-index --no-color -- a), $n ]; + my $cmd = [ git_exe, qw(diff --no-index --no-color -- a), $n ]; my $opt = { -C => "$self->{tmp}", quiet => 1 }; my $qsp = PublicInbox::Qspawn->new($cmd, undef, $opt); $qsp->psgi_qx($self->{ctx}->{env}, undef, \&emit_msg_diff, $self); diff --git a/lib/PublicInbox/MultiGit.pm b/lib/PublicInbox/MultiGit.pm index b7691806..32bb3588 100644 --- a/lib/PublicInbox/MultiGit.pm +++ b/lib/PublicInbox/MultiGit.pm @@ -7,6 +7,7 @@ use strict; use v5.10.1; use PublicInbox::Spawn qw(run_die run_qx); use PublicInbox::Import; +use PublicInbox::Git qw(git_exe); use File::Temp 0.19; use List::Util qw(max); use PublicInbox::IO qw(read_all); @@ -110,11 +111,12 @@ sub epoch_cfg_set { my ($self, $epoch_nr) = @_; my $f = epoch_dir($self)."/$epoch_nr.git/config"; my $v = "../../$self->{all}/config"; + my @cmd = (git_exe, qw(config -f), $f, 'include.path'); if (-r $f) { - chomp(my $x = run_qx([qw(git config -f), $f, 'include.path'])); + chomp(my $x = run_qx(\@cmd)); return if $x eq $v; } - run_die([qw(git config -f), $f, 'include.path', $v ]); + run_die [@cmd, $v]; } sub add_epoch { diff --git a/lib/PublicInbox/WwwCoderepo.pm b/lib/PublicInbox/WwwCoderepo.pm index 8d825284..5e086fee 100644 --- a/lib/PublicInbox/WwwCoderepo.pm +++ b/lib/PublicInbox/WwwCoderepo.pm @@ -24,8 +24,9 @@ use PublicInbox::OnDestroy; use URI::Escape qw(uri_escape_utf8); use File::Spec; use autodie qw(fcntl open); +use PublicInbox::Git qw(git_exe); -my @EACH_REF = (qw(git for-each-ref --sort=-creatordate), +my @EACH_REF = (git_exe, qw(for-each-ref --sort=-creatordate), "--format=%(HEAD)%00".join('%00', map { "%($_)" } qw(objectname refname:short subject creatordate:short))); my $HEADS_CMD = <<''; @@ -249,7 +250,7 @@ sub summary ($$) { my $qsp_err = \($ctx->{-qsp_err} = ''); my %opt = (quiet => 1, 2 => $ctx->{wcr}->{log_fh}); my %env = (GIT_DIR => $ctx->{git}->{git_dir}); - my @log = (qw(git log), "-$nl", '--pretty=format:%d %H %h %cs %s'); + my @log = (git_exe, 'log', "-$nl", '--pretty=format:%d %H %h %cs %s'); push(@log, $tip) if defined $tip; # limit scope for MockHTTP test (t/solver_git.t)