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

Re: [PATCH] setup: avoid unconditional open with write flags

From
Taylor Blau <me@ttaylorr.com>
Date
Dec 5, 2022, 22:59 UTC
Message-ID
<Y453yiYAifv/oV5n@nand.local>
In-Reply-To
<20221205190019.52829-1-cgzones@googlemail.com>
On Mon, Dec 05, 2022 at 08:00:19PM +0100, Christian Göttsche wrote:
Show 5 quoted lines
> @@ -1669,7 +1669,14 @@ const char *resolve_gitdir_gently(const char *suspect, int *return_error_code)
>  /* if any standard file descriptor is missing open it to /dev/null */
>  void sanitize_stdfds(void)
>  {
> -	int fd = xopen("/dev/null", O_RDWR);

So before we would do one syscall here unconditionally. Then in the common case, we'll do another syscall to close the descriptor we just opened. IOW, ordinarily we expect this function to execute two syscalls.

Show 6 quoted lines
> +	int fd;
> +
> +	if (fcntl(0, F_GETFD) != -1 &&
> +	    fcntl(1, F_GETFD) != -1 &&
> +	    fcntl(2, F_GETFD) != -1)
> +		return;

But under the same circumstances (i.e., where all three descriptors are already valid), we now have to make three syscalls to determine the same fact.

...So I'm not sure that I agree with brian's "this isn't making anything worse" statement earlier in the thread.

In practice, however, it appears to be basically undetectable. Here, 'git.old' is the pre-image of this patch, and 'git.new' is the post-image. I figure that running 'git -h' is about the fastest thing I could do:

    $ hyperfine -N -L V old,new './git.{V} -h'
    Benchmark 1: ./git.old -h
      Time (mean ± σ):       1.6 ms ±   0.2 ms    [User: 1.1 ms, System: 0.4 ms]
      Range (min … max):     0.8 ms …   2.2 ms    1589 runs
    Benchmark 2: ./git.new -h
      Time (mean ± σ):       1.6 ms ±   0.2 ms    [User: 1.1 ms, System: 0.4 ms]
      Range (min … max):     0.9 ms …   2.2 ms    1746 runs
    Summary
      './git.old -h' ran
        1.00 ± 0.14 times faster than './git.new -h'

So it appears that the old version is ever-so-slightly faster than the new one. But it's so noisy, and the regression is so small that it's hard to notice it at all.

So I wouldn't strongly oppose the patch based on those numbers, but in principle it seems flawed.

Thanks, Taylor

Previous: Christian GöttscheNext: Junio C Hamano
Message 5 of 10 in “setup: avoid unconditional open with write flags”
  1. setup: avoid unconditional open with write flagsChristian Göttsche, Dec 5, 2022
  2. brian m. carlsonDec 5, 2022
  3. Jeff KingDec 6, 2022
  4. Christian GöttscheDec 6, 2022
  5. Taylor BlauDec 5, 2022
  6. Junio C HamanoDec 6, 2022
  7. Taylor BlauDec 6, 2022
  8. Junio C HamanoDec 6, 2022
  9. René ScharfeDec 6, 2022
  10. Junio C HamanoDec 6, 2022

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.