]> git.ipfire.org Git - thirdparty/git.git/commitdiff
send-email: lazily shell out to "git var"
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Fri, 28 May 2021 09:23:46 +0000 (11:23 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 May 2021 09:38:07 +0000 (18:38 +0900)
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>
git-send-email.perl

index de62cbf2506bd459c2b9604ceb3ce8beec583c0a..38408ec75a863e5d7c8cf42436e9d7f18431e2ca 100755 (executable)
@@ -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 '');
        }