1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-05 18:46:10 +02:00

send-email: move trivial config handling to Perl

Optimize the startup time of git-send-email by using an amended
config_regexp() function to retrieve the list of config keys and
values we're interested in.

For boolean keys we can handle the [true|false] case ourselves, and
the "--get" case didn't need any parsing. Let's leave "--path" and
other "--bool" cases to "git config". I'm not bothering with the
"undef" or "" case (true and false, respectively), let's just punt on
those and others and have "git config --type=bool" handle it.

The "grep { defined } @values" here covers a rather subtle case. For
list values such as sendemail.to it is possible as with any other
config key to provide a plain "-c sendemail.to", i.e. to set the key
as a boolean true. In that case the Git::config() API will return an
empty string, but this new parser will correctly return "undef".

However, that means we can end up with "undef" in the middle of a
list. E.g. for sendemail.smtpserveroption in conjuction with
sendemail.smtpserver as a path this would have produce a warning. For
most of the other keys we'd behave the same despite the subtle change
in the value, e.g. sendemail.to would behave the same because
Mail::Address->parse() happens to return an empty list if fed
"undef". For the boolean values we were already prepared to handle
these variables being initialized as undef anyway.

This brings the runtime of "git send-email" from ~60-~70ms to a very
steady ~40ms on my test box. We now run just one "git config"
invocation on startup instead of 8, the exact number will differ based
on the local sendemail.* config. I happen to have 8 of those set.

This brings the runtime of t9001-send-email.sh from ~13s down to ~12s
for me. The change there is less impressive as many of those tests set
various config values, and we're also getting to the point of
diminishing returns for optimizing "git send-email" itself.

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:51 +02:00 committed by Junio C Hamano
parent 5a544a4e11
commit c95e3a3f0b

View File

@ -334,7 +334,11 @@ sub read_config {
my $target = $config_bool_settings{$setting};
my $key = "$prefix.$setting";
next unless exists $known_keys->{$key};
my $v = Git::config_bool(@repo, $key);
my $v = (@{$known_keys->{$key}} == 1 &&
(defined $known_keys->{$key}->[0] &&
$known_keys->{$key}->[0] =~ /^(?:true|false)$/s))
? $known_keys->{$key}->[0] eq 'true'
: Git::config_bool(@repo, $key);
next unless defined $v;
next if $configured->{$setting}++;
$$target = $v;
@ -363,13 +367,13 @@ sub read_config {
my $key = "$prefix.$setting";
next unless exists $known_keys->{$key};
if (ref($target) eq "ARRAY") {
my @values = Git::config(@repo, $key);
next unless @values;
my @values = @{$known_keys->{$key}};
@values = grep { defined } @values;
next if $configured->{$setting}++;
@$target = @values;
}
else {
my $v = Git::config(@repo, $key);
my $v = $known_keys->{$key}->[0];
next unless defined $v;
next if $configured->{$setting}++;
$$target = $v;
@ -381,12 +385,19 @@ sub config_regexp {
my ($regex) = @_;
my @ret;
eval {
@ret = Git::command(
my $ret = Git::command(
'config',
'--name-only',
'--null',
'--get-regexp',
$regex,
);
@ret = map {
# We must always return ($k, $v) here, since
# empty config values will be just "key\0",
# not "key\nvalue\0".
my ($k, $v) = split /\n/, $_, 2;
($k, $v);
} split /\0/, $ret;
1;
} or do {
# If we have no keys we're OK, otherwise re-throw
@ -399,8 +410,10 @@ sub config_regexp {
# parses 'bool' etc.) by only doing so for config keys that exist.
my %known_config_keys;
{
my @known_config_keys = config_regexp("^sende?mail[.]");
@known_config_keys{@known_config_keys} = ();
my @kv = config_regexp("^sende?mail[.]");
while (my ($k, $v) = splice @kv, 0, 2) {
push @{$known_config_keys{$k}} => $v;
}
}
# sendemail.identity yields to --identity. We must parse this