From 2fe1e28220c543ddbf4e12ee7396e44ee85ad8e0 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 17 Apr 2015 22:23:52 +0900 Subject: 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. --- src/pattern.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/pattern.go') 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 -- cgit v1.2.3