<commit_before>package the_platinum_searcher

import "sync"

var newLine = []byte("\n")

type grep struct {
	in      chan string
	done    chan struct{}
	grepper grepper
	printer printer
	opts    Option
}

func newGrep(pattern pattern, in chan string, done chan struct{}, opts Option, printer printer) grep {
	return grep{
		in:   in,
		done: done,
		grepper: newGrepper(
			pattern,
			printer,
			opts,
		),
		printer: printer,
		opts:    opts,
	}
}

func (g grep) start() {
	sem := make(chan struct{}, 208)
	wg := &sync.WaitGroup{}

	for path := range g.in {
		sem <- struct{}{}
		wg.Add(1)
		go func(path string) {
			defer wg.Done()
			defer func() { <-sem }()
			g.grepper.grep(path)
		}(path)
	}
	wg.Wait()
	close(g.printer.in)
	g.done <- <-g.printer.done
}

type grepper interface {
	grep(path string)
}

func newGrepper(pattern pattern, printer printer, opts Option) grepper {
	if opts.SearchOption.EnableFilesWithRegexp {
		return passthroughGrep{
			printer: printer,
		}<commit_msg>To stabilize resource utilization, reuse goroutine for grep.<commit_after>package the_platinum_searcher

import (
	"math"
	"runtime"
	"sync"
)

var newLine = []byte("\n")

type grep struct {
	in      chan string
	done    chan struct{}
	grepper grepper
	printer printer
	opts    Option
}

func newGrep(pattern pattern, in chan string, done chan struct{}, opts Option, printer printer) grep {
	return grep{
		in:   in,
		done: done,
		grepper: newGrepper(
			pattern,
			printer,
			opts,
		),
		printer: printer,
		opts:    opts,
	}
}

func (g grep) start() {
	wg := &sync.WaitGroup{}
	worker := func() {
		defer wg.Done()
		for path := range g.in {
			g.grepper.grep(path)
		}
	}
	num := int(math.Max(float64(runtime.NumCPU()), 2.0))
	for i := 0; i < num; i++ {
		wg.Add(1)
		go worker()
	}

	wg.Wait()
	close(g.printer.in)
	g.done <- <-g.printer.done
}

type grepper interface {
	grep(path string)
}

func newGrepper(pattern pattern, printer printer, opts Option) grepper {
	if opts.SearchOption.EnableFilesWithRegexp {
		return passthroughGrep{
			printer: printer,
		}
{'fraction_non_alphanumeric': 0.1298449612403101, 'fraction_numerical': 0.0048449612403100775, 'mean_word_length': 3.3565400843881856, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6994312', 'n_tokens_mistral': 869, 'n_tokens_neox': 764, 'n_words': 211}