Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (m *coreMatcher) matchesForFields(fields []Field, bufs *nfaBuffers) ([]X, e
for i := 0; i < len(fields); i++ {
tryToMatch(fields, i, cmFields.state, matches, bufs)
}
return matches.matches(), nil
return matches.matchesInto(bufs.resultBuf[:0]), nil
}

// tryToMatch tries to match the field at fields[index] to the provided state. If it does match and generate
Expand Down
9 changes: 9 additions & 0 deletions match_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ func (m *matchSet) matches() []X {
}
return matches
}

// matchesInto appends matches to the provided buffer and returns it.
// This avoids allocating a new slice for the result.
func (m *matchSet) matchesInto(buf []X) []X {
for x := range m.set {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might the builtin copy() work here? Might even be a tiny bit faster.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry, of course not, m.set is a map not a slice

buf = append(buf, x)
}
return buf
}
2 changes: 2 additions & 0 deletions nfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type nfaBuffers struct {
eClosure *epsilonClosure
matches *matchSet
transitionsBuf []*fieldMatcher
resultBuf []X
}

func newNfaBuffers() *nfaBuffers {
Expand All @@ -84,6 +85,7 @@ func newNfaBuffers() *nfaBuffers {
eClosure: newEpsilonClosure(),
matches: newMatchSet(),
transitionsBuf: make([]*fieldMatcher, 0, 16),
resultBuf: make([]X, 0, 16),
}
}

Expand Down
Loading