send-email: lazily shell out to "git var"

Optimize git-send-email by only shelling out to "git var" if we need
to. This is easily done by re-inventing our own small version of
perl's Memoize module.

I suppose I could just use Memoize itself, but in a subsequent patch
I'll be micro-optimizing send-email's use of dependencies. Using
Memoize is a measly extra 5-10 milliseconds, but as we'll see that'll
end up mattering for us in the end.

This brings the runtime of a plain "send-email" from around ~160-170ms
to ~140m-150ms. The runtime of the tests is around the same, or around
~20s.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2021-05-28 11:23:46 +02:00 committed by Junio C Hamano
parent 9264d29bf6
commit fef381e6dc

View File

@ -588,8 +588,18 @@ if (0) {
}
my ($repoauthor, $repocommitter);
($repoauthor) = Git::ident_person(@repo, 'author');
($repocommitter) = Git::ident_person(@repo, 'committer');
{
my %cache;
my ($author, $committer);
my $common = sub {
my ($what) = @_;
return $cache{$what} if exists $cache{$what};
($cache{$what}) = Git::ident_person(@repo, $what);
return $cache{$what};
};
$repoauthor = sub { $common->('author') };
$repocommitter = sub { $common->('committer') };
}
sub parse_address_line {
return map { $_->format } Mail::Address->parse($_[0]);
@ -777,7 +787,7 @@ if ($compose) {
or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
my $tpl_subject = $initial_subject || '';
my $tpl_in_reply_to = $initial_in_reply_to || '';
my $tpl_reply_to = $reply_to || '';
@ -983,7 +993,7 @@ if (defined $sender) {
$sender =~ s/^\s+|\s+$//g;
($sender) = expand_aliases($sender);
} else {
$sender = $repoauthor || $repocommitter || '';
$sender = $repoauthor->() || $repocommitter->() || '';
}
# $sender could be an already sanitized address
@ -1132,7 +1142,7 @@ sub make_message_id {
$uniq = "$message_id_stamp-$message_id_serial";
my $du_part;
for ($sender, $repocommitter, $repoauthor) {
for ($sender, $repocommitter->(), $repoauthor->()) {
$du_part = extract_valid_address(sanitize_address($_));
last if (defined $du_part and $du_part ne '');
}