Skip to content

feat: add Swap and Clear methods (#13)#23

Merged
millerjp merged 1 commit into
mainfrom
feat/swap-clear-13
Apr 20, 2026
Merged

feat: add Swap and Clear methods (#13)#23
millerjp merged 1 commit into
mainfrom
feat/swap-clear-13

Conversation

@millerjp

Copy link
Copy Markdown

Summary

Wraps sync.Map.Swap (Go 1.20) and sync.Map.Clear (Go 1.23) as methods on SyncMap. Zero API surface surprise — Swap mirrors the LoadAndDelete !loaded guard for typed-zero-on-miss; Clear is a bare pass-through.

Closes #13.

Added methods

func (m *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool)
func (m *SyncMap[K, V]) Clear()

Verification

$ make check
… (14/14 gates green, coverage 100%, govulncheck clean)

$ go test -race -count=10 -run 'TestSwap|TestClear|TestSwapContention' ./...
ok github.com/axonops/syncmap 1.300s

$ go test -bench='Swap|Clear' -benchmem -run=^$ -benchtime=50000x ./...
BenchmarkSwap-8              92.9 ns/op   71 B/op   2 allocs/op
BenchmarkSwapAbsent-8       159.9 ns/op  119 B/op   3 allocs/op
BenchmarkSwapParallel-8     226.9 ns/op   71 B/op   2 allocs/op
BenchmarkClear-8             75.2 ns/op  208 B/op   2 allocs/op  (includes one Store per iter)
BenchmarkClearParallel-8     39.4 ns/op  208 B/op   2 allocs/op

Agent gates

  • Plan — single-commit scope, placement between Delete and CompareAndSwap.
  • code-reviewer (post) — ship it; NITs non-actionable.
  • security-reviewer (post) — no blocking; Swap's !loaded guard prevents nil-interface type-assertion panic; atomics + WaitGroup correct.
  • performance-reviewer (post) — flagged BLOCKING missing SwapParallel / ClearParallel; both added in a follow-up edit. Now passing.
  • go-quality — ALL PASS (14/14).
  • commit-message-reviewer — PASS.

Deferrals

Test plan

  • CI green
  • Coverage remains 100%
  • Contention test stable under -race × 10

Wrap sync.Map.Swap (Go 1.20) and sync.Map.Clear (Go 1.23) as methods
on SyncMap. Swap mirrors the LoadAndDelete !loaded guard so it returns
the typed zero V when the key is absent — matching the established
pattern and preventing the nil-interface type-assertion panic path.
Clear is a bare pass-through to sync.Map.Clear.

Tests cover absent/present/zero-V-distinguished for Swap, empty-noop
/ populated-becomes-empty / subsequent-Store-works for Clear, plus a
100-goroutine contention test that asserts exactly one Swap observes
loaded=false on a fresh key.

Benchmarks added: BenchmarkSwap / SwapAbsent / SwapParallel and
BenchmarkClear / ClearParallel. Header comment in syncmap_bench_test.go
updated to cite both #13 and #14.

CHANGELOG entry deferred to #17.

Coverage remains at 100%.
@millerjp millerjp merged commit 6169e3f into main Apr 20, 2026
13 checks passed
@millerjp millerjp deleted the feat/swap-clear-13 branch April 20, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add Swap and Clear methods

1 participant