Skip to content

Commit 74d760f

Browse files
authored
refactor: reduce memory thrashing in log search (#314)
* feat: reduce memory thrashing in log search Reuse collections instead of allocating new ones for each test.
1 parent ab6cd4b commit 74d760f

File tree

5 files changed

+37
-32
lines changed

5 files changed

+37
-32
lines changed

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
111111
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
112112
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
113113
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
114-
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
115-
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
116114
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
117115
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
118116
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
@@ -121,8 +119,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
121119
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
122120
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
123121
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
124-
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
125-
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
126122
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
127123
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
128124
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -140,8 +136,6 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc
140136
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
141137
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
142138
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
143-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
144-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
145139
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
146140
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
147141
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
@@ -152,8 +146,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
152146
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
153147
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
154148
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
155-
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
156-
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
157149
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
158150
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
159151
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=

waflog/read.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ import (
1616
"github.com/rs/zerolog/log"
1717
)
1818

19+
const maxRuleIdsEstimate = 15
20+
21+
var ruleIdsSet = make(map[uint]bool, maxRuleIdsEstimate)
22+
1923
// TriggeredRules returns the IDs of all the rules found in the log for the current test
2024
func (ll *FTWLogLines) TriggeredRules() []uint {
21-
if ll.triggeredRules != nil {
25+
if ll.triggeredRulesInitialized {
2226
return ll.triggeredRules
2327
}
28+
ll.triggeredRulesInitialized = true
2429

2530
lines := ll.getMarkedLines()
26-
2731
regex := regexp.MustCompile(`\[id "(\d+)"\]|"id":\s*"?(\d+)"?`)
28-
ruleIds := []uint{}
2932
for _, line := range lines {
3033
log.Trace().Msgf("ftw/waflog: Looking for any rule in %s", line)
3134
match := regex.FindAllSubmatch(line, -1)
@@ -42,13 +45,21 @@ func (ll *FTWLogLines) TriggeredRules() []uint {
4245
log.Error().Caller().Msgf("Failed to parse uint from %s", submatch)
4346
continue
4447
}
45-
ruleIds = append(ruleIds, uint(ruleId))
48+
ruleIdsSet[uint(ruleId)] = true
4649
}
4750
}
4851
}
4952
}
53+
ruleIds := make([]uint, 0, len(ruleIdsSet))
54+
for ruleId := range ruleIdsSet {
55+
ruleIds = append(ruleIds, ruleId)
56+
}
5057
ll.triggeredRules = ruleIds
51-
return ruleIds
58+
// Reset map for next use
59+
for key := range ruleIdsSet {
60+
delete(ruleIdsSet, key)
61+
}
62+
return ll.triggeredRules
5263
}
5364

5465
// ContainsAllIds returns true if all of the specified rule IDs appear in the log for the current test.
@@ -106,20 +117,19 @@ func (ll *FTWLogLines) MatchesRegex(pattern string) bool {
106117
}
107118

108119
func (ll *FTWLogLines) getMarkedLines() [][]byte {
109-
if ll.markedLines != nil {
120+
if ll.markedLinesInitialized {
110121
return ll.markedLines
111122
}
123+
ll.markedLinesInitialized = true
112124

113125
if ll.startMarker == nil || ll.endMarker == nil {
114126
log.Fatal().Msg("Both start and end marker must be set before the log can be inspected")
115127
}
116128

117-
var found [][]byte
118-
119129
fi, err := ll.logFile.Stat()
120130
if err != nil {
121131
log.Error().Caller().Msgf("cannot read file's size")
122-
return found
132+
return ll.markedLines
123133
}
124134

125135
// Lines in modsec logging can be quite large
@@ -149,11 +159,10 @@ func (ll *FTWLogLines) getMarkedLines() [][]byte {
149159

150160
saneCopy := make([]byte, len(line))
151161
copy(saneCopy, line)
152-
found = append(found, saneCopy)
162+
ll.markedLines = append(ll.markedLines, saneCopy)
153163
}
154164

155-
ll.markedLines = found
156-
return found
165+
return ll.markedLines
157166
}
158167

159168
// CheckLogForMarker reads the log file and searches for a marker line.

waflog/read_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (s *readTestSuite) TestReadGetMarkedLines() {
114114
s.Equal(len(foundLines), 3, "found unexpected number of log lines")
115115

116116
for index, line := range strings.Split(logLinesOnly, "\n") {
117-
s.Equalf(foundLines[index], []byte(line), "log lines don't match: \n%s\n%s", line, string(foundLines[index]))
117+
s.Equalf(string(foundLines[index]), line, "log lines don't match: \n%s\n%s", line, string(foundLines[index]))
118118
}
119119
}
120120

waflog/types.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ package waflog
66

77
import (
88
"os"
9+
"slices"
910
)
1011

1112
// FTWLogLines represents the filename to search for logs in a certain timespan
1213
type FTWLogLines struct {
13-
logFile *os.File
14-
LogMarkerHeaderName []byte
15-
startMarker []byte
16-
endMarker []byte
17-
triggeredRules []uint
18-
markedLines [][]byte
14+
logFile *os.File
15+
LogMarkerHeaderName []byte
16+
startMarker []byte
17+
endMarker []byte
18+
triggeredRules []uint
19+
markedLines [][]byte
20+
markedLinesInitialized bool
21+
triggeredRulesInitialized bool
1922
}
2023

2124
func (ll *FTWLogLines) StartMarker() []byte {
@@ -29,6 +32,8 @@ func (ll *FTWLogLines) EndMarker() []byte {
2932
func (ll *FTWLogLines) reset() {
3033
ll.startMarker = nil
3134
ll.endMarker = nil
32-
ll.triggeredRules = nil
33-
ll.markedLines = nil
35+
ll.triggeredRules = slices.Delete(ll.triggeredRules, 0, len(ll.triggeredRules))
36+
ll.markedLines = slices.Delete(ll.markedLines, 0, len(ll.markedLines))
37+
ll.markedLinesInitialized = false
38+
ll.triggeredRulesInitialized = false
3439
}

waflog/waflog_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func (s *waflogTestSuite) TestLogLinesReset() {
7070
s.Equal("X-Tests", string(ll.LogMarkerHeaderName))
7171
s.Nil(ll.startMarker)
7272
s.Nil(ll.endMarker)
73-
s.Nil(ll.triggeredRules)
74-
s.Nil(ll.triggeredRules)
75-
s.Nil(ll.markedLines)
73+
s.Empty(ll.triggeredRules)
74+
s.Empty(ll.markedLines)
7675
}

0 commit comments

Comments
 (0)