From ea8edd653f520af8735b9ede517f945bdae140e5 Mon Sep 17 00:00:00 2001 From: clawbot Date: Tue, 17 Mar 2026 05:49:01 -0700 Subject: [PATCH] =?UTF-8?q?fix:=20replace=20O(n=C2=B2)=20duplicate=20detec?= =?UTF-8?q?tion=20with=20map-based=20O(1)=20lookups?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace linear scan deduplication of snapshot IDs in RemoveAllSnapshots() and PruneBlobs() with map[string]bool for O(1) lookups. Previously, each new snapshot ID was checked against the entire collected slice via a linear scan, resulting in O(n²) overall complexity. Now a 'seen' map provides constant-time membership checks while preserving insertion order in the slice. closes https://git.eeqj.de/sneak/vaultik/issues/12 --- internal/vaultik/snapshot.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/internal/vaultik/snapshot.go b/internal/vaultik/snapshot.go index 21e796d..87d5ee8 100644 --- a/internal/vaultik/snapshot.go +++ b/internal/vaultik/snapshot.go @@ -988,6 +988,7 @@ func (v *Vaultik) listAllRemoteSnapshotIDs() ([]string, error) { log.Info("Listing all snapshots") objectCh := v.Storage.ListStream(v.ctx, "metadata/") + seen := make(map[string]bool) var snapshotIDs []string for object := range objectCh { if object.Err != nil { @@ -1002,14 +1003,8 @@ func (v *Vaultik) listAllRemoteSnapshotIDs() ([]string, error) { } if strings.HasSuffix(object.Key, "/") || strings.Contains(object.Key, "/manifest.json.zst") { sid := parts[1] - found := false - for _, id := range snapshotIDs { - if id == sid { - found = true - break - } - } - if !found { + if !seen[sid] { + seen[sid] = true snapshotIDs = append(snapshotIDs, sid) } } -- 2.49.1