summaryrefslogtreecommitdiffstats
path: root/src/matcher.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2016-08-19 02:39:32 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2016-08-19 02:39:32 +0900
commit37dc273148df0893053bf5cda0582a23f5c2b2d2 (patch)
treed90f5e96fa97de429265461268b1a6db2703cb4c /src/matcher.go
parentf7f01d109eb05c7eae82c243b6b6d5c5951ee707 (diff)
Micro-optimizations
- Make structs smaller - Introduce Result struct and use it to represent matched items instead of reusing Item struct for that purpose - Avoid unnecessary memory allocation - Avoid growing slice from the initial capacity - Code cleanup
Diffstat (limited to 'src/matcher.go')
-rw-r--r--src/matcher.go16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/matcher.go b/src/matcher.go
index 4c00db7e..8cd9a9f8 100644
--- a/src/matcher.go
+++ b/src/matcher.go
@@ -128,7 +128,7 @@ func (m *Matcher) sliceChunks(chunks []*Chunk) [][]*Chunk {
type partialResult struct {
index int
- matches []*Item
+ matches []*Result
}
func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
@@ -155,15 +155,21 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
waitGroup.Add(1)
go func(idx int, chunks []*Chunk) {
defer func() { waitGroup.Done() }()
- sliceMatches := []*Item{}
- for _, chunk := range chunks {
+ count := 0
+ allMatches := make([][]*Result, len(chunks))
+ for idx, chunk := range chunks {
matches := request.pattern.Match(chunk)
- sliceMatches = append(sliceMatches, matches...)
+ allMatches[idx] = matches
+ count += len(matches)
if cancelled.Get() {
return
}
countChan <- len(matches)
}
+ sliceMatches := make([]*Result, 0, count)
+ for _, matches := range allMatches {
+ sliceMatches = append(sliceMatches, matches...)
+ }
if m.sort {
if m.tac {
sort.Sort(ByRelevanceTac(sliceMatches))
@@ -200,7 +206,7 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) {
}
}
- partialResults := make([][]*Item, numSlices)
+ partialResults := make([][]*Result, numSlices)
for _ = range slices {
partialResult := <-resultChan
partialResults[partialResult.index] = partialResult.matches