Re: [PATCH] builtin/commit.c: memoize git-path for COMMIT_EDITMSG
- From
Pranit Bauva <pranit.bauva@gmail.com>
- Date
- May 24, 2016, 11:41 UTC
- Message-ID
- <CAFZEwPMmMEQMuT0+ap00e5UgccDfYZ75qbovRL_d8qWZ0_2XWw@mail.gmail.com>
- In-Reply-To
- <vpq1t4rri2a.fsf@anie.imag.fr>
Hey Matthieu,
On Tue, May 24, 2016 at 1:41 PM, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:
Show 33 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Pranit Bauva <pranit.bauva@gmail.com> writes:
>>
>>> static const char *use_message_buffer;
>>> -static const char commit_editmsg[] = "COMMIT_EDITMSG";
>>> +static const char commit_editmsg_path[] = git_path_commit_editmsg();
>>
>> The function defined with the macro looks like
>>
>> const char *git_path_commit_editmsg(void)
>> {
>> static char *ret;
>> if (!ret)
>> ret = git_pathdup("COMMIT_EDITMSG");
>> return ret;
>> }
>>
>> so receiving its result to "const char v[]" looks somewhat
>> suspicious.
>>
>> More importantly, when is this function evaluated and returned value
>> used to fill commit_editmsg_path[]?
>
> I may have missed something, but I'd say "never", as the code is not
> compilable at least with my gcc:
>
> builtin/commit.c:98:1: error: invalid initializer
> static const char commit_editmsg_path[] = git_path_commit_editmsg();
> ^
>
> AFAIK, initializing a global variable with a function call is allowed in
> C++, but not in C.I wasn't aware of this fact. Thanks.
Show 10 quoted lines
> And indeed, this construct is a huge source of trouble, as it would mean > that git_path_commit_editmsg() is called 1) unconditionnally, and 2) > before entering main(). > > 1) means that the function call is made even when git is called for > another command. This is terrible for the startup time: if all git > commands have a not-totally-immediate initializer, then all commands > would need to run the initializers for all other commands. 2) means it's > a nightmare to debug, as you can hardly predict when the code will be > executed.
Yes I agree that this approach will definitely cause a lot of problems. I will re-roll with how Junio suggested.
Show 7 quoted lines
> -- > Matthieu Moy > http://www-verimag.imag.fr/~moy/ > -- > To unsubscribe from this list: send the line "unsubscribe git" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html