fix: report reclaimable bytes for inefficiencies

This commit is contained in:
Harold Hunt 2026-02-22 15:58:13 -05:00
commit 51c0446df6
5 changed files with 15 additions and 4 deletions

View file

@ -20,7 +20,7 @@
"sizeBytes": 6405
}
],
"inefficientBytes": 32025,
"inefficientBytes": 19215,
"sizeBytes": 1220598
},
"layer": [

View file

@ -86,7 +86,7 @@ func (v *ImageDetails) Render() error {
var wastedSpace int64
for idx := 0; idx < len(v.inefficiencies); idx++ {
data := v.inefficiencies[len(v.inefficiencies)-1-idx]
wastedSpace += data.CumulativeSize
wastedSpace += data.WastedSize()
inefficiencyReport += fmt.Sprintf(analysisTemplate, strconv.Itoa(len(data.Nodes)), humanize.Bytes(uint64(data.CumulativeSize)), data.Path)
}

View file

@ -14,6 +14,17 @@ type EfficiencyData struct {
minDiscoveredSize int64
}
// WastedSize is the estimated reclaimable size for this path.
// Exactly one discovered copy must remain, so only bytes beyond the smallest
// discovered copy count as removable.
func (data *EfficiencyData) WastedSize() int64 {
wasted := data.CumulativeSize - data.minDiscoveredSize
if wasted < 0 {
return 0
}
return wasted
}
// EfficiencySlice represents an ordered set of EfficiencyData data structures.
type EfficiencySlice []*EfficiencyData

View file

@ -30,7 +30,7 @@ func Analyze(ctx context.Context, img *Image) (*Analysis, error) {
var wastedBytes uint64
for _, file := range inefficiencies {
wastedBytes += uint64(file.CumulativeSize)
wastedBytes += uint64(file.WastedSize())
}
return &Analysis{

View file

@ -14,7 +14,7 @@ func Test_Analysis(t *testing.T) {
wastedPercent float64
path string
}{
"docker-image": {0.9844212134184309, 1220598, 66237, 32025, 0.4834911001404049, "../../../.data/test-docker-image.tar"},
"docker-image": {0.9844212134184309, 1220598, 66237, 19215, 0.29009466008424295, "../../../.data/test-docker-image.tar"},
}
for name, test := range table {