From 55deaa89088873c55269237e0793b1a60cc01660 Mon Sep 17 00:00:00 2001 From: Christopher LaPointe Date: Sat, 16 Nov 2019 13:22:23 -0500 Subject: [PATCH] Tweak line parsing to not create heap memory --- pkg/extractor/extractor.go | 13 ++++++------- profiling.go | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/extractor/extractor.go b/pkg/extractor/extractor.go index 1cf24fe..a98793a 100644 --- a/pkg/extractor/extractor.go +++ b/pkg/extractor/extractor.go @@ -72,7 +72,7 @@ func indexToSlices(s string, indexMatches []int) []string { } // async safe -func (s *Extractor) processLineSync(line BString) *Match { +func (s *Extractor) processLineSync(line BString) (Match, bool) { lineNum := atomic.AddUint64(&s.readLines, 1) matches := s.regex.FindSubmatchIndex(line) @@ -88,7 +88,7 @@ func (s *Extractor) processLineSync(line BString) *Match { if len(extractedKey) > 0 { matchNum := atomic.AddUint64(&s.matchedLines, 1) - return &Match{ + return Match{ bLine: line, Line: lineStringPtr, Groups: slices, @@ -96,7 +96,7 @@ func (s *Extractor) processLineSync(line BString) *Match { Extracted: extractedKey, LineNumber: lineNum, MatchNumber: matchNum, - } + }, true } atomic.AddUint64(&s.ignoredLines, 1) @@ -104,7 +104,7 @@ func (s *Extractor) processLineSync(line BString) *Match { atomic.AddUint64(&s.ignoredLines, 1) } } - return nil + return Match{}, false } // New an extractor from an input channel @@ -135,13 +135,12 @@ func New(inputBatch <-chan []BString, config *Config) (*Extractor, error) { var matchBatch []Match for _, s := range batch { - match := extractor.processLineSync(s) - if match != nil { + if match, ok := extractor.processLineSync(s); ok { if matchBatch == nil { // Initialize to expected cap (only if we have any matches) matchBatch = make([]Match, 0, len(batch)) } - matchBatch = append(matchBatch, *match) + matchBatch = append(matchBatch, match) } } if len(matchBatch) > 0 { diff --git a/profiling.go b/profiling.go index 2586481..8515ad1 100644 --- a/profiling.go +++ b/profiling.go @@ -18,7 +18,7 @@ func startProfiler(basename string) { idx := 0 for { select { - case <-time.After(100 * time.Millisecond): + case <-time.After(500 * time.Millisecond): filename := fmt.Sprintf("%s_%03d.prof", basename, idx) if f, err := os.Create(filename); err == nil { pprof.WriteHeapProfile(f)