git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] wrapper: use a loop instead of repetitive statements

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 26, 2019, 20:14 UTC
Message-ID
<nycvar.QRO.7.76.6.1909262155020.15067@tvgsbejvaqbjf.bet>
In-Reply-To
<03e509db-942e-4538-4729-4e345df82a7e@gmail.com>
Hi,
On Thu, 26 Sep 2019, Derrick Stolee wrote:
Show 32 quoted lines
> On 9/24/2019 10:01 PM, Alex Henrie wrote:
> > Signed-off-by: Alex Henrie <alexhenrie24@gmail.com>
> > ---
> >  wrapper.c | 11 +++++------
> >  1 file changed, 5 insertions(+), 6 deletions(-)
> >
> > diff --git a/wrapper.c b/wrapper.c
> > index c55d7722d7..c23ac6adcd 100644
> > --- a/wrapper.c
> > +++ b/wrapper.c
> > @@ -469,13 +469,12 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
> >  	filename_template = &pattern[len - 6 - suffix_len];
> >  	for (count = 0; count < TMP_MAX; ++count) {
> >  		uint64_t v = value;
> > +		int i;
> >  		/* Fill in the random bits. */
> > -		filename_template[0] = letters[v % num_letters]; v /= num_letters;
> > -		filename_template[1] = letters[v % num_letters]; v /= num_letters;
> > -		filename_template[2] = letters[v % num_letters]; v /= num_letters;
> > -		filename_template[3] = letters[v % num_letters]; v /= num_letters;
> > -		filename_template[4] = letters[v % num_letters]; v /= num_letters;
> > -		filename_template[5] = letters[v % num_letters]; v /= num_letters;
> > +		for (i = 0; i < 6; i++) {
> > +			filename_template[i] = letters[v % num_letters];
> > +			v /= num_letters;
> > +		}
> >
> >  		fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
> >  		if (fd >= 0)
> >
>
> This change is clear.
Not so fast.

This looks like it was intended to help compilers that cannot unroll loops all that easily, and just because current clang can does not mean that we should put people at a deliberate disadvantage when they are stuck with a C compiler that cannot optimize the post-image of this diff.

Let's first see whether my gut feeling has any merit.

This part of the code entered Git's tree in 0620b39b3b7 (compat: add a mkstemps() compatibility function, 2009-05-31), and it was clearly copy-edited from libiberty.

It entered libiberty in https://github.com/gcc-mirror/gcc/commit/119735e34916. That commit claims that it was copy-edited from glibc, and edited it was, as it inlined the `__gen_tempname()` function.

Sadly, the commit message of the patch that introduced this pattern into glibc is pretty much not helpful at all here: https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=a7ab2023fcdd5c90c9f664cbaed8ef90dd38e818 (it only talks about using a random-name generator that is already used elsewhere.)

In short: sadly, this history excursion did not reveal anything to back up my intuition that your change would revert a change that might be crucial on older platforms.

However, I think that this patch should at least be accompanied by a commit message that suggests that some thought was put into it, and that concerns like mine were considered carefully.

I mean, if there is _any_ performance-critical code path hitting this unrolled loop, we may want to keep it unrolled.

Ciao, Johannes

Previous: Derrick StoleeNext: Jeff King
Message 9 of 13 in “scan-build fixes”
  1. 0/3 scan-build fixesAlex Henrie, Sep 25, 2019
  2. 1/3 commit-graph: remove a duplicate assignmentAlex Henrie, Sep 25, 2019
  3. Derrick StoleeSep 26, 2019
  4. Alex HenrieSep 26, 2019
  5. 2/3 diffcore-break: use a goto instead of a redundant if statementAlex Henrie, Sep 25, 2019
  6. Derrick StoleeSep 26, 2019
  7. 3/3 wrapper: use a loop instead of repetitive statementsAlex Henrie, Sep 25, 2019
  8. Derrick StoleeSep 26, 2019
  9. Johannes SchindelinSep 26, 2019
  10. Jeff KingSep 27, 2019
  11. Alex HenrieSep 29, 2019
  12. Jeff KingSep 27, 2019
  13. Derrick StoleeSep 27, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.