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>

Ævar Arnfjörð Bjarmason committed May 28, 2021 at 11:23 UTC fef381e6dcbc328c4ccabaf4e8d4cdf19d1aba00
1 file changed +15 -5
git-send-email.perl
+15 -5
@@ -588,8 +588,18 @@ if (0) {
588 }
589
590 my ($repoauthor, $repocommitter);
591 -($repoauthor) = Git::ident_person(@repo, 'author');
592 -($repocommitter) = Git::ident_person(@repo, 'committer');
591 +{
592 + my %cache;
593 + my ($author, $committer);
594 + my $common = sub {
595 + my ($what) = @_;
596 + return $cache{$what} if exists $cache{$what};
597 + ($cache{$what}) = Git::ident_person(@repo, $what);
598 + return $cache{$what};
599 + };
600 + $repoauthor = sub { $common->('author') };
601 + $repocommitter = sub { $common->('committer') };
602 +}
603
604 sub parse_address_line {
605 return map { $_->format } Mail::Address->parse($_[0]);
@@ -777,7 +787,7 @@ if ($compose) {
787 or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
788
789
780 - my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
790 + my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
791 my $tpl_subject = $initial_subject || '';
792 my $tpl_in_reply_to = $initial_in_reply_to || '';
793 my $tpl_reply_to = $reply_to || '';
@@ -983,7 +993,7 @@ if (defined $sender) {
993 $sender =~ s/^\s+|\s+$//g;
994 ($sender) = expand_aliases($sender);
995 } else {
986 - $sender = $repoauthor || $repocommitter || '';
996 + $sender = $repoauthor->() || $repocommitter->() || '';
997 }
998
999 # $sender could be an already sanitized address
@@ -1132,7 +1142,7 @@ sub make_message_id {
1142 $uniq = "$message_id_stamp-$message_id_serial";
1143
1144 my $du_part;
1135 - for ($sender, $repocommitter, $repoauthor) {
1145 + for ($sender, $repocommitter->(), $repoauthor->()) {
1146 $du_part = extract_valid_address(sanitize_address($_));
1147 last if (defined $du_part and $du_part ne '');
1148 }