Skip to content

Commit

Permalink
perf: use worker numbers based on NumCPU
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed Apr 28, 2021
1 parent 6b5df0b commit ff0812f
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions cmd/triangle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -62,6 +63,7 @@ func main() {
grayscale = flag.Bool("gray", false, "Output in grayscale mode")
showInBrowser = flag.Bool("web", false, "Open the SVG file in the web browser")
bgColor = flag.String("bg", "", "Background color (specified as hex value)")
workers = flag.Int("w", runtime.NumCPU(), "Number of files to process concurrently")
)

flag.Usage = func() {
Expand Down Expand Up @@ -110,12 +112,6 @@ func main() {
case mode.IsDir():
var wg sync.WaitGroup

// Read source directory.
files, err := ioutil.ReadDir(*source)
if err != nil {
log.Fatalf("Unable to read directory: %v", err)
}

// Read destination file or directory.
dst, err := os.Stat(*destination)
if err != nil {
Expand All @@ -139,8 +135,8 @@ func main() {

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

wg.Add(len(files))
for i := 0; i < len(files); i++ {
wg.Add(*workers)
for i := 0; i < *workers; i++ {
go func() {
defer wg.Done()
consumer(done, paths, *destination, p, ch)
Expand Down

0 comments on commit ff0812f

Please sign in to comment.