Skip to content

Commit

Permalink
Introduce map instead of slice
Browse files Browse the repository at this point in the history
Merge pull request #4 from vyshnav-vinod/performance-improvements
  • Loading branch information
vyshnav-vinod authored May 22, 2024
2 parents fed17a5 + fa1f3cd commit e403ed3
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -122,7 +121,7 @@ func pathfinder(w io.Writer, c *Cache, ignoreDir bool, path string) int {
}

var pathReturned string
var dirsAlreadyWalked []string // to ignore walking through already walked directories
dirsAlreadyWalked := make(map[string]struct{}) // to ignore walking through already walked directories

// TODO: Goroutines or a new algorithm
if !ignoreDir {
Expand All @@ -132,15 +131,15 @@ func pathfinder(w io.Writer, c *Cache, ignoreDir bool, path string) int {
}
}

dirsAlreadyWalked = append(dirsAlreadyWalked, cwd)
dirsAlreadyWalked[cwd] = struct{}{}
if traverseAndMatchDir(w, filepath.Dir(cwd), path, &pathReturned, dirsAlreadyWalked, c) {
// Walk from one directory above
return success(w, pathReturned, c)
}

usrHome, err := os.UserHomeDir()
HandleError(err)
dirsAlreadyWalked = append(dirsAlreadyWalked, filepath.Dir(cwd))
dirsAlreadyWalked[filepath.Dir(cwd)] = struct{}{}
if traverseAndMatchDir(w, usrHome, path, &pathReturned, dirsAlreadyWalked, c) {
// Walk from $HOME
return success(w, pathReturned, c)
Expand All @@ -153,32 +152,37 @@ func pathfinder(w io.Writer, c *Cache, ignoreDir bool, path string) int {
return EXIT_FOLDERNOTFOUND
}

func traverseAndMatchDir(w io.Writer, dirName string, searchDir string, pathReturned *string, dirsAlreadyWalked []string, c *Cache) bool {
if !strings.HasPrefix(filepath.Base(dirName), ".") && !slices.Contains(dirsAlreadyWalked, dirName) {
file, err := os.Open(dirName)
HandleError(err)
defer file.Close()
dirEntries, err := file.Readdirnames(0)
func traverseAndMatchDir(w io.Writer, dirName string, searchDir string, pathReturned *string, dirsAlreadyWalked map[string]struct{}, c *Cache) bool {
if strings.HasPrefix(filepath.Base(dirName), ".") {
return false
}
if _, ok := dirsAlreadyWalked[dirName]; ok {
return false
}
file, err := os.Open(dirName)
HandleError(err)
defer file.Close()
dirEntries, err := file.Readdirnames(0)
HandleError(err)
for _, n := range dirEntries {
path, err := filepath.Abs(filepath.Join(dirName, n))
HandleError(err)
for _, n := range dirEntries {
path, err := filepath.Abs(filepath.Join(dirName, n))
HandleError(err)
f, err := os.Stat(path)
if os.IsNotExist(err) {
continue
}
if f.IsDir() {
if f.Name() == searchDir {
*pathReturned = path
f, err := os.Stat(path)
if os.IsNotExist(err) {
continue
}
if f.IsDir() {
if f.Name() == searchDir {
*pathReturned = path
return true
} else {
if traverseAndMatchDir(w, path, searchDir, pathReturned, dirsAlreadyWalked, c) {
return true
} else {
if traverseAndMatchDir(w, path, searchDir, pathReturned, dirsAlreadyWalked, c) {
return true
}
}
}
}
}

return false
}

Expand Down

0 comments on commit e403ed3

Please sign in to comment.