summaryrefslogtreecommitdiff
path: root/src/algo
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-08-02 14:00:18 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-08-02 14:00:18 +0900
commit0ea66329b84cc6e4f8ff61ee99c00bb238070247 (patch)
tree72c3bc62ec491246390b56b2aac5b33645839503 /src/algo
parent634670e3ea51a2fa1498a3de0c074b819828e2d8 (diff)
downloadfzf-0ea66329b84cc6e4f8ff61ee99c00bb238070247.tar.gz
Performance tuning - eager rune array conversion
> wc -l /tmp/list2 2594098 /tmp/list2 > time cat /tmp/list2 | fzf-0.10.1-darwin_amd64 -fqwerty > /dev/null real 0m5.418s user 0m10.990s sys 0m1.302s > time cat /tmp/list2 | fzf-head -fqwerty > /dev/null real 0m4.862s user 0m6.619s sys 0m0.982s
Diffstat (limited to 'src/algo')
-rw-r--r--src/algo/algo.go26
-rw-r--r--src/algo/algo_test.go5
2 files changed, 15 insertions, 16 deletions
diff --git a/src/algo/algo.go b/src/algo/algo.go
index c93563a9..afc12aa8 100644
--- a/src/algo/algo.go
+++ b/src/algo/algo.go
@@ -16,7 +16,7 @@ import (
*/
// FuzzyMatch performs fuzzy-match
-func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
+func FuzzyMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
if len(pattern) == 0 {
return 0, 0
}
@@ -34,7 +34,7 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
sidx := -1
eidx := -1
- for index, char := range *runes {
+ for index, char := range runes {
// This is considerably faster than blindly applying strings.ToLower to the
// whole string
if !caseSensitive {
@@ -61,7 +61,7 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
if sidx >= 0 && eidx >= 0 {
pidx--
for index := eidx - 1; index >= sidx; index-- {
- char := (*runes)[index]
+ char := runes[index]
if !caseSensitive {
if char >= 'A' && char <= 'Z' {
char += 32
@@ -88,12 +88,12 @@ func FuzzyMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
//
// We might try to implement better algorithms in the future:
// http://en.wikipedia.org/wiki/String_searching_algorithm
-func ExactMatchNaive(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
+func ExactMatchNaive(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
if len(pattern) == 0 {
return 0, 0
}
- numRunes := len(*runes)
+ numRunes := len(runes)
plen := len(pattern)
if numRunes < plen {
return -1, -1
@@ -101,7 +101,7 @@ func ExactMatchNaive(caseSensitive bool, runes *[]rune, pattern []rune) (int, in
pidx := 0
for index := 0; index < numRunes; index++ {
- char := (*runes)[index]
+ char := runes[index]
if !caseSensitive {
if char >= 'A' && char <= 'Z' {
char += 32
@@ -123,13 +123,13 @@ func ExactMatchNaive(caseSensitive bool, runes *[]rune, pattern []rune) (int, in
}
// PrefixMatch performs prefix-match
-func PrefixMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
- if len(*runes) < len(pattern) {
+func PrefixMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
+ if len(runes) < len(pattern) {
return -1, -1
}
for index, r := range pattern {
- char := (*runes)[index]
+ char := runes[index]
if !caseSensitive {
char = unicode.ToLower(char)
}
@@ -141,7 +141,7 @@ func PrefixMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
}
// SuffixMatch performs suffix-match
-func SuffixMatch(caseSensitive bool, input *[]rune, pattern []rune) (int, int) {
+func SuffixMatch(caseSensitive bool, input []rune, pattern []rune) (int, int) {
runes := util.TrimRight(input)
trimmedLen := len(runes)
diff := trimmedLen - len(pattern)
@@ -161,11 +161,11 @@ func SuffixMatch(caseSensitive bool, input *[]rune, pattern []rune) (int, int) {
return trimmedLen - len(pattern), trimmedLen
}
-func EqualMatch(caseSensitive bool, runes *[]rune, pattern []rune) (int, int) {
- if len(*runes) != len(pattern) {
+func EqualMatch(caseSensitive bool, runes []rune, pattern []rune) (int, int) {
+ if len(runes) != len(pattern) {
return -1, -1
}
- runesStr := string(*runes)
+ runesStr := string(runes)
if !caseSensitive {
runesStr = strings.ToLower(runesStr)
}
diff --git a/src/algo/algo_test.go b/src/algo/algo_test.go
index 32056dfb..db241962 100644
--- a/src/algo/algo_test.go
+++ b/src/algo/algo_test.go
@@ -5,12 +5,11 @@ import (
"testing"
)
-func assertMatch(t *testing.T, fun func(bool, *[]rune, []rune) (int, int), caseSensitive bool, input string, pattern string, sidx int, eidx int) {
+func assertMatch(t *testing.T, fun func(bool, []rune, []rune) (int, int), caseSensitive bool, input string, pattern string, sidx int, eidx int) {
if !caseSensitive {
pattern = strings.ToLower(pattern)
}
- runes := []rune(input)
- s, e := fun(caseSensitive, &runes, []rune(pattern))
+ s, e := fun(caseSensitive, []rune(input), []rune(pattern))
if s != sidx {
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", s, sidx, input, pattern)
}