Skip to content

Commit

Permalink
populate slice
Browse files Browse the repository at this point in the history
  • Loading branch information
rosecodym committed Oct 30, 2023
1 parent 0b71013 commit bb281d3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/engine/ahocorasickcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func (ac *AhoCorasickCore) MatchString(input string) []*ahocorasick.Match {
// PopulateDetectorsByMatch populates the given detectorMap based on the Aho-Corasick match results.
// This method is designed to reuse the same map for performance optimization,
// reducing the need for repeated allocations within each detector worker in the engine.
func (ac *AhoCorasickCore) PopulateDetectorsByMatch(match *ahocorasick.Match, detectors map[detectorspb.DetectorType]detectors.Detector) bool {
func (ac *AhoCorasickCore) PopulateDetectorsByMatch(match *ahocorasick.Match, detectors *[]detectors.Detector) bool {
matchedDetectorKeys, ok := ac.keywordsToDetectors[match.MatchString()]
if !ok {
return false
}
for _, key := range matchedDetectorKeys {
detectors[key.detectorType] = ac.detectorsByKey[key]
*detectors = append(*detectors, ac.detectorsByKey[key])
}
return true
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/ahocorasickcore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func TestAhoCorasickCore_MultipleCustomDetectorsMatchable(t *testing.T) {
matches := ac.MatchString("a")
assert.Equal(t, 1, len(matches))

matchingDetectors := make(map[detectorspb.DetectorType]detectors.Detector)
ac.PopulateDetectorsByMatch(matches[0], matchingDetectors)
matchingDetectors := make([]detectors.Detector, 0, 2)
ac.PopulateDetectorsByMatch(matches[0], &matchingDetectors)
assert.Equal(t, 2, len(matchingDetectors))
assert.Contains(t, matchingDetectors, customDetector1)
assert.Contains(t, matchingDetectors, customDetector2)
Expand All @@ -95,8 +95,8 @@ func TestAhoCorasickCore_MultipleDetectorVersionsMatchable(t *testing.T) {
matches := ac.MatchString("a")
assert.Equal(t, 1, len(matches))

matchingDetectors := make(map[detectorspb.DetectorType]detectors.Detector)
ac.PopulateDetectorsByMatch(matches[0], matchingDetectors)
matchingDetectors := make([]detectors.Detector, 0, 2)
ac.PopulateDetectorsByMatch(matches[0], &matchingDetectors)
assert.Equal(t, 2, len(matchingDetectors))
assert.Contains(t, matchingDetectors, v1)
assert.Contains(t, matchingDetectors, v2)
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func (e *Engine) detectorWorker(ctx context.Context) {

// Reuse the same map to avoid allocations.
const avgDetectorsPerChunk = 2
chunkSpecificDetectors := make(map[detectorspb.DetectorType]detectors.Detector, avgDetectorsPerChunk)
chunkSpecificDetectors := make([]detectors.Detector, 0, avgDetectorsPerChunk)
for originalChunk := range e.ChunksChan() {
for chunk := range sources.Chunker(originalChunk) {
atomic.AddUint64(&e.metrics.BytesScanned, uint64(len(chunk.Data)))
Expand All @@ -470,12 +470,12 @@ func (e *Engine) detectorWorker(ctx context.Context) {
}

for _, match := range e.ahoCorasickCore.MatchString(string(decoded.Chunk.Data)) {
if !e.ahoCorasickCore.PopulateDetectorsByMatch(match, chunkSpecificDetectors) {
if !e.ahoCorasickCore.PopulateDetectorsByMatch(match, &chunkSpecificDetectors) {
continue
}
}

for k, detector := range chunkSpecificDetectors {
for _, detector := range chunkSpecificDetectors {
decoded.Chunk.Verify = e.verify
wgDetect.Add(1)
e.detectableChunksChan <- detectableChunk{
Expand All @@ -484,8 +484,8 @@ func (e *Engine) detectorWorker(ctx context.Context) {
decoder: decoded.DecoderType,
wgDoneFn: wgDetect.Done,
}
delete(chunkSpecificDetectors, k)
}
clear(chunkSpecificDetectors)
}
}
atomic.AddUint64(&e.metrics.ChunksScanned, 1)
Expand Down

0 comments on commit bb281d3

Please sign in to comment.