Skip to content

Commit 2d060f7

Browse files
authored
perf: make fmt parallel (#2102)
1 parent 7a514ce commit 2d060f7

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

format/format.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"io/fs"
78
"os"
89
"path/filepath"
10+
"runtime"
911
"strings"
1012

1113
"github.com/swaggo/swag"
14+
"golang.org/x/sync/errgroup"
1215
)
1316

1417
// Format implements `fmt` command for formatting swag comments in Go source
@@ -58,24 +61,27 @@ func (f *Format) Build(config *Config) error {
5861
f.exclude[filepath.Clean(fi)] = true
5962
}
6063
}
64+
var eg errgroup.Group
65+
eg.SetLimit(runtime.GOMAXPROCS(0))
6166
for _, searchDir := range searchDirs {
62-
err := filepath.Walk(searchDir, f.visit)
67+
err := filepath.Walk(searchDir, func(path string, fileInfo fs.FileInfo, err error) error {
68+
if fileInfo.IsDir() && f.excludeDir(path) {
69+
return filepath.SkipDir
70+
}
71+
if f.excludeFile(path) {
72+
return nil
73+
}
74+
eg.Go(func() error {
75+
return f.format(path)
76+
})
77+
return nil
78+
})
6379
if err != nil {
6480
return err
6581
}
6682
}
67-
return nil
68-
}
69-
70-
func (f *Format) visit(path string, fileInfo os.FileInfo, err error) error {
71-
if fileInfo.IsDir() && f.excludeDir(path) {
72-
return filepath.SkipDir
73-
}
74-
if f.excludeFile(path) {
75-
return nil
76-
}
77-
if err := f.format(path); err != nil {
78-
return fmt.Errorf("fmt: %w", err)
83+
if err := eg.Wait(); err != nil {
84+
return err
7985
}
8086
return nil
8187
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/go-openapi/spec v0.20.4
88
github.com/stretchr/testify v1.7.0
99
github.com/urfave/cli/v2 v2.3.0
10+
golang.org/x/sync v0.12.0
1011
golang.org/x/text v0.23.0
1112
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
1213
sigs.k8s.io/yaml v1.3.0

0 commit comments

Comments
 (0)