re-use cache when checking and adding

This commit is contained in:
Tejal Desai 2020-06-06 15:27:39 -07:00
parent c576771497
commit 988114d022
1 changed files with 13 additions and 4 deletions

View File

@ -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