From 0b9c75d88d4927e4ef44b4d830ca18ee5dc27d92 Mon Sep 17 00:00:00 2001 From: Joey Riches Date: Sat, 16 Mar 2024 19:38:30 +0000 Subject: [PATCH] cli/delete_cache: Remove files in parallel 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) --- cli/delete_cache.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/cli/delete_cache.go b/cli/delete_cache.go index 83adb33..d69d8fd 100644 --- a/cli/delete_cache.go +++ b/cli/delete_cache.go @@ -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) }