diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2017-08-26 21:58:18 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2017-08-27 01:46:11 +0900 |
commit | 6b4805ca1a1b0758363d1bb8c4c996730e19dc5a (patch) | |
tree | 14780508e5273c86ced515d7a806de3bfa71ed24 /src | |
parent | 159699b5d7505b132d2299690d7bf9f79d5fbda4 (diff) |
Optimize rank comparison on x86 (little-endian)
Diffstat (limited to 'src')
-rw-r--r-- | src/result.go | 15 | ||||
-rw-r--r-- | src/result_others.go | 16 | ||||
-rw-r--r-- | src/result_test.go | 8 | ||||
-rw-r--r-- | src/result_x86.go | 16 |
4 files changed, 37 insertions, 18 deletions
diff --git a/src/result.go b/src/result.go index 58cbafd7..289d83a0 100644 --- a/src/result.go +++ b/src/result.go @@ -70,7 +70,7 @@ func buildResult(item *Item, offsets []Offset, score int) Result { } } } - result.points[idx] = val + result.points[3-idx] = val } return result @@ -224,16 +224,3 @@ func (a ByRelevanceTac) Swap(i, j int) { func (a ByRelevanceTac) Less(i, j int) bool { return compareRanks(a[i], a[j], true) } - -func compareRanks(irank Result, jrank Result, tac bool) bool { - for idx := 0; idx < 4; idx++ { - left := irank.points[idx] - right := jrank.points[idx] - if left < right { - return true - } else if left > right { - return false - } - } - return (irank.item.Index() <= jrank.item.Index()) != tac -} diff --git a/src/result_others.go b/src/result_others.go new file mode 100644 index 00000000..e3363a8e --- /dev/null +++ b/src/result_others.go @@ -0,0 +1,16 @@ +// +build !386,!amd64 + +package fzf + +func compareRanks(irank Result, jrank Result, tac bool) bool { + for idx := 3; idx >= 0; idx-- { + left := irank.points[idx] + right := jrank.points[idx] + if left < right { + return true + } else if left > right { + return false + } + } + return (irank.item.Index() <= jrank.item.Index()) != tac +} diff --git a/src/result_test.go b/src/result_test.go index 1d86b1d3..afd17307 100644 --- a/src/result_test.go +++ b/src/result_test.go @@ -59,10 +59,10 @@ func TestResultRank(t *testing.T) { strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")} item1 := buildResult( withIndex(&Item{text: util.RunesToChars(strs[0])}, 1), []Offset{}, 2) - if item1.points[0] != math.MaxUint16-2 || // Bonus - item1.points[1] != 3 || // Length - item1.points[2] != 0 || // Unused - item1.points[3] != 0 || // Unused + if item1.points[3] != math.MaxUint16-2 || // Bonus + item1.points[2] != 3 || // Length + item1.points[1] != 0 || // Unused + item1.points[0] != 0 || // Unused item1.item.Index() != 1 { t.Error(item1) } diff --git a/src/result_x86.go b/src/result_x86.go new file mode 100644 index 00000000..60e26e99 --- /dev/null +++ b/src/result_x86.go @@ -0,0 +1,16 @@ +// +build 386 amd64 + +package fzf + +import "unsafe" + +func compareRanks(irank Result, jrank Result, tac bool) bool { + left := *(*uint64)(unsafe.Pointer(&irank.points[0])) + right := *(*uint64)(unsafe.Pointer(&jrank.points[0])) + if left < right { + return true + } else if left > right { + return false + } + return (irank.item.Index() <= jrank.item.Index()) != tac +} |