diff options
author | Will Murphy <willmurphyscode@gmail.com> | 2018-07-08 13:00:08 -0400 |
---|---|---|
committer | Will Murphy <willmurphyscode@gmail.com> | 2018-07-08 13:00:08 -0400 |
commit | de7c3a759a7fcb9d508e1fbd246efcd2de1d6671 (patch) | |
tree | 32551759b285c205d9e5be51d72306f71d91d83f /filetree/tree.go | |
parent | bbe7d436ce5d444ecdb6a74056b2dc06244bb1f0 (diff) |
Count occurrences of each leaf path
We want to be able to know whether the same file appears many times
in the layers of the docker image, because a file that appears many
times may represent a real inefficiency in the dockerfile.
Diffstat (limited to 'filetree/tree.go')
-rw-r--r-- | filetree/tree.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/filetree/tree.go b/filetree/tree.go index 5fdbcbe..373f457 100644 --- a/filetree/tree.go +++ b/filetree/tree.go @@ -187,7 +187,7 @@ func (tree *FileTree) Compare(upper *FileTree) error { if upperNode.IsWhiteout() { err := tree.MarkRemoved(upperNode.Path()) if err != nil { - return fmt.Errorf("cannot remove upperNode %s: %v", upperNode.Path(), err.Error()) + return fmt.Errorf("cannot remove upperNode %s: %v", upperNode.Path(), err.Error()) } } else { lowerNode, _ := tree.GetNode(upperNode.Path()) @@ -195,7 +195,7 @@ func (tree *FileTree) Compare(upper *FileTree) error { newNode, err := tree.AddPath(upperNode.Path(), upperNode.Data.FileInfo) // fmt.Printf("added new upperNode at %s\n", newNode.Path()) if err != nil { - return fmt.Errorf("cannot add new upperNode %s: %v", upperNode.Path(), err.Error()) + return fmt.Errorf("cannot add new upperNode %s: %v", upperNode.Path(), err.Error()) } newNode.AssignDiffType(Added) } else { @@ -224,3 +224,20 @@ func StackRange(trees []*FileTree, start, stop int) *FileTree { } return tree } + +// EfficiencyMap creates a map[string]int showing how often each int +// appears in the +func EfficiencyMap(trees []*FileTree) map[string]int { + result := make(map[string]int) + visitor := func(node *FileNode) error { + result[node.Path()]++ + return nil + } + visitEvaluator := func(node *FileNode) bool { + return node.IsLeaf() + } + for _, tree := range trees { + tree.VisitDepthChildFirst(visitor, visitEvaluator) + } + return result +} |