summaryrefslogtreecommitdiffstats
path: root/src/pattern.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-04-17 22:23:52 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-04-17 22:23:52 +0900
commit2fe1e28220c543ddbf4e12ee7396e44ee85ad8e0 (patch)
tree82c77b5e639cd66b941356788bcb8e0e053949be /src/pattern.go
parent288131ac5a895ba335681339d85ee039557490da (diff)
Improvements in performance and memory usage
I profiled fzf and it turned out that it was spending significant amount of time repeatedly converting character arrays into Unicode codepoints. This commit greatly improves search performance after the initial scan by memoizing the converted results. This commit also addresses the problem of unbounded memory usage of fzf. fzf is a short-lived process that usually processes small input, so it was implemented to cache the intermediate results very aggressively with no notion of cache expiration/eviction. I still think a proper implementation of caching scheme is definitely an overkill. Instead this commit introduces limits to the maximum size (or minimum selectivity) of the intermediate results that can be cached.
Diffstat (limited to 'src/pattern.go')
-rw-r--r--src/pattern.go19
1 files changed, 9 insertions, 10 deletions
diff --git a/src/pattern.go b/src/pattern.go
index e6bda5f3..75cc5f80 100644
--- a/src/pattern.go
+++ b/src/pattern.go
@@ -43,7 +43,7 @@ type Pattern struct {
hasInvTerm bool
delimiter *regexp.Regexp
nth []Range
- procFun map[termType]func(bool, *string, []rune) (int, int)
+ procFun map[termType]func(bool, *[]rune, []rune) (int, int)
}
var (
@@ -122,7 +122,7 @@ func BuildPattern(mode Mode, caseMode Case,
hasInvTerm: hasInvTerm,
nth: nth,
delimiter: delimiter,
- procFun: make(map[termType]func(bool, *string, []rune) (int, int))}
+ procFun: make(map[termType]func(bool, *[]rune, []rune) (int, int))}
ptr.procFun[termFuzzy] = algo.FuzzyMatch
ptr.procFun[termExact] = algo.ExactMatchNaive
@@ -300,28 +300,27 @@ func (p *Pattern) extendedMatch(item *Item) []Offset {
return offsets
}
-func (p *Pattern) prepareInput(item *Item) *Transformed {
+func (p *Pattern) prepareInput(item *Item) *[]Token {
if item.transformed != nil {
return item.transformed
}
- var ret *Transformed
+ var ret *[]Token
if len(p.nth) > 0 {
tokens := Tokenize(item.text, p.delimiter)
ret = Transform(tokens, p.nth)
} else {
- trans := Transformed{
- whole: item.text,
- parts: []Token{Token{text: item.text, prefixLength: 0}}}
+ runes := []rune(*item.text)
+ trans := []Token{Token{text: &runes, prefixLength: 0}}
ret = &trans
}
item.transformed = ret
return ret
}
-func (p *Pattern) iter(pfun func(bool, *string, []rune) (int, int),
- inputs *Transformed, pattern []rune) (int, int) {
- for _, part := range inputs.parts {
+func (p *Pattern) iter(pfun func(bool, *[]rune, []rune) (int, int),
+ tokens *[]Token, pattern []rune) (int, int) {
+ for _, part := range *tokens {
prefixLength := part.prefixLength
if sidx, eidx := pfun(p.caseSensitive, part.text, pattern); sidx >= 0 {
return sidx + prefixLength, eidx + prefixLength