]> Cypherpunks.ru repositories - goredo.git/blobdiff - ood.go
Optimise memory storage of dependency information
[goredo.git] / ood.go
diff --git a/ood.go b/ood.go
index c455a3b01e22c3221f0b9a5eccc72db6b83996d0..3843fa1170b173e2469d240a2feef8ab005d7c2c 100644 (file)
--- a/ood.go
+++ b/ood.go
@@ -1,6 +1,6 @@
 /*
 goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -20,13 +20,14 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
+       "bytes"
        "errors"
        "fmt"
        "io"
+       "io/fs"
        "log"
        "os"
        "path"
-       "path/filepath"
        "strings"
 
        "golang.org/x/sys/unix"
@@ -46,45 +47,48 @@ var (
        OODTgts       map[string]struct{}
        FdOODTgts     *os.File
        FdOODTgtsLock *os.File
+
+       OODCache        = make(map[string]bool)
+       FileExistsCache = make(map[string]bool)
+
+       ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
 )
 
-type TgtErr struct {
+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 errors.Is(err, fs.ErrNotExist) {
+               FileExistsCache[p] = false
+       }
+       return false
+}
+
+type TgtError struct {
        Tgt string
        Err error
 }
 
-func (e TgtErr) Unwrap() error { return e.Err }
+func (e TgtError) Unwrap() error { return e.Err }
 
-func (e TgtErr) Error() string {
+func (e TgtError) Error() string {
        return fmt.Sprintf("%s: %s", e.Tgt, e.Err)
 }
 
-func cwdMustRel(paths ...string) string {
-       rel, err := filepath.Rel(Cwd, path.Join(paths...))
-       if err != nil {
-               panic(err)
-       }
-       return rel
-}
-
-func cwdAndTgt(tgt string) (string, string) {
-       cwd, tgt := path.Split(tgt)
-       cwd, err := filepath.Abs(cwd)
-       if err != nil {
-               panic(err)
-       }
-       return cwd, tgt
-}
-
 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
@@ -93,129 +97,141 @@ func isSrc(cwd, tgt string) bool {
 func isOODByBuildUUID(cwd, tgtOrig string) bool {
        cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
        depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
-       fdDep, err := os.Open(depPath)
-       if err != nil {
-               return true
-       }
-       depInfo, err := depRead(fdDep)
-       fdDep.Close()
-       if err != nil || depInfo.build != BuildUUID {
-               return true
-       }
-       return false
+       build, err := depReadBuild(depPath)
+       return err != nil || build != BuildUUID
 }
 
 func isOOD(cwd, tgtOrig string, level int, seen map[string]struct{}) (bool, error) {
        indent := strings.Repeat(". ", level)
-       trace(CDebug, "ood: %s%s checking", indent, tgtOrig)
+       tracef(CDebug, "ood: %s%s checking", indent, tgtOrig)
        cwd, tgt := cwdAndTgt(path.Join(cwd, tgtOrig))
-       depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
-       fdDep, err := os.Open(depPath)
-       if err != nil {
-               trace(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
-               return true, nil
+       ood, cached := OODCache[path.Join(cwd, tgt)]
+       if cached {
+               tracef(CDebug, "ood: %s%s -> cached: %v", indent, tgtOrig, ood)
+               return ood, nil
        }
-       depInfo, err := depRead(fdDep)
-       fdDep.Close()
+       depPath := path.Join(cwd, RedoDir, tgt+DepSuffix)
+       depInfo, err := depRead(depPath)
        if err != nil {
-               return true, TgtErr{tgtOrig, err}
+               if errors.Is(err, fs.ErrNotExist) {
+                       if isSrc(cwd, tgt) {
+                               ood = false
+                               tracef(CDebug, "ood: %s%s -> is source", indent, tgtOrig)
+                       } else {
+                               ood = true
+                               tracef(CDebug, "ood: %s%s -> no dep: %s", indent, tgtOrig, depPath)
+                       }
+                       OODCache[path.Join(cwd, tgt)] = ood
+                       return ood, nil
+               }
+               if err != nil {
+                       return true, TgtError{tgtOrig, ErrLine(err)}
+               }
        }
 
        if depInfo.build == BuildUUID {
-               trace(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
+               tracef(CDebug, "ood: %s%s -> already built", indent, tgtOrig)
+               OODCache[path.Join(cwd, tgt)] = false
                return false, nil
        }
-       if _, err := os.Stat(path.Join(cwd, tgt)); err != nil && os.IsNotExist(err) {
-               trace(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
+       if !FileExists(path.Join(cwd, tgt)) {
+               tracef(CDebug, "ood: %s%s -> non-existent", indent, tgtOrig)
+               OODCache[path.Join(cwd, tgt)] = true
                return true, nil
        }
-       ood := false
 
        for _, dep := range depInfo.ifcreates {
-               if _, err := os.Stat(path.Join(cwd, dep)); err == nil {
-                       trace(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
+               if FileExists(path.Join(cwd, dep)) {
+                       tracef(CDebug, "ood: %s%s -> %s created", indent, tgtOrig, dep)
                        ood = true
                        goto Done
                }
        }
 
-       for _, m := range depInfo.ifchanges {
-               dep := m["Target"]
-               if dep == "" {
-                       return ood, TgtErr{tgtOrig, errors.New("invalid format of .rec: missing Target")}
+       for _, dep := range depInfo.ifchanges {
+               if dep.tgt == "" {
+                       return ood, TgtError{tgtOrig, ErrMissingTarget}
                }
-               theirInode, err := inodeFromRec(m)
-               if err != nil {
-                       return ood, TgtErr{tgtOrig, fmt.Errorf("invalid format of .rec: %w", err)}
+               tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep.tgt)
+               ood, cached = OODCache[path.Join(cwd, dep.tgt)]
+               if cached {
+                       tracef(CDebug, "ood: %s%s -> %s: cached: %v", indent, tgtOrig, dep.tgt, ood)
+                       if ood {
+                               goto Done
+                       }
+                       continue
                }
-               theirHsh := m["Hash"]
-               trace(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
 
-               fd, err := os.Open(path.Join(cwd, dep))
+               inode, err := inodeFromFileByPath(path.Join(cwd, dep.tgt))
                if err != nil {
-                       if os.IsNotExist(err) {
-                               trace(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
+                       if errors.Is(err, fs.ErrNotExist) {
+                               tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep.tgt)
                                ood = true
+                               OODCache[path.Join(cwd, dep.tgt)] = ood
                                goto Done
                        }
-                       return ood, TgtErr{tgtOrig, err}
+                       return ood, TgtError{tgtOrig, ErrLine(err)}
                }
-               defer fd.Close()
 
-               inode, err := inodeFromFile(fd)
-               if err != nil {
-                       return ood, TgtErr{tgtOrig, err}
-               }
-               if inode.Size != theirInode.Size {
-                       trace(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
+               if inode.Size != dep.inode.Size {
+                       tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep.tgt)
                        ood = true
+                       OODCache[path.Join(cwd, dep.tgt)] = ood
                        goto Done
                }
-               if InodeTrust && inode.Equals(theirInode) {
-                       trace(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
+               if InodeTrust != InodeTrustNone && inode.Equals(dep.inode) {
+                       tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep.tgt)
                } else {
-                       trace(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
+                       tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep.tgt)
+                       fd, err := os.Open(path.Join(cwd, dep.tgt))
+                       if err != nil {
+                               return ood, TgtError{tgtOrig, ErrLine(err)}
+                       }
                        hsh, err := fileHash(fd)
+                       fd.Close()
                        if err != nil {
-                               return ood, TgtErr{tgtOrig, err}
+                               return ood, TgtError{tgtOrig, ErrLine(err)}
                        }
-                       if theirHsh != hsh {
-                               trace(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep)
+                       if !bytes.Equal(dep.hash, hsh) {
+                               tracef(CDebug, "ood: %s%s -> %s: hash differs", indent, tgtOrig, dep.tgt)
                                ood = true
+                               OODCache[path.Join(cwd, dep.tgt)] = ood
                                goto Done
                        }
-                       trace(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
+                       tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep.tgt)
                }
-               fd.Close() // optimization not to hold it for long
 
-               if dep == tgt {
-                       trace(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
+               if dep.tgt == tgt {
+                       tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep.tgt)
                        continue
                }
-               if isSrc(cwd, dep) {
-                       trace(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep)
+               if isSrc(cwd, dep.tgt) {
+                       tracef(CDebug, "ood: %s%s -> %s: is source", indent, tgtOrig, dep.tgt)
+                       OODCache[path.Join(cwd, dep.tgt)] = false
                        continue
                }
 
-               if _, ok := seen[cwdMustRel(cwd, dep)]; ok {
-                       trace(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep)
+               if _, ok := seen[cwdMustRel(cwd, dep.tgt)]; ok {
+                       tracef(CDebug, "ood: %s%s -> %s: was always built", indent, tgtOrig, dep.tgt)
+                       OODCache[path.Join(cwd, dep.tgt)] = false
                        continue
                }
 
-               depOod, err := isOODWithTrace(cwd, dep, level+1, seen)
+               depOOD, err := isOODWithTrace(cwd, dep.tgt, level+1, seen)
                if err != nil {
-                       return ood, TgtErr{tgtOrig, err}
+                       return ood, TgtError{tgtOrig, err}
                }
-               if depOod {
-                       trace(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep)
+               if depOOD {
+                       tracef(CDebug, "ood: %s%s -> %s: ood", indent, tgtOrig, dep.tgt)
                        ood = true
                        goto Done
                }
-               trace(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep)
+               tracef(CDebug, "ood: %s%s -> %s: !ood", indent, tgtOrig, dep.tgt)
        }
 
 Done:
-       trace(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
+       tracef(CDebug, "ood: %s%s: %v", indent, tgtOrig, ood)
+       OODCache[path.Join(cwd, tgt)] = ood
        return ood, nil
 }
 
@@ -224,21 +240,19 @@ func isOODWithTrace(
        level int,
        seen map[string]struct{},
 ) (bool, error) {
-       p, err := filepath.Abs(path.Join(cwd, tgtOrig))
-       if err != nil {
-               panic(err)
-       }
+       p := mustAbs(path.Join(cwd, tgtOrig))
        _, ood := OODTgts[p]
+       var err error
        if ood {
                if !isOODByBuildUUID(cwd, tgtOrig) {
-                       trace(
+                       tracef(
                                CDebug,
                                "ood: %s%s -> already built",
                                strings.Repeat(". ", level), tgtOrig,
                        )
                        return false, nil
                }
-               trace(
+               tracef(
                        CDebug,
                        "ood: %s%s true, external decision",
                        strings.Repeat(". ", level), tgtOrig,
@@ -250,25 +264,40 @@ func isOODWithTrace(
                return ood, err
        }
 RecordOODTgt:
-       if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
-               log.Fatalln(err)
+       flock := unix.Flock_t{
+               Type:   unix.F_WRLCK,
+               Whence: io.SeekStart,
+       }
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
+               log.Fatal(err)
        }
        if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
-               log.Fatalln(err)
+               log.Fatal(err)
        }
        if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
-               log.Fatalln(err)
+               log.Fatal(err)
+       }
+       flock.Type = unix.F_UNLCK
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
+               log.Fatal(err)
        }
-       unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
        return true, nil
 }
 
 func oodTgtsClear() {
-       if err := unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
-               log.Fatalln(err)
+       var err error
+       flock := unix.Flock_t{
+               Type:   unix.F_WRLCK,
+               Whence: io.SeekStart,
+       }
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
+               log.Fatal(err)
+       }
+       if err = FdOODTgts.Truncate(0); err != nil {
+               log.Fatal(err)
        }
-       if err := FdOODTgts.Truncate(0); err != nil {
-               log.Fatalln(err)
+       flock.Type = unix.F_UNLCK
+       if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
+               log.Fatal(err)
        }
-       unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN)
 }