Skip to content

Commit

Permalink
ruleguard: remove matchData allocations (#413)
Browse files Browse the repository at this point in the history
Do not use interface type for matchData since it caused
heap allocations in both match and reject cases.
  • Loading branch information
quasilyte authored Oct 1, 2022
1 parent 0588088 commit 92ca799
Show file tree
Hide file tree
Showing 7 changed files with 943 additions and 492 deletions.
35 changes: 4 additions & 31 deletions ruleguard/match_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,14 @@ import (
"github.com/quasilyte/gogrep"
)

// matchData is used to handle both regexp and AST match sets in the same way.
type matchData interface {
// TODO: don't use gogrep.CapturedNode type here.

Node() ast.Node
CaptureList() []gogrep.CapturedNode
CapturedByName(name string) (ast.Node, bool)
}

type commentMatchData struct {
node ast.Node
capture []gogrep.CapturedNode
}

func (m commentMatchData) Node() ast.Node { return m.node }

func (m commentMatchData) CaptureList() []gogrep.CapturedNode { return m.capture }

func (m commentMatchData) CapturedByName(name string) (ast.Node, bool) {
for _, c := range m.capture {
if c.Name == name {
return c.Node, true
}
}
return nil, false
}

type astMatchData struct {
type matchData struct {
match gogrep.MatchData
}

func (m astMatchData) Node() ast.Node { return m.match.Node }
func (m matchData) Node() ast.Node { return m.match.Node }

func (m astMatchData) CaptureList() []gogrep.CapturedNode { return m.match.Capture }
func (m matchData) CaptureList() []gogrep.CapturedNode { return m.match.Capture }

func (m astMatchData) CapturedByName(name string) (ast.Node, bool) {
func (m matchData) CapturedByName(name string) (ast.Node, bool) {
return m.match.CapturedByName(name)
}
Loading

0 comments on commit 92ca799

Please sign in to comment.