Skip to content

Commit

Permalink
cli/delete_cache: Remove files in parallel
Browse files Browse the repository at this point in the history
Make use of the parallelized walk.Walk() function instead of RemoveAll()
to remove files, this is significantly quicker. Additionally, get the
directory size as we're deleting so we only run walk.Walk() once. Left over
directories are still removed with RemoveAll() after all files have been
deleted.

If we could tweak the file walk to search depth first, we could append
all directories to an array and then delete them with Remove() after all files
have been removed which would be slightly faster. However, that is a micro
optimization for the future.

Before: 0m0.174s | Now: 0m0.081s (uncached, 720.1 MiB)
  • Loading branch information
joebonrichie committed Mar 16, 2024
1 parent d9e193d commit 0b9c75d
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions cli/delete_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,39 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) {
continue
}

size, err := getDirSize(p)
totalSize += size

if err != nil {
slog.Warn("Couldn't get directory size", "reason", err)
}

slog.Info(fmt.Sprintf("Removing cache directory '%s', of size '%s", p, humanReadableFormat(float64(size))))
var size int64

/* Parallelized file walk */
err = walk.Walk(p, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.IsDir() {
size += info.Size()
}

/* Remove if file */
if info.Mode().IsRegular() {
if err := os.Remove(path); err != nil {
slog.Warn("Could not remove file", "reason", err)
}
}

return err
})

slog.Info(fmt.Sprintf("Removed cache directory '%s', of size '%s", p, humanReadableFormat(float64(size))))

totalSize += size

/* Remove the remaining directories */
/* TODO: Remove() instead of RemoveAll() would be slightly faster here but the
* dirs would need to be sorted depth-first from the file walk */
if err := os.RemoveAll(p); err != nil {
log.Panic("Could not remove cache directory", "reason", err)
}
Expand Down

0 comments on commit 0b9c75d

Please sign in to comment.