From bbe10f4f7745000c121b629ff68e81bba5a497f6 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 18 Jul 2017 03:10:49 +0900 Subject: Consolidate Result and rank structs By not storing item index twice, we can cut down the size of Result struct and now it makes more sense to store and pass Results by values. Benchmarks show no degradation of performance by additional pointer indirection for looking up index. --- src/result.go | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'src/result.go') diff --git a/src/result.go b/src/result.go index fd4d1a98..2df101b3 100644 --- a/src/result.go +++ b/src/result.go @@ -19,22 +19,17 @@ type colorOffset struct { index int32 } -type rank struct { - points [4]uint16 - index int32 -} - type Result struct { - item *Item - rank rank + item *Item + points [4]uint16 } -func buildResult(item *Item, offsets []Offset, score int) *Result { +func buildResult(item *Item, offsets []Offset, score int) Result { if len(offsets) > 1 { sort.Sort(ByOrder(offsets)) } - result := Result{item: item, rank: rank{index: item.Index()}} + result := Result{item: item} numChars := item.text.Length() minBegin := math.MaxUint16 minEnd := math.MaxUint16 @@ -75,10 +70,10 @@ func buildResult(item *Item, offsets []Offset, score int) *Result { } } } - result.rank.points[idx] = val + result.points[idx] = val } - return &result + return result } // Sort criteria to use. Never changes once fzf is started. @@ -89,8 +84,8 @@ func (result *Result) Index() int32 { return result.item.Index() } -func minRank() rank { - return rank{index: 0, points: [4]uint16{math.MaxUint16, 0, 0, 0}} +func minRank() Result { + return Result{item: &nilItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}} } func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, color tui.ColorPair, attr tui.Attr, current bool) []colorOffset { @@ -201,7 +196,7 @@ func (a ByOrder) Less(i, j int) bool { } // ByRelevance is for sorting Items -type ByRelevance []*Result +type ByRelevance []Result func (a ByRelevance) Len() int { return len(a) @@ -212,11 +207,11 @@ func (a ByRelevance) Swap(i, j int) { } func (a ByRelevance) Less(i, j int) bool { - return compareRanks((*a[i]).rank, (*a[j]).rank, false) + return compareRanks(a[i], a[j], false) } // ByRelevanceTac is for sorting Items -type ByRelevanceTac []*Result +type ByRelevanceTac []Result func (a ByRelevanceTac) Len() int { return len(a) @@ -227,10 +222,10 @@ func (a ByRelevanceTac) Swap(i, j int) { } func (a ByRelevanceTac) Less(i, j int) bool { - return compareRanks((*a[i]).rank, (*a[j]).rank, true) + return compareRanks(a[i], a[j], true) } -func compareRanks(irank rank, jrank rank, tac bool) bool { +func compareRanks(irank Result, jrank Result, tac bool) bool { for idx := 0; idx < 4; idx++ { left := irank.points[idx] right := jrank.points[idx] @@ -240,5 +235,5 @@ func compareRanks(irank rank, jrank rank, tac bool) bool { return false } } - return (irank.index <= jrank.index) != tac + return (irank.item.Index() <= jrank.item.Index()) != tac } -- cgit v1.2.3