diff --git a/core_matcher.go b/core_matcher.go index 142ad4a..891c00b 100644 --- a/core_matcher.go +++ b/core_matcher.go @@ -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 diff --git a/match_set.go b/match_set.go index 1da255e..712d684 100644 --- a/match_set.go +++ b/match_set.go @@ -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 { + buf = append(buf, x) + } + return buf +} diff --git a/nfa.go b/nfa.go index 7a23865..8b285e8 100644 --- a/nfa.go +++ b/nfa.go @@ -75,6 +75,7 @@ type nfaBuffers struct { eClosure *epsilonClosure matches *matchSet transitionsBuf []*fieldMatcher + resultBuf []X } func newNfaBuffers() *nfaBuffers { @@ -84,6 +85,7 @@ func newNfaBuffers() *nfaBuffers { eClosure: newEpsilonClosure(), matches: newMatchSet(), transitionsBuf: make([]*fieldMatcher, 0, 16), + resultBuf: make([]X, 0, 16), } }