Simplify nth comparison when reusing transformed tokens

This commit is contained in:
Junegunn Choi 2025-01-13 17:37:50 +09:00
parent ba0935c71f
commit 56fef7c8df
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 14 additions and 19 deletions

View File

@ -192,11 +192,12 @@ func Run(opts *Options) (int, error) {
} }
nth := opts.Nth nth := opts.Nth
nthRevision := 0
patternCache := make(map[string]*Pattern) patternCache := make(map[string]*Pattern)
patternBuilder := func(runes []rune) *Pattern { patternBuilder := func(runes []rune) *Pattern {
return BuildPattern(cache, patternCache, return BuildPattern(cache, patternCache,
opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward, withPos, 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{} inputRevision := revision{}
snapshotRevision := revision{} snapshotRevision := revision{}
@ -378,6 +379,7 @@ func Run(opts *Options) (int, error) {
if val.nth != nil { if val.nth != nil {
// Change nth and clear caches // Change nth and clear caches
nth = *val.nth nth = *val.nth
nthRevision++
patternCache = make(map[string]*Pattern) patternCache = make(map[string]*Pattern)
cache.Clear() cache.Clear()
inputRevision.bumpMinor() inputRevision.bumpMinor()

View File

@ -8,9 +8,9 @@ import (
type transformed struct { type transformed struct {
// Because nth can be changed dynamically by change-nth action, we need to // Because nth can be changed dynamically by change-nth action, we need to
// keep the nth value at the time of transformation. // keep the revision number at the time of transformation.
nth []Range revision int
tokens []Token tokens []Token
} }
// Item represents each input line. 56 bytes. // Item represents each input line. 56 bytes.

View File

@ -60,6 +60,7 @@ type Pattern struct {
cacheKey string cacheKey string
delimiter Delimiter delimiter Delimiter
nth []Range nth []Range
revision int
procFun map[termType]algo.Algo procFun map[termType]algo.Algo
cache *ChunkCache cache *ChunkCache
} }
@ -72,7 +73,7 @@ func init() {
// BuildPattern builds Pattern object from the given arguments // 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, 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 var asString string
if extended { if extended {
@ -140,6 +141,7 @@ func BuildPattern(cache *ChunkCache, patternCache map[string]*Pattern, fuzzy boo
sortable: sortable, sortable: sortable,
cacheable: cacheable, cacheable: cacheable,
nth: nth, nth: nth,
revision: revision,
delimiter: delimiter, delimiter: delimiter,
cache: cache, cache: cache,
procFun: make(map[termType]algo.Algo)} 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 { func (p *Pattern) transformInput(item *Item) []Token {
if item.transformed != nil { if item.transformed != nil {
transformed := *item.transformed transformed := *item.transformed
if len(transformed.nth) == len(p.nth) { if transformed.revision == p.revision {
same := true return transformed.tokens
for idx, rangeItem := range transformed.nth {
if rangeItem != p.nth[idx] {
same = false
break
}
}
if same {
return transformed.tokens
}
} }
} }
tokens := Tokenize(item.text.ToString(), p.delimiter) tokens := Tokenize(item.text.ToString(), p.delimiter)
ret := Transform(tokens, p.nth) ret := Transform(tokens, p.nth)
item.transformed = &transformed{p.nth, ret} item.transformed = &transformed{p.revision, ret}
return ret return ret
} }

View File

@ -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 { withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern {
return BuildPattern(NewChunkCache(), make(map[string]*Pattern), return BuildPattern(NewChunkCache(), make(map[string]*Pattern),
fuzzy, fuzzyAlgo, extended, caseMode, normalize, forward, fuzzy, fuzzyAlgo, extended, caseMode, normalize, forward,
withPos, cacheable, nth, delimiter, runes) withPos, cacheable, nth, delimiter, 0, runes)
} }
func TestExact(t *testing.T) { func TestExact(t *testing.T) {
@ -135,7 +135,7 @@ func TestOrigTextAndTransformed(t *testing.T) {
chunk.items[0] = Item{ chunk.items[0] = Item{
text: util.ToChars([]byte("junegunn")), text: util.ToChars([]byte("junegunn")),
origText: &origBytes, origText: &origBytes,
transformed: &transformed{pattern.nth, trans}} transformed: &transformed{pattern.revision, trans}}
pattern.extended = extended pattern.extended = extended
matches := pattern.matchChunk(&chunk, nil, slab) // No cache matches := pattern.matchChunk(&chunk, nil, slab) // No cache
if !(matches[0].item.text.ToString() == "junegunn" && if !(matches[0].item.text.ToString() == "junegunn" &&