Optimize rank comparison on x86 (little-endian)

This commit is contained in:
Junegunn Choi 2017-08-26 21:58:18 +09:00
parent 159699b5d7
commit 6b4805ca1a
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 37 additions and 18 deletions

View File

@ -70,7 +70,7 @@ func buildResult(item *Item, offsets []Offset, score int) Result {
} }
} }
} }
result.points[idx] = val result.points[3-idx] = val
} }
return result return result
@ -224,16 +224,3 @@ func (a ByRelevanceTac) Swap(i, j int) {
func (a ByRelevanceTac) Less(i, j int) bool { func (a ByRelevanceTac) Less(i, j int) bool {
return compareRanks(a[i], a[j], true) 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
}

16
src/result_others.go Normal file
View File

@ -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
}

View File

@ -59,10 +59,10 @@ func TestResultRank(t *testing.T) {
strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")} strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}
item1 := buildResult( item1 := buildResult(
withIndex(&Item{text: util.RunesToChars(strs[0])}, 1), []Offset{}, 2) withIndex(&Item{text: util.RunesToChars(strs[0])}, 1), []Offset{}, 2)
if item1.points[0] != math.MaxUint16-2 || // Bonus if item1.points[3] != math.MaxUint16-2 || // Bonus
item1.points[1] != 3 || // Length item1.points[2] != 3 || // Length
item1.points[2] != 0 || // Unused item1.points[1] != 0 || // Unused
item1.points[3] != 0 || // Unused item1.points[0] != 0 || // Unused
item1.item.Index() != 1 { item1.item.Index() != 1 {
t.Error(item1) t.Error(item1)
} }

16
src/result_x86.go Normal file
View File

@ -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
}