Commit 89fde30f authored by Russ Cox's avatar Russ Cox

os: cache Getwd result as hint for next time

Avoids the dot-dot-based algorithm on repeated calls
when the directory hasn't changed.

R=golang-dev, iant, bradfitz
CC=golang-dev
https://golang.org/cl/7340043
parent 158a0353
...@@ -5,9 +5,15 @@ ...@@ -5,9 +5,15 @@
package os package os
import ( import (
"sync"
"syscall" "syscall"
) )
var getwdCache struct {
sync.Mutex
dir string
}
// Getwd returns a rooted path name corresponding to the // Getwd returns a rooted path name corresponding to the
// current directory. If the current directory can be // current directory. If the current directory can be
// reached via multiple paths (due to symbolic links), // reached via multiple paths (due to symbolic links),
...@@ -35,6 +41,17 @@ func Getwd() (pwd string, err error) { ...@@ -35,6 +41,17 @@ func Getwd() (pwd string, err error) {
} }
} }
// Apply same kludge but to cached dir instead of $PWD.
getwdCache.Lock()
pwd = getwdCache.dir
getwdCache.Unlock()
if len(pwd) > 0 {
d, err := Stat(pwd)
if err == nil && SameFile(dot, d) {
return pwd, nil
}
}
// Root is a special case because it has no parent // Root is a special case because it has no parent
// and ends in a slash. // and ends in a slash.
root, err := Stat("/") root, err := Stat("/")
...@@ -88,5 +105,11 @@ func Getwd() (pwd string, err error) { ...@@ -88,5 +105,11 @@ func Getwd() (pwd string, err error) {
// Set up for next round. // Set up for next round.
dot = pd dot = pd
} }
// Save answer as hint to avoid the expensive path next time.
getwdCache.Lock()
getwdCache.dir = pwd
getwdCache.Unlock()
return pwd, nil return pwd, nil
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment