diff options
| author | Egon Elbre <egonelbre@gmail.com> | 2021-04-20 17:38:54 +0300 |
|---|---|---|
| committer | Tobias Klauser <tobias.klauser@gmail.com> | 2021-04-22 21:03:32 +0000 |
| commit | cfe5d79c5c2c9888a0e56e089dca99e405a225b9 (patch) | |
| tree | 98ecca28b1161a03395b5f737bf3e0230fab7a00 /src/os/executable_procfs.go | |
| parent | ecfce58965da6017e02f5fc5c03eda52fc41c8d6 (diff) | |
| download | go-cfe5d79c5c2c9888a0e56e089dca99e405a225b9.tar.xz | |
os: depend on Readlink only when necessary
Currently Readlink gets linked into the binary even when Executable is
not needed.
This reduces a simple "os.Stdout.Write([]byte("hello"))" by ~10KiB.
Previously the executable path was read during init time, because
deleting the executable would make "Readlink" return "(deleted)" suffix.
There's probably a slight chance that the init time reading would return
it anyways.
Updates #6853
Change-Id: Ic76190c5b64d9320ceb489cd6a553108614653d1
Reviewed-on: https://go-review.googlesource.com/c/go/+/311790
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Diffstat (limited to 'src/os/executable_procfs.go')
| -rw-r--r-- | src/os/executable_procfs.go | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/os/executable_procfs.go b/src/os/executable_procfs.go index 9c64a0d474..76ba0e6d08 100644 --- a/src/os/executable_procfs.go +++ b/src/os/executable_procfs.go @@ -12,10 +12,7 @@ import ( "runtime" ) -// We query the executable path at init time to avoid the problem of -// readlink returns a path appended with " (deleted)" when the original -// binary gets deleted. -var executablePath, executablePathErr = func() (string, error) { +func executable() (string, error) { var procfn string switch runtime.GOOS { default: @@ -25,9 +22,17 @@ var executablePath, executablePathErr = func() (string, error) { case "netbsd": procfn = "/proc/curproc/exe" } - return Readlink(procfn) -}() + path, err := Readlink(procfn) -func executable() (string, error) { - return executablePath, executablePathErr + // When the executable has been deleted then Readlink returns a + // path appended with " (deleted)". + return stringsTrimSuffix(path, " (deleted)"), err +} + +// stringsTrimSuffix is the same as strings.TrimSuffix. +func stringsTrimSuffix(s, suffix string) string { + if len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix { + return s[:len(s)-len(suffix)] + } + return s } |
