reduce allocations done when splitting

This commit is contained in:
Yann Hamon 2020-12-15 17:04:44 +01:00
parent 11a3e8f64f
commit 4afe9b1977
2 changed files with 32 additions and 22 deletions

View file

@ -75,7 +75,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
flags.StringVar(&c.OutputFormat, "output", "text", "output format - json, tap, text")
flags.BoolVar(&c.Verbose, "verbose", false, "print results for all resources")
flags.BoolVar(&c.SkipTLS, "insecure-skip-tls-verify", false, "disable verification of the server's SSL certificate. This will make your HTTPS connections insecure")
flags.StringVar(&c.CPUProfileFile, "cpu-prof", "", "debug - log CPU profiling to file")
//flags.StringVar(&c.CPUProfileFile, "cpu-prof", "", "debug - log CPU profiling to file")
flags.BoolVar(&c.Help, "h", false, "show help information")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [FILE OR FOLDER]...\n", progName)

View file

@ -42,6 +42,7 @@ func isIgnored(path string, ignoreFilePatterns []string) (bool, error) {
func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string) (<-chan Resource, <-chan error) {
resources := make(chan Resource)
files := make(chan string)
errors := make(chan error)
go func() {
@ -71,27 +72,7 @@ func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string
return nil
}
f, err := os.Open(p)
if err != nil {
return err
}
scanner := bufio.NewScanner(f)
const maxResourceSize = 4 * 1024 * 1024 // 4MB ought to be enough for everybody
buf := make([]byte, maxResourceSize)
scanner.Buffer(buf, maxResourceSize)
scanner.Split(SplitYAMLDocument)
nRes := 0
for res := scanner.Scan(); res != false; res = scanner.Scan() {
resources <- Resource{Path: p, Bytes: scanner.Bytes()}
nRes++
}
if err := scanner.Err(); err != nil {
errors <- DiscoveryError{p, err}
}
if nRes == 0 {
resources <- Resource{Path: p, Bytes: []byte{}}
}
files <- p
return nil
})
@ -101,6 +82,35 @@ func FromFiles(ctx context.Context, ignoreFilePatterns []string, paths ...string
}
}
close(files)
}()
go func() {
maxResourceSize := 4 * 1024 * 1024
buf := make([]byte, maxResourceSize)
for p := range files {
f, err := os.Open(p)
if err != nil {
errors <- DiscoveryError{p, err}
}
scanner := bufio.NewScanner(f)
scanner.Buffer(buf, maxResourceSize)
scanner.Split(SplitYAMLDocument)
nRes := 0
for res := scanner.Scan(); res != false; res = scanner.Scan() {
resources <- Resource{Path: p, Bytes: []byte(scanner.Text())}
nRes++
}
if err := scanner.Err(); err != nil {
errors <- DiscoveryError{p, err}
}
if nRes == 0 {
resources <- Resource{Path: p, Bytes: []byte{}}
}
}
close(resources)
close(errors)
}()