From edac9820b54d8db7ef667c2d79b9e526c625f59d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 25 Oct 2021 18:46:59 +0900 Subject: [PATCH] Cache cygpath result No need to repeatedly run cygpath process because $SHELL never changes. --- src/util/util_windows.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/util/util_windows.go b/src/util/util_windows.go index 8a85a07..9ac062a 100644 --- a/src/util/util_windows.go +++ b/src/util/util_windows.go @@ -7,19 +7,28 @@ import ( "os" "os/exec" "strings" + "sync/atomic" "syscall" ) +var shellPath atomic.Value + // ExecCommand executes the given command with $SHELL func ExecCommand(command string, setpgid bool) *exec.Cmd { - shell := os.Getenv("SHELL") - if len(shell) == 0 { - shell = "cmd" - } else if strings.Contains(shell, "/") { - out, err := exec.Command("cygpath", "-w", shell).Output() - if err == nil { - shell = strings.Trim(string(out), "\n") + var shell string + if cached := shellPath.Load(); cached != nil { + shell = cached.(string) + } else { + shell = os.Getenv("SHELL") + if len(shell) == 0 { + shell = "cmd" + } else if strings.Contains(shell, "/") { + out, err := exec.Command("cygpath", "-w", shell).Output() + if err == nil { + shell = strings.Trim(string(out), "\n") + } } + shellPath.Store(shell) } return ExecCommandWith(shell, command, setpgid) }