From 56fef7c8df2713261fbdbd396aa81ed23d9a945a Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 13 Jan 2025 17:37:50 +0900 Subject: [PATCH] Simplify nth comparison when reusing transformed tokens --- src/core.go | 4 +++- src/item.go | 6 +++--- src/pattern.go | 19 ++++++------------- src/pattern_test.go | 4 ++-- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/core.go b/src/core.go index aaf1cf0..b8851d7 100644 --- a/src/core.go +++ b/src/core.go @@ -192,11 +192,12 @@ func Run(opts *Options) (int, error) { } nth := opts.Nth + nthRevision := 0 patternCache := make(map[string]*Pattern) patternBuilder := func(runes []rune) *Pattern { return BuildPattern(cache, patternCache, opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward, withPos, - opts.Filter == nil, nth, opts.Delimiter, runes) + opts.Filter == nil, nth, opts.Delimiter, nthRevision, runes) } inputRevision := revision{} snapshotRevision := revision{} @@ -378,6 +379,7 @@ func Run(opts *Options) (int, error) { if val.nth != nil { // Change nth and clear caches nth = *val.nth + nthRevision++ patternCache = make(map[string]*Pattern) cache.Clear() inputRevision.bumpMinor() diff --git a/src/item.go b/src/item.go index 6b91d06..1943486 100644 --- a/src/item.go +++ b/src/item.go @@ -8,9 +8,9 @@ import ( type transformed struct { // Because nth can be changed dynamically by change-nth action, we need to - // keep the nth value at the time of transformation. - nth []Range - tokens []Token + // keep the revision number at the time of transformation. + revision int + tokens []Token } // Item represents each input line. 56 bytes. diff --git a/src/pattern.go b/src/pattern.go index 9a38628..dd9c529 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -60,6 +60,7 @@ type Pattern struct { cacheKey string delimiter Delimiter nth []Range + revision int procFun map[termType]algo.Algo cache *ChunkCache } @@ -72,7 +73,7 @@ func init() { // BuildPattern builds Pattern object from the given arguments func BuildPattern(cache *ChunkCache, patternCache map[string]*Pattern, fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool, - withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern { + withPos bool, cacheable bool, nth []Range, delimiter Delimiter, revision int, runes []rune) *Pattern { var asString string if extended { @@ -140,6 +141,7 @@ func BuildPattern(cache *ChunkCache, patternCache map[string]*Pattern, fuzzy boo sortable: sortable, cacheable: cacheable, nth: nth, + revision: revision, delimiter: delimiter, cache: cache, procFun: make(map[termType]algo.Algo)} @@ -394,23 +396,14 @@ func (p *Pattern) extendedMatch(item *Item, withPos bool, slab *util.Slab) ([]Of func (p *Pattern) transformInput(item *Item) []Token { if item.transformed != nil { transformed := *item.transformed - if len(transformed.nth) == len(p.nth) { - same := true - for idx, rangeItem := range transformed.nth { - if rangeItem != p.nth[idx] { - same = false - break - } - } - if same { - return transformed.tokens - } + if transformed.revision == p.revision { + return transformed.tokens } } tokens := Tokenize(item.text.ToString(), p.delimiter) ret := Transform(tokens, p.nth) - item.transformed = &transformed{p.nth, ret} + item.transformed = &transformed{p.revision, ret} return ret } diff --git a/src/pattern_test.go b/src/pattern_test.go index 1487d70..0f0632c 100644 --- a/src/pattern_test.go +++ b/src/pattern_test.go @@ -68,7 +68,7 @@ func buildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern { return BuildPattern(NewChunkCache(), make(map[string]*Pattern), fuzzy, fuzzyAlgo, extended, caseMode, normalize, forward, - withPos, cacheable, nth, delimiter, runes) + withPos, cacheable, nth, delimiter, 0, runes) } func TestExact(t *testing.T) { @@ -135,7 +135,7 @@ func TestOrigTextAndTransformed(t *testing.T) { chunk.items[0] = Item{ text: util.ToChars([]byte("junegunn")), origText: &origBytes, - transformed: &transformed{pattern.nth, trans}} + transformed: &transformed{pattern.revision, trans}} pattern.extended = extended matches := pattern.matchChunk(&chunk, nil, slab) // No cache if !(matches[0].item.text.ToString() == "junegunn" &&