]> Cypherpunks.ru repositories - goredo.git/commitdiff
Optimise redo-sources
authorSergey Matveev <stargrave@stargrave.org>
Sat, 30 Sep 2023 09:58:25 +0000 (12:58 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 30 Sep 2023 13:27:57 +0000 (16:27 +0300)
main.go
sources.go

diff --git a/main.go b/main.go
index 025ecbed5be4972146276c386d0fb14810de6975..c05d3c777e2247d8a2b4ad69ef73aed78b571d92 100644 (file)
--- a/main.go
+++ b/main.go
@@ -499,11 +499,23 @@ CmdSwitch:
                        }
                }
                sort.Strings(tgts)
-               var srcs []string
-               srcs, err = sourcesWalker(tgts)
-               err = ErrLine(err)
-               sort.Strings(srcs)
-               for _, src := range srcs {
+               seen := make(map[string]struct{})
+               seenDeps := make(map[string]struct{})
+               srcs := make(map[string]struct{})
+               err = ErrLine(sourcesWalker(tgts, seen, seenDeps, srcs))
+               seen = nil
+               seenDeps = nil
+               if err != nil {
+                       break
+               }
+               seenDeps = nil
+               res := make([]string, 0, len(srcs))
+               for p := range srcs {
+                       res = append(res, p)
+               }
+               srcs = nil
+               sort.Strings(res)
+               for _, src := range res {
                        fmt.Println(src)
                }
        case CmdNameRedoDepFix:
index d53e0e32501b880be35e737cf377602cf9f3ed5e..3f375a6fe58f666c01a5b190a5c82ee149bf20d5 100644 (file)
@@ -18,47 +18,54 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 package main
 
 import (
-       "log"
        "os"
        "path"
 )
 
-func sourcesWalker(tgts []string) ([]string, error) {
-       seen := make(map[string]struct{}, 1<<10)
+func sourcesWalker(
+       tgts []string,
+       seen map[string]struct{},
+       seenDeps map[string]struct{},
+       srcs map[string]struct{},
+) error {
        for _, tgt := range tgts {
                tgtAbsPath := mustAbs(path.Join(Cwd, tgt))
                cwd, f := path.Split(path.Join(Cwd, tgt))
-               fdDep, err := os.Open(path.Join(cwd, RedoDir, f+DepSuffix))
+               depPath := path.Join(cwd, RedoDir, f+DepSuffix)
+               if _, ok := seenDeps[depPath]; ok {
+                       continue
+               }
+               seenDeps[depPath] = struct{}{}
+               fdDep, err := os.Open(depPath)
                if err != nil {
                        if errors.Is(err, fs.ErrNotExist) {
                                continue
                        }
-                       return nil, ErrLine(err)
+                       return ErrLine(err)
                }
                depInfo, err := depRead(fdDep)
                fdDep.Close()
                if err != nil {
-                       return nil, ErrLine(err)
+                       return ErrLine(err)
                }
                for _, m := range depInfo.ifchanges {
                        depTgt := m["Target"]
                        depTgtAbsPath := mustAbs(path.Join(cwd, depTgt))
+                       if _, ok := seen[depTgtAbsPath]; ok {
+                               continue
+                       }
+                       seen[depTgtAbsPath] = struct{}{}
                        if isSrc(cwd, depTgt) {
-                               seen[cwdMustRel(depTgtAbsPath)] = struct{}{}
+                               srcs[cwdMustRel(depTgtAbsPath)] = struct{}{}
                        } else if depTgtAbsPath != tgtAbsPath {
-                               subSrcs, err := sourcesWalker([]string{cwdMustRel(depTgtAbsPath)})
-                               if err != nil {
-                                       log.Fatal(err)
-                               }
-                               for _, p := range subSrcs {
-                                       seen[p] = struct{}{}
+                               if err := sourcesWalker(
+                                       []string{cwdMustRel(depTgtAbsPath)},
+                                       seen, seenDeps, srcs,
+                               ); err != nil {
+                                       return err
                                }
                        }
                }
        }
-       srcs := make([]string, 0, len(seen))
-       for p := range seen {
-               srcs = append(srcs, p)
-       }
-       return srcs, nil
+       return nil
 }