feat: use errgroup to improve perf

main
sudo pacman -Syu 2023-01-17 17:30:58 +07:00
parent cf624ad2ea
commit bd64964fc1
3 changed files with 27 additions and 11 deletions

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/urfave/cli/v2 v2.23.7
go.uber.org/automaxprocs v1.5.1
golang.org/x/mod v0.7.0
golang.org/x/sync v0.1.0
golang.org/x/tools v0.5.0
)

1
go.sum
View File

@ -30,6 +30,7 @@ go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -16,6 +16,7 @@ import (
"github.com/dave/dst/decorator"
"github.com/pkg/diff"
"golang.org/x/mod/modfile"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/packages"
)
@ -46,6 +47,7 @@ type Formatter struct {
stdPackages map[string]struct{}
moduleNames map[string]string
formattedPaths map[string]struct{}
eg errgroup.Group
companyPrefix string
muModuleNames sync.RWMutex
muFormattedPaths sync.RWMutex
@ -87,7 +89,7 @@ func (ft *Formatter) Format(paths ...string) error {
// Logic switch case copy from goimports, gofumpt
for _, path := range paths {
path = strings.TrimSpace(path)
path := strings.TrimSpace(path)
if path == "" {
continue
}
@ -100,16 +102,24 @@ func (ft *Formatter) Format(paths ...string) error {
return err
}
default:
if err := ft.formatFile(path); err != nil {
if ft.isIgnoreError(err) {
continue
ft.eg.Go(func() error {
if err := ft.formatFile(path); err != nil {
if ft.isIgnoreError(err) {
return nil
}
return err
}
return err
}
return nil
})
}
}
if err := ft.eg.Wait(); err != nil {
return err
}
return nil
}
@ -128,13 +138,17 @@ func (ft *Formatter) formatDir(path string) error {
return nil
}
if err := ft.formatFile(path); err != nil {
if ft.isIgnoreError(err) {
return nil
ft.eg.Go(func() error {
if err := ft.formatFile(path); err != nil {
if ft.isIgnoreError(err) {
return nil
}
return err
}
return err
}
return nil
})
return nil
}); err != nil {