mirror of
https://github.com/hrfee/jfa-go.git
synced 2026-03-18 21:50:33 +01:00
scripts/variants: add parallel flag
doesn't result in much gain on my machine or my CI server so it's not the default. On local machine (5800x), went from ~5ms to ~4ms, on my CI (hetzner ampere altra with 8 vCPUs) went from ~15ms to ~10ms.
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var VERBOSE = false
|
var VERBOSE = false
|
||||||
@@ -71,6 +72,42 @@ func ParseDir(in, out string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParseDirParallel(in, out string) error {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
err := filepath.WalkDir(in, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
info, err := d.Info()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
perm := info.Mode()
|
||||||
|
rel, err := filepath.Rel(in, path)
|
||||||
|
outFile := filepath.Join(out, rel)
|
||||||
|
if d.IsDir() {
|
||||||
|
return os.MkdirAll(outFile, perm)
|
||||||
|
}
|
||||||
|
if VERBOSE {
|
||||||
|
log.Printf("\"%s\" => \"%s\"\n", path, outFile)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
wg.Go(func() {
|
||||||
|
if err := ParseFile(path, outFile, &perm); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func ParseFile(in, out string, perm *fs.FileMode) error {
|
func ParseFile(in, out string, perm *fs.FileMode) error {
|
||||||
file, err := os.ReadFile(in)
|
file, err := os.ReadFile(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -133,10 +170,12 @@ func ParseFile(in, out string, perm *fs.FileMode) error {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var inFile, inDir, out string
|
var inFile, inDir, out string
|
||||||
|
var parallel bool
|
||||||
flag.StringVar(&inFile, "file", "", "Input of an individual file.")
|
flag.StringVar(&inFile, "file", "", "Input of an individual file.")
|
||||||
flag.StringVar(&inDir, "dir", "", "Input of a whole directory.")
|
flag.StringVar(&inDir, "dir", "", "Input of a whole directory.")
|
||||||
flag.StringVar(&out, "out", "", "Output filepath/directory, depending on if -file or -dir passed.")
|
flag.StringVar(&out, "out", "", "Output filepath/directory, depending on if -file or -dir passed.")
|
||||||
flag.BoolVar(&VERBOSE, "v", false, "prints information about files and replacements as they are made")
|
flag.BoolVar(&VERBOSE, "v", false, "Prints information about files and replacements as they are made")
|
||||||
|
flag.BoolVar(¶llel, "p", false, "Run a goroutine per file. Probably won't speed things up.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if out == "" {
|
if out == "" {
|
||||||
@@ -147,7 +186,11 @@ func main() {
|
|||||||
if inFile != "" {
|
if inFile != "" {
|
||||||
err = ParseFile(inFile, out, nil)
|
err = ParseFile(inFile, out, nil)
|
||||||
} else if inDir != "" {
|
} else if inDir != "" {
|
||||||
|
if parallel {
|
||||||
|
err = ParseDirParallel(inDir, out)
|
||||||
|
} else {
|
||||||
err = ParseDir(inDir, out)
|
err = ParseDir(inDir, out)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user