summaryrefslogtreecommitdiffstats
path: root/src/pattern.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-01-11 03:53:07 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-01-11 03:53:07 +0900
commit313578a1a07e79aba273f47a281247e76e6329d6 (patch)
tree1005704e5ba0885ecba1b14f313e3362a6bba63a /src/pattern.go
parentbd7331ecf5f2dc6dd7b5e7e20979b8dc8021e04f (diff)
Improve prefix/suffix cache lookup
Diffstat (limited to 'src/pattern.go')
-rw-r--r--src/pattern.go27
1 files changed, 11 insertions, 16 deletions
diff --git a/src/pattern.go b/src/pattern.go
index 2aa45c2c..31ba8137 100644
--- a/src/pattern.go
+++ b/src/pattern.go
@@ -194,24 +194,19 @@ func (p *Pattern) Match(chunk *Chunk) []*Item {
}
}
- // ChunkCache: Prefix match
- foundPrefixCache := false
- for idx := len(cacheKey) - 1; idx > 0; idx-- {
- if cached, found := _cache.Find(chunk, cacheKey[:idx]); found {
- cachedChunk := Chunk(cached)
- space = &cachedChunk
- foundPrefixCache = true
- break
- }
- }
-
- // ChunkCache: Suffix match
- if !foundPrefixCache {
- for idx := 1; idx < len(cacheKey); idx++ {
- if cached, found := _cache.Find(chunk, cacheKey[idx:]); found {
+ // ChunkCache: Prefix/suffix match
+Loop:
+ for idx := 1; idx < len(cacheKey); idx++ {
+ // [---------| ] | [ |---------]
+ // [--------| ] | [ |--------]
+ // [-------| ] | [ |-------]
+ prefix := cacheKey[:len(cacheKey)-idx]
+ suffix := cacheKey[idx:]
+ for _, substr := range [2]*string{&prefix, &suffix} {
+ if cached, found := _cache.Find(chunk, *substr); found {
cachedChunk := Chunk(cached)
space = &cachedChunk
- break
+ break Loop
}
}
}