Skip to content

Commit ff0812f

Browse files
committed
perf: use worker numbers based on NumCPU
1 parent 6b5df0b commit ff0812f

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

cmd/triangle/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"path/filepath"
12+
"runtime"
1213
"strings"
1314
"sync"
1415
"time"
@@ -62,6 +63,7 @@ func main() {
6263
grayscale = flag.Bool("gray", false, "Output in grayscale mode")
6364
showInBrowser = flag.Bool("web", false, "Open the SVG file in the web browser")
6465
bgColor = flag.String("bg", "", "Background color (specified as hex value)")
66+
workers = flag.Int("w", runtime.NumCPU(), "Number of files to process concurrently")
6567
)
6668

6769
flag.Usage = func() {
@@ -110,12 +112,6 @@ func main() {
110112
case mode.IsDir():
111113
var wg sync.WaitGroup
112114

113-
// Read source directory.
114-
files, err := ioutil.ReadDir(*source)
115-
if err != nil {
116-
log.Fatalf("Unable to read directory: %v", err)
117-
}
118-
119115
// Read destination file or directory.
120116
dst, err := os.Stat(*destination)
121117
if err != nil {
@@ -139,8 +135,8 @@ func main() {
139135

140136
paths, errc := walkDir(done, *source, srcExts)
141137

142-
wg.Add(len(files))
143-
for i := 0; i < len(files); i++ {
138+
wg.Add(*workers)
139+
for i := 0; i < *workers; i++ {
144140
go func() {
145141
defer wg.Done()
146142
consumer(done, paths, *destination, p, ch)

0 commit comments

Comments
 (0)