Skip to content

Commit 92ca799

Browse files
authored
ruleguard: remove matchData allocations (#413)
Do not use interface type for matchData since it caused heap allocations in both match and reject cases.
1 parent 0588088 commit 92ca799

File tree

7 files changed

+943
-492
lines changed

7 files changed

+943
-492
lines changed

ruleguard/match_data.go

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,14 @@ import (
66
"github.com/quasilyte/gogrep"
77
)
88

9-
// matchData is used to handle both regexp and AST match sets in the same way.
10-
type matchData interface {
11-
// TODO: don't use gogrep.CapturedNode type here.
12-
13-
Node() ast.Node
14-
CaptureList() []gogrep.CapturedNode
15-
CapturedByName(name string) (ast.Node, bool)
16-
}
17-
18-
type commentMatchData struct {
19-
node ast.Node
20-
capture []gogrep.CapturedNode
21-
}
22-
23-
func (m commentMatchData) Node() ast.Node { return m.node }
24-
25-
func (m commentMatchData) CaptureList() []gogrep.CapturedNode { return m.capture }
26-
27-
func (m commentMatchData) CapturedByName(name string) (ast.Node, bool) {
28-
for _, c := range m.capture {
29-
if c.Name == name {
30-
return c.Node, true
31-
}
32-
}
33-
return nil, false
34-
}
35-
36-
type astMatchData struct {
9+
type matchData struct {
3710
match gogrep.MatchData
3811
}
3912

40-
func (m astMatchData) Node() ast.Node { return m.match.Node }
13+
func (m matchData) Node() ast.Node { return m.match.Node }
4114

42-
func (m astMatchData) CaptureList() []gogrep.CapturedNode { return m.match.Capture }
15+
func (m matchData) CaptureList() []gogrep.CapturedNode { return m.match.Capture }
4316

44-
func (m astMatchData) CapturedByName(name string) (ast.Node, bool) {
17+
func (m matchData) CapturedByName(name string) (ast.Node, bool) {
4518
return m.match.CapturedByName(name)
4619
}

0 commit comments

Comments
 (0)