]> Cypherpunks.ru repositories - goredo.git/commitdiff
Cache file existence state
authorSergey Matveev <stargrave@stargrave.org>
Sun, 28 Aug 2022 14:09:09 +0000 (17:09 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 28 Aug 2022 14:09:09 +0000 (17:09 +0300)
do.go
ood.go
run.go

diff --git a/do.go b/do.go
index fd3bde00f75440c902a02d58e239514c00c9f701..4fbb6c9050d6289704f76f871cf0493ff1df63b6 100644 (file)
--- a/do.go
+++ b/do.go
@@ -34,7 +34,7 @@ const (
 var TopDir string
 
 func existsDo(fdDep *os.File, cwd, pth string) (bool, error) {
-       if _, err := os.Stat(path.Join(cwd, pth)); err == nil {
+       if FileExists(path.Join(cwd, pth)) {
                return true, nil
        }
        return false, ifcreate(fdDep, pth)
@@ -89,7 +89,7 @@ func findDo(fdDep *os.File, cwd, tgt string) (string, int, error) {
                if dirAbs == TopDir {
                        break
                }
-               if _, err = os.Stat(path.Join(dirAbs, RedoDir, TopFile)); err == nil {
+               if FileExists(path.Join(dirAbs, RedoDir, TopFile)) {
                        break
                }
                if dirAbs == dirAbsPrev {
diff --git a/ood.go b/ood.go
index 7a315734287268d3f37c68fe1b82d0a91bc2579e..3ce732f10ec70f6526131b5febeb484a88dc3acb 100644 (file)
--- a/ood.go
+++ b/ood.go
@@ -47,9 +47,26 @@ var (
        FdOODTgts     *os.File
        FdOODTgtsLock *os.File
 
+       FileExistsCache map[string]bool = make(map[string]bool)
+
        ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
 )
 
+func FileExists(p string) bool {
+       if exists, known := FileExistsCache[p]; known {
+               return exists
+       }
+       _, err := os.Stat(p)
+       if err == nil {
+               FileExistsCache[p] = true
+               return true
+       }
+       if os.IsNotExist(err) {
+               FileExistsCache[p] = false
+       }
+       return false
+}
+
 type TgtError struct {
        Tgt string
        Err error
@@ -80,13 +97,13 @@ func cwdAndTgt(tgt string) (string, string) {
 
 func isSrc(cwd, tgt string) bool {
        d, f := path.Split(path.Join(cwd, tgt))
-       if _, err := os.Stat(path.Join(d, f)); err != nil {
+       if !FileExists(path.Join(d, f)) {
                return false
        }
-       if _, err := os.Stat(path.Join(d, f+".do")); err == nil {
+       if FileExists(path.Join(d, f+".do")) {
                return false
        }
-       if _, err := os.Stat(path.Join(d, RedoDir, f+DepSuffix)); err == nil {
+       if FileExists(path.Join(d, RedoDir, f+DepSuffix)) {
                return false
        }
        return true
@@ -127,14 +144,14 @@ func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, erro
                tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
                return false, nil
        }
-       if _, err := os.Stat(path.Join(cwd, tgt)); err != nil && os.IsNotExist(err) {
+       if !FileExists(path.Join(cwd, tgt)) {
                tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
                return true, nil
        }
        ood := false
 
        for _, dep := range depInfo.ifcreates {
-               if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
+               if FileExists(path.Join(cwd, dep)) {
                        tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
                        ood = true
                        goto Done
diff --git a/run.go b/run.go
index 5e67c41c37ac0107e3a816d7b48a5258f7db4e7b..985db83918e0879af20b3988215ab3f96b957f03 100644 (file)
--- a/run.go
+++ b/run.go
@@ -126,7 +126,7 @@ func (e RunError) Error() string {
 }
 
 func mkdirs(pth string) error {
-       if _, err := os.Stat(pth); err == nil {
+       if FileExists(pth) {
                return nil
        }
        return os.MkdirAll(pth, os.FileMode(0777))