Skip to content

Commit 04c97aa

Browse files
fsologurengzeripathlunny
authored
Change use of Walk to WalkDir to improve disk performance (#22462)
As suggest by Go developers, use `filepath.WalkDir` instead of `filepath.Walk` because [*Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling `os.Lstat` on every file or directory visited](https://pkg.go.dev/path/filepath#Walk). This proposition address that, in a similar way as #22392 did. Co-authored-by: zeripath <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent da27438 commit 04c97aa

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

build/generate-bindata.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ func needsUpdate(dir, filename string) (bool, []byte) {
3232

3333
hasher := sha1.New()
3434

35-
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
35+
err = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
3636
if err != nil {
3737
return err
3838
}
39-
_, _ = hasher.Write([]byte(info.Name()))
39+
info, err := d.Info()
40+
if err != nil {
41+
return err
42+
}
43+
_, _ = hasher.Write([]byte(d.Name()))
4044
_, _ = hasher.Write([]byte(info.ModTime().String()))
4145
_, _ = hasher.Write([]byte(strconv.FormatInt(info.Size(), 16)))
4246
return nil

modules/log/file.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,24 @@ func compressOldLogFile(fname string, compressionLevel int) error {
225225

226226
func (log *FileLogger) deleteOldLog() {
227227
dir := filepath.Dir(log.Filename)
228-
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
228+
_ = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) (returnErr error) {
229229
defer func() {
230230
if r := recover(); r != nil {
231231
returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r)
232232
}
233233
}()
234234

235-
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*log.Maxdays) {
235+
if err != nil {
236+
return err
237+
}
238+
if d.IsDir() {
239+
return nil
240+
}
241+
info, err := d.Info()
242+
if err != nil {
243+
return err
244+
}
245+
if info.ModTime().Unix() < (time.Now().Unix() - 60*60*24*log.Maxdays) {
236246
if strings.HasPrefix(filepath.Base(path), filepath.Base(log.Filename)) {
237247
if err := util.Remove(path); err != nil {
238248
returnErr = fmt.Errorf("Failed to remove %s: %w", path, err)

modules/repository/generate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
173173
// Avoid walking tree if there are no globs
174174
if len(gt.Globs()) > 0 {
175175
tmpDirSlash := strings.TrimSuffix(filepath.ToSlash(tmpDir), "/") + "/"
176-
if err := filepath.Walk(tmpDirSlash, func(path string, info os.FileInfo, walkErr error) error {
176+
if err := filepath.WalkDir(tmpDirSlash, func(path string, d os.DirEntry, walkErr error) error {
177177
if walkErr != nil {
178178
return walkErr
179179
}
180180

181-
if info.IsDir() {
181+
if d.IsDir() {
182182
return nil
183183
}
184184

modules/storage/local.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (l *LocalStorage) URL(path, name string) (*url.URL, error) {
129129

130130
// IterateObjects iterates across the objects in the local storage
131131
func (l *LocalStorage) IterateObjects(fn func(path string, obj Object) error) error {
132-
return filepath.Walk(l.dir, func(path string, info os.FileInfo, err error) error {
132+
return filepath.WalkDir(l.dir, func(path string, d os.DirEntry, err error) error {
133133
if err != nil {
134134
return err
135135
}
@@ -141,7 +141,7 @@ func (l *LocalStorage) IterateObjects(fn func(path string, obj Object) error) er
141141
if path == l.dir {
142142
return nil
143143
}
144-
if info.IsDir() {
144+
if d.IsDir() {
145145
return nil
146146
}
147147
relPath, err := filepath.Rel(l.dir, path)

routers/web/user/setting/profile.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,17 @@ func Repos(ctx *context.Context) {
280280
repos := map[string]*repo_model.Repository{}
281281
// We're going to iterate by pagesize.
282282
root := user_model.UserPath(ctxUser.Name)
283-
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
283+
if err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
284284
if err != nil {
285285
if os.IsNotExist(err) {
286286
return nil
287287
}
288288
return err
289289
}
290-
if !info.IsDir() || path == root {
290+
if !d.IsDir() || path == root {
291291
return nil
292292
}
293-
name := info.Name()
293+
name := d.Name()
294294
if !strings.HasSuffix(name, ".git") {
295295
return filepath.SkipDir
296296
}
@@ -304,7 +304,7 @@ func Repos(ctx *context.Context) {
304304
count++
305305
return filepath.SkipDir
306306
}); err != nil {
307-
ctx.ServerError("filepath.Walk", err)
307+
ctx.ServerError("filepath.WalkDir", err)
308308
return
309309
}
310310

services/repository/adopt.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -303,31 +303,31 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in
303303

304304
// We're going to iterate by pagesize.
305305
root := filepath.Clean(setting.RepoRootPath)
306-
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
306+
if err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
307307
if err != nil {
308308
return err
309309
}
310-
if !info.IsDir() || path == root {
310+
if !d.IsDir() || path == root {
311311
return nil
312312
}
313313

314+
name := d.Name()
315+
314316
if !strings.ContainsRune(path[len(root)+1:], filepath.Separator) {
315317
// Got a new user
316318
if err = checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil {
317319
return err
318320
}
319321
repoNamesToCheck = repoNamesToCheck[:0]
320322

321-
if !globUser.Match(info.Name()) {
323+
if !globUser.Match(name) {
322324
return filepath.SkipDir
323325
}
324326

325-
userName = info.Name()
327+
userName = name
326328
return nil
327329
}
328330

329-
name := info.Name()
330-
331331
if !strings.HasSuffix(name, ".git") {
332332
return filepath.SkipDir
333333
}

0 commit comments

Comments
 (0)