From c62d4cdc24e20a25ba7c651595b53038f3f28f12 Mon Sep 17 00:00:00 2001 From: Dane Strandboge Date: Fri, 16 Jul 2021 09:11:11 -0500 Subject: [PATCH] refactor: move to `filepath.WalkDir` for slight performance gain in Go 1.16 (#21858) --- .../inspect/verify_seriesfile/verify_seriesfile_test.go | 4 ++-- cmd/influxd/inspect/verify_tombstone/verify_tombstone.go | 2 +- cmd/influxd/inspect/verify_tsm/verify_tsm.go | 2 +- cmd/influxd/upgrade/fs.go | 8 ++++++-- influxql/v1validation/validation_test.go | 2 +- internal/fs/influx_dir.go | 2 +- pkg/tar/stream.go | 8 ++++++-- 7 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/influxd/inspect/verify_seriesfile/verify_seriesfile_test.go b/cmd/influxd/inspect/verify_seriesfile/verify_seriesfile_test.go index 2349ce870a..ab8fb533fd 100644 --- a/cmd/influxd/inspect/verify_seriesfile/verify_seriesfile_test.go +++ b/cmd/influxd/inspect/verify_seriesfile/verify_seriesfile_test.go @@ -39,10 +39,10 @@ func TestVerifies_Invalid(t *testing.T) { test := NewTest(t) defer os.RemoveAll(test.Path) - require.NoError(t, filepath.Walk(test.Path, func(path string, info os.FileInfo, err error) error { + require.NoError(t, filepath.WalkDir(test.Path, func(path string, entry os.DirEntry, err error) error { require.NoError(t, err) - if info.IsDir() { + if entry.IsDir() { return nil } diff --git a/cmd/influxd/inspect/verify_tombstone/verify_tombstone.go b/cmd/influxd/inspect/verify_tombstone/verify_tombstone.go index e323293006..78db60f908 100644 --- a/cmd/influxd/inspect/verify_tombstone/verify_tombstone.go +++ b/cmd/influxd/inspect/verify_tombstone/verify_tombstone.go @@ -59,7 +59,7 @@ func NewVerifyTombstoneCommand() *cobra.Command { } func (v *verifier) loadFiles() error { - return filepath.Walk(v.path, func(path string, f os.FileInfo, err error) error { + return filepath.WalkDir(v.path, func(path string, d os.DirEntry, err error) error { if err != nil { return err } diff --git a/cmd/influxd/inspect/verify_tsm/verify_tsm.go b/cmd/influxd/inspect/verify_tsm/verify_tsm.go index 7a4ce0ee82..6f0b0cbccd 100644 --- a/cmd/influxd/inspect/verify_tsm/verify_tsm.go +++ b/cmd/influxd/inspect/verify_tsm/verify_tsm.go @@ -151,7 +151,7 @@ func (v *verifyChecksums) run(cmd *cobra.Command, dataPath string, verbose bool) } func (v *verifyTSM) loadFiles(dataPath string) error { - err := filepath.Walk(dataPath, func(path string, f os.FileInfo, err error) error { + err := filepath.WalkDir(dataPath, func(path string, d os.DirEntry, err error) error { if err != nil { return err } diff --git a/cmd/influxd/upgrade/fs.go b/cmd/influxd/upgrade/fs.go index 7d42bf3925..57379a5a31 100644 --- a/cmd/influxd/upgrade/fs.go +++ b/cmd/influxd/upgrade/fs.go @@ -11,11 +11,15 @@ import ( // DirSize returns total size in bytes of containing files func DirSize(path string) (uint64, error) { var size uint64 - err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { + err := filepath.WalkDir(path, func(_ string, entry os.DirEntry, err error) error { if err != nil { return err } - if !info.IsDir() { + if !entry.IsDir() { + info, err := entry.Info() + if err != nil { + return err + } size += uint64(info.Size()) } return err diff --git a/influxql/v1validation/validation_test.go b/influxql/v1validation/validation_test.go index 6d3eaeda14..0dd5723a58 100644 --- a/influxql/v1validation/validation_test.go +++ b/influxql/v1validation/validation_test.go @@ -48,7 +48,7 @@ type Test struct { } func TestGoldenFiles(t *testing.T) { - err := filepath.Walk("./goldenfiles", func(path string, info os.FileInfo, err error) error { + err := filepath.WalkDir("./goldenfiles", func(path string, info os.DirEntry, err error) error { if info.IsDir() { return nil } diff --git a/internal/fs/influx_dir.go b/internal/fs/influx_dir.go index 5c3635ab12..e225f9a7db 100644 --- a/internal/fs/influx_dir.go +++ b/internal/fs/influx_dir.go @@ -42,7 +42,7 @@ func BoltFile() (string, error) { return "", err } var file string - filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { + filepath.WalkDir(dir, func(p string, info os.DirEntry, err error) error { if err != nil { return err } diff --git a/pkg/tar/stream.go b/pkg/tar/stream.go index c1c77cf24a..44b3fe031d 100644 --- a/pkg/tar/stream.go +++ b/pkg/tar/stream.go @@ -24,13 +24,13 @@ func Stream(w io.Writer, dir, relativePath string, writeFunc func(f os.FileInfo, writeFunc = StreamFile } - return filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { + return filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error { if err != nil { return err } // Skip adding an entry for the root dir - if dir == path && f.IsDir() { + if dir == path && entry.IsDir() { return nil } @@ -40,6 +40,10 @@ func Stream(w io.Writer, dir, relativePath string, writeFunc func(f os.FileInfo, if err != nil { return err } + f, err := entry.Info() + if err != nil { + return err + } return writeFunc(f, filepath.Join(relativePath, subDir), path, tw) })