From d94dfe087694d68073f01a51c7357fc4741641d8 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 19 Mar 2015 19:59:38 +0900 Subject: [PATCH] Fix #151 - reduce initial memory footprint --- src/matcher.go | 4 ++++ src/merger.go | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/matcher.go b/src/matcher.go index 0879a08..06352f5 100644 --- a/src/matcher.go +++ b/src/matcher.go @@ -134,6 +134,10 @@ func (m *Matcher) scan(request MatchRequest) (*Merger, bool) { } pattern := request.pattern empty := pattern.IsEmpty() + if empty { + return PassMerger(&request.chunks, m.tac), false + } + cancelled := util.NewAtomicBool(false) slices := m.sliceChunks(request.chunks) diff --git a/src/merger.go b/src/merger.go index 41323c1..21a7459 100644 --- a/src/merger.go +++ b/src/merger.go @@ -10,6 +10,7 @@ var EmptyMerger = NewMerger([][]*Item{}, false, false) type Merger struct { lists [][]*Item merged []*Item + chunks *[]*Chunk cursors []int sorted bool tac bool @@ -17,11 +18,24 @@ type Merger struct { count int } +func PassMerger(chunks *[]*Chunk, tac bool) *Merger { + mg := Merger{ + chunks: chunks, + tac: tac, + count: 0} + + for _, chunk := range *mg.chunks { + mg.count += len(*chunk) + } + return &mg +} + // NewMerger returns a new Merger func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger { mg := Merger{ lists: lists, merged: []*Item{}, + chunks: nil, cursors: make([]int, len(lists)), sorted: sorted, tac: tac, @@ -41,12 +55,20 @@ func (mg *Merger) Length() int { // Get returns the pointer to the Item object indexed by the given integer func (mg *Merger) Get(idx int) *Item { + if mg.chunks != nil { + if mg.tac { + idx = mg.count - idx - 1 + } + chunk := (*mg.chunks)[idx/ChunkSize] + return (*chunk)[idx%ChunkSize] + } + if mg.sorted { return mg.mergedGet(idx) } if mg.tac { - idx = mg.Length() - idx - 1 + idx = mg.count - idx - 1 } for _, list := range mg.lists { numItems := len(list)