From 988114d022ffd80a67c51c7ab3f65616807f4904 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Sat, 6 Jun 2020 15:27:39 -0700 Subject: [PATCH] re-use cache when checking and adding --- pkg/snapshot/layered_map.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/snapshot/layered_map.go b/pkg/snapshot/layered_map.go index 0097b57a5..c6dbbf6c8 100644 --- a/pkg/snapshot/layered_map.go +++ b/pkg/snapshot/layered_map.go @@ -30,9 +30,10 @@ import ( ) type LayeredMap struct { - layers []map[string]string - whiteouts []map[string]string - hasher func(string) (string, error) + layers []map[string]string + whiteouts []map[string]string + layerHashCache map[string]string + hasher func(string) (string, error) // cacheHasher doesn't include mtime in it's hash so that filesystem cache keys are stable cacheHasher func(string) (string, error) } @@ -103,7 +104,14 @@ func (l *LayeredMap) MaybeAddWhiteout(s string) bool { // Add will add the specified file s to the layered map. func (l *LayeredMap) Add(s string) error { // Use hash function and add to layers - newV, err := l.hasher(s) + newV, err := func(s string) (string, error) { + if v, ok := l.layerHashCache[s]; ok { + // clear it cache for next layer. + delete(l.layerHashCache, s) + return v, nil + } + return l.hasher(s) + }(s) if err != nil { return fmt.Errorf("error creating hash for %s: %v", s, err) } @@ -126,6 +134,7 @@ func (l *LayeredMap) CheckFileChange(s string) (bool, error) { } return false, err } + l.layerHashCache[s] = newV oldV, ok := l.Get(s) if ok && newV == oldV { return false, nil