From 8c6743e45b4ffafd9134c1f2fb8902c9d8405faa Mon Sep 17 00:00:00 2001 From: bulk88 Date: Wed, 8 May 2024 03:52:33 -0400 Subject: [PATCH] win32: don't twice search disk for $(SHELL) unless PATH or SHELL changed -prevents a useless 2nd disk I/O pass through pwd/cwd and PATH on make process start looking for sh.exe, only search for sh.exe a 2nd time if global makefile state changes. The sh.exe searches are visible with MS Process Monitor tool sniffing gnumake proc's system calls. On my Win 7 SSD PC, its 7 milliseconds in Proc Mon tool to do the sh.exe search loop first time, and 4ms 2nd sh.exe search loop. A perf win. --- src/job.c | 2 + src/main.c | 137 ++++++++++++++++++++++++++++++-------------------- src/makeint.h | 10 +++- 3 files changed, 93 insertions(+), 56 deletions(-) diff --git a/src/job.c b/src/job.c index c0654cfb..b81f5091 100644 --- a/src/job.c +++ b/src/job.c @@ -26,6 +26,8 @@ this program. If not, see . */ const char *default_shell = "sh.exe"; int no_default_sh_exe = 1; +int did_prev_shell_find = 0; +char * last_env_path; int batch_mode_shell = 1; HANDLE main_thread; diff --git a/src/main.c b/src/main.c index 36bd5cec..d2dafd07 100644 --- a/src/main.c +++ b/src/main.c @@ -1057,70 +1057,84 @@ find_and_set_default_shell (const char *token) /* no new information, path already set or known */ sh_found = 1; } - else if (_access (search_token, 0) == 0) - { - /* search token path was found */ - default_shell = xstrdup (w32ify (search_token, 0)); - DB (DB_VERBOSE, (_("find_and_set_shell() setting default_shell = %s\n"), - default_shell)); - sh_found = 1; + else { + struct variable * v; + int do_find = 1; + char *p; + char *ep; + v = lookup_variable(STRING_SIZE_TUPLE("PATH")); + if (v && v->value) { + p = v->value; + } else { + p = NULL; + } + if (!token) { + if (!did_prev_shell_find) { + did_prev_shell_find = 1; + if (p) { + last_env_path = xstrdup(p); + } else { + last_env_path = NULL; + } + } else { + do_find = strcmp_nulok(last_env_path, p); + if (last_env_path) { + free(last_env_path); + last_env_path = NULL; + } + } } - else - { - char *p; - struct variable *v = lookup_variable (STRING_SIZE_TUPLE ("PATH")); - - /* Search Path for shell */ - if (v && v->value) - { - char *ep; - - p = v->value; - ep = strchr (p, PATH_SEPARATOR_CHAR); - - while (ep && *ep) - { - PATH_VAR (sh_path); - - *ep = '\0'; - - snprintf (sh_path, GET_PATH_MAX, "%s/%s", p, search_token); - if (_access (sh_path, 0) == 0) - { - default_shell = xstrdup (w32ify (sh_path, 0)); - sh_found = 1; - *ep = PATH_SEPARATOR_CHAR; - - /* terminate loop */ - p += strlen (p); - } - else - { - *ep = PATH_SEPARATOR_CHAR; - p = ++ep; - } - ep = strchr (p, PATH_SEPARATOR_CHAR); + if (do_find) { + if (_access(search_token, 0) == 0) { + /* search token path was found */ + default_shell = xstrdup(w32ify(search_token, 0)); + DB(DB_VERBOSE, (_("find_and_set_shell() setting default_shell = %s\n"), + default_shell)); + sh_found = 1; + } else { + /* Search Path for shell */ + if (p) { + ep = strchr(p, PATH_SEPARATOR_CHAR); + while (ep && * ep) { + PATH_VAR(sh_path); + + * ep = '\0'; + + snprintf(sh_path, GET_PATH_MAX, "%s/%s", p, search_token); + if (_access(sh_path, 0) == 0) { + default_shell = xstrdup(w32ify(sh_path, 0)); + sh_found = 1; + * ep = PATH_SEPARATOR_CHAR; + + /* terminate loop */ + p += strlen(p); + } else { + * ep = PATH_SEPARATOR_CHAR; + p = ++ep; } + ep = strchr(p, PATH_SEPARATOR_CHAR); + } + /* be sure to check last element of Path */ - if (p && *p) - { - PATH_VAR (sh_path); - snprintf (sh_path, GET_PATH_MAX, "%s/%s", p, search_token); - if (_access (sh_path, 0) == 0) - { - default_shell = xstrdup (w32ify (sh_path, 0)); - sh_found = 1; - } + if (p && * p) { + PATH_VAR(sh_path); + snprintf(sh_path, GET_PATH_MAX, "%s/%s", p, search_token); + if (_access(sh_path, 0) == 0) { + default_shell = xstrdup(w32ify(sh_path, 0)); + sh_found = 1; } + } if (sh_found) - DB (DB_VERBOSE, - (_("find_and_set_shell() path search set default_shell = %s\n"), - default_shell)); + DB(DB_VERBOSE, + (_("find_and_set_shell() path search set default_shell = %s\n"), + default_shell)); } + } } + } /* naive test */ if (!unixy_shell && sh_found @@ -1138,6 +1152,21 @@ find_and_set_default_shell (const char *token) return (sh_found); } + +int +strcmp_nulok(const char * s1, const char * s2) +{ + if(s1 == s2) { + return 0; + } + else if(s1 && s2) { + return strcmp(s1,s2); + } + else { + return 1; + } +} + #endif /* MK_OS_W32 */ #if MK_OS_DOS diff --git a/src/makeint.h b/src/makeint.h index 3d8812e9..cdc5c555 100644 --- a/src/makeint.h +++ b/src/makeint.h @@ -421,8 +421,14 @@ void sync_Path_environment (void); int w32_kill (pid_t pid, int sig); int find_and_set_default_shell (const char *token); -/* indicates whether or not we have Bourne shell */ -extern int no_default_sh_exe; +int strcmp_nulok(const char * s1, const char * s2); + + /* indicates whether or not we have Bourne shell */ + extern int no_default_sh_exe; + +extern int did_prev_shell_find; +/* path copy used on first default shell search */ +extern char * last_env_path; /* is default_shell unixy? */ extern int unixy_shell; -- 2.35.1.windows.2