Skip to content

Commit

Permalink
Use filepath.WalkDir instead of filepath.Walk (#1210)
Browse files Browse the repository at this point in the history
This has the benefit of not having to stat all files along the way.

Ideally, we'd be able to build the data bundle in the same function
more efficiently too, but that's a more complex operation.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Oct 16, 2024
1 parent 173a992 commit 63b90e1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions internal/lsp/bundles/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"slices"
"strings"

"github.com/open-policy-agent/opa/bundle"
Expand Down Expand Up @@ -144,14 +145,20 @@ type cacheBundle struct {
func (c *cacheBundle) Refresh(path string) (bool, error) {
onDiskSourceDigests := make(map[string][]byte)

filter := []string{".manifest", "data.json", "data.yml", "data.yaml"}

// walk the bundle path and calculate the MD5 hash of each file on disk
// at the moment
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
err := filepath.WalkDir(path, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}

if info.IsDir() || dataFileLoaderFilter(path, info, 0) {
if rio.IsSkipWalkDirectory(entry) {
return filepath.SkipDir
}

if entry.IsDir() || !slices.Contains(filter, entry.Name()) {
return nil
}

Expand Down

0 comments on commit 63b90e1

Please sign in to comment.