diff options
| author | Junegunn Choi <junegunn.c@gmail.com> | 2016-08-19 02:39:32 +0900 |
|---|---|---|
| committer | Junegunn Choi <junegunn.c@gmail.com> | 2016-08-19 02:39:32 +0900 |
| commit | 37dc273148df0893053bf5cda0582a23f5c2b2d2 (patch) | |
| tree | d90f5e96fa97de429265461268b1a6db2703cb4c /src/merger.go | |
| parent | f7f01d109eb05c7eae82c243b6b6d5c5951ee707 (diff) | |
| download | fzf-37dc273148df0893053bf5cda0582a23f5c2b2d2.tar.gz | |
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/merger.go')
| -rw-r--r-- | src/merger.go | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/src/merger.go b/src/merger.go index 0d3fb801..3879ab7e 100644 --- a/src/merger.go +++ b/src/merger.go @@ -3,13 +3,13 @@ package fzf import "fmt" // EmptyMerger is a Merger with no data -var EmptyMerger = NewMerger([][]*Item{}, false, false) +var EmptyMerger = NewMerger([][]*Result{}, false, false) // Merger holds a set of locally sorted lists of items and provides the view of // a single, globally-sorted list type Merger struct { - lists [][]*Item - merged []*Item + lists [][]*Result + merged []*Result chunks *[]*Chunk cursors []int sorted bool @@ -33,10 +33,10 @@ func PassMerger(chunks *[]*Chunk, tac bool) *Merger { } // NewMerger returns a new Merger -func NewMerger(lists [][]*Item, sorted bool, tac bool) *Merger { +func NewMerger(lists [][]*Result, sorted bool, tac bool) *Merger { mg := Merger{ lists: lists, - merged: []*Item{}, + merged: []*Result{}, chunks: nil, cursors: make([]int, len(lists)), sorted: sorted, @@ -55,14 +55,14 @@ func (mg *Merger) Length() int { return mg.count } -// Get returns the pointer to the Item object indexed by the given integer -func (mg *Merger) Get(idx int) *Item { +// Get returns the pointer to the Result object indexed by the given integer +func (mg *Merger) Get(idx int) *Result { if mg.chunks != nil { if mg.tac { idx = mg.count - idx - 1 } chunk := (*mg.chunks)[idx/chunkSize] - return (*chunk)[idx%chunkSize] + return &Result{item: (*chunk)[idx%chunkSize]} } if mg.sorted { @@ -86,9 +86,9 @@ func (mg *Merger) cacheable() bool { return mg.count < mergerCacheMax } -func (mg *Merger) mergedGet(idx int) *Item { +func (mg *Merger) mergedGet(idx int) *Result { for i := len(mg.merged); i <= idx; i++ { - minRank := buildEmptyRank(0) + minRank := minRank() minIdx := -1 for listIdx, list := range mg.lists { cursor := mg.cursors[listIdx] @@ -97,7 +97,7 @@ func (mg *Merger) mergedGet(idx int) *Item { continue } if cursor >= 0 { - rank := list[cursor].Rank(false) + rank := list[cursor].rank if minIdx < 0 || compareRanks(rank, minRank, mg.tac) { minRank = rank minIdx = listIdx |
