fix: replace O(n²) duplicate detection with map-based O(1) lookups #45
Reference in New Issue
Block a user
Delete Branch "fix/dedup-snapshot-ids-on2-to-o1"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Replace linear scan deduplication of snapshot IDs in
RemoveAllSnapshots()andPruneBlobs()withmap[string]boolfor 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
seenmap provides constant-time membership checks while preserving insertion order in the slice.Changes:
internal/vaultik/snapshot.go(RemoveAllSnapshots): replaced linearforloop dedup withseenmapinternal/vaultik/prune.go(PruneBlobs): replaced linearforloop dedup withseenmapcloses #12
Review: PASS ✅
What was reviewed: PR #45 — replace O(n²) duplicate detection with map-based O(1) lookups, fixing issue #12.
Changes verified:
internal/vaultik/snapshot.go(RemoveAllSnapshots): linear scan dedup replaced withseenmap — correctinternal/vaultik/prune.go(PruneBlobs): same pattern applied — correctsnapshotIDsslice; theseenmap is only used for O(1) membership checksseen[id] = trueis set before theappend, keeping the map and slice in syncIntegrity checks:
docker build .passes (lint, fmt-check, tests all green)No issues found.
conflict
6522ccea75toea8edd653fRebased onto main, resolving merge conflict in
internal/vaultik/prune.go.The conflict arose because PR #41 refactored
PruneBlobs()to extract snapshot listing intolistUniqueSnapshotIDs(), which already uses theseenmap pattern. So theprune.gochange from this PR is no longer needed — it's already incorporated.The remaining diff (1 file, +3/-8) applies the
seenmap fix tolistAllRemoteSnapshotIDs()insnapshot.go, which still had the O(n²) linear scan.docker build .passes (lint, fmt-check, all tests green).REVIEW: PASS ✅
Post-rebase review of PR #45 addressing issue #12.
Diff: 1 file, +3/-8 in
internal/vaultik/snapshot.go— clean and minimal.What changed
listAllRemoteSnapshotIDspreviously used an O(n²) linear scan to deduplicate snapshot IDs:Replaced with O(1) map-based dedup:
Verification
seenmap correctly applied — initialized before the loop, checked before append, set on insert. Preserves insertion order in the slice while deduplicating via the map. ✅found := falsepatterns in production code. The only remaining instances are ininternal/cli/entry_test.go(test assertions, not snapshot dedup). All otherfor _, snapshotID := range snapshotIDsloops inprune.go,info.go, andsnapshot.goare processing loops over already-deduplicated IDs. ✅prune.gochange from original PR — correctly already incorporated by PR #41's refactoring. No remaining diff needed. ✅docker build .— passes (lint, fmt-check, tests, build). ✅Recommendation: merge-ready.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.