help: improve is_executable() on Windows

On Windows, executables need to have the file extension `.exe`, or they are not executables. Hence, to support scripts, Git for Windows also looks for a she-bang line by opening the file in question, and executing it via the specified script interpreter. To figure out whether files in the `PATH` are executable, `git help` has code that imitates this behavior. With one exception: it *always* opens the files and looks for a she-bang line *or* an `MZ` tell-tale (nevermind that files with the magic `MZ` but without file extension `.exe` would still not be executable). Opening this many files leads to performance problems that are even more serious when a virus scanner is running. Therefore, let's change the code to look for the file extension `.exe` early, and avoid opening the file altogether if we already know that it is executable. See the following measurements (in seconds) as an example, where we execute a simple program that simply lists the directory contents and calls open() on every listed file: With virus scanner running (coldcache): $ ./a.exe /libexec/git-core/ before open (git-add.exe): 0.000000 after open (git-add.exe): 0.412873 before open (git-annotate.exe): 0.000175 after open (git-annotate.exe): 0.397925 before open (git-apply.exe): 0.000243 after open (git-apply.exe): 0.399996 before open (git-archive.exe): 0.000147 after open (git-archive.exe): 0.397783 before open (git-bisect--helper.exe): 0.000160 after open (git-bisect--helper.exe): 0.397700 before open (git-blame.exe): 0.000160 after open (git-blame.exe): 0.399136 ... With virus scanner running (hotcache): $ ./a.exe /libexec/git-core/ before open (git-add.exe): 0.000000 after open (git-add.exe): 0.000325 before open (git-annotate.exe): 0.000229 after open (git-annotate.exe): 0.000177 before open (git-apply.exe): 0.000167 after open (git-apply.exe): 0.000150 before open (git-archive.exe): 0.000154 after open (git-archive.exe): 0.000156 before open (git-bisect--helper.exe): 0.000132 after open (git-bisect--helper.exe): 0.000180 before open (git-blame.exe): 0.000718 after open (git-blame.exe): 0.000724 ... With this patch I get: $ time git help git Launching default browser to display HTML ... real 0m8.723s user 0m0.000s sys 0m0.000s and without $ time git help git Launching default browser to display HTML ... real 1m37.734s user 0m0.000s sys 0m0.031s both tests with cold cache and giving the machine some time to settle down after restart. [jes: adjusted the commit message] Signed-off-by: Heiko Voigt <heiko.voigt@mahr.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Heiko Voigt committed Jan 30, 2017 at 13:40 UTC c755015f79ac03bb5afa0754c30e937887fc68ab
1 file changed +18 -3
help.c
+18 -3
@@ -105,7 +105,22 @@ static int is_executable(const char *name)
105 return 0;
106
107 #if defined(GIT_WINDOWS_NATIVE)
108 -{ /* cannot trust the executable bit, peek into the file instead */
108 + /*
109 + * On Windows there is no executable bit. The file extension
110 + * indicates whether it can be run as an executable, and Git
111 + * has special-handling to detect scripts and launch them
112 + * through the indicated script interpreter. We test for the
113 + * file extension first because virus scanners may make
114 + * it quite expensive to open many files.
115 + */
116 + if (ends_with(name, ".exe"))
117 + return S_IXUSR;
118 +
119 +{
120 + /*
121 + * Now that we know it does not have an executable extension,
122 + * peek into the file instead.
123 + */
124 char buf[3] = { 0 };
125 int n;
126 int fd = open(name, O_RDONLY);
@@ -113,8 +128,8 @@ static int is_executable(const char *name)
128 if (fd >= 0) {
129 n = read(fd, buf, 2);
130 if (n == 2)
116 - /* DOS executables start with "MZ" */
117 - if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
131 + /* look for a she-bang */
132 + if (!strcmp(buf, "#!"))
133 st.st_mode |= S_IXUSR;
134 close(fd);
135 }