feat: add top-level CompareAndSwap and CompareAndDelete (#14)#22
Merged
feat: add top-level CompareAndSwap and CompareAndDelete (#14)#22
Conversation
Wrap sync.Map.CompareAndSwap and sync.Map.CompareAndDelete as package-level generic functions constrained to [K, V comparable]. SyncMap's own constraints stay [K comparable, V any] so non-comparable value types remain supported; the CAS operations simply reject them at compile time rather than panicking at runtime inside sync.Map. Godoc calls out the residual interface-V panic path (an interface V holding a dynamic non-comparable value still panics inside sync.Map comparison, matching Go's == semantics). Tests cover the four happy and unhappy paths per function, plus a 100-goroutine contention test that asserts exactly one CAS wins and the stored value matches the winner's id. Includes a comment block demonstrating the non-comparable-V compile error next to the happy path tests. Seeds syncmap_bench_test.go with CAS + CAD benchmarks (contended and uncontended). Full benchmark coverage across all public methods is owned by #15. CHANGELOG entry for the new functions is deferred to #17 (CHANGELOG owner). Coverage remains at 100%.
This was referenced Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds top-level generic
CompareAndSwapandCompareAndDeletefunctions, wrapping the correspondingsync.Mapmethods with a tighterV comparableconstraint.SyncMap[K, V any]stays unchanged — non-comparable V types remain supported, and CAS operations reject them at compile time rather than panicking at runtime.Closes #14.
Signatures
Verification
The 1 alloc/op on the success paths is the V→any boxing cost at the
sync.Mapboundary — the same cost rawsync.Mapusers pay. Target of "zero additional allocations beyond sync.Map itself" met.Agent gates
syncmap.gobetweenDeleteandRange.[K, V comparable]sugar semantics, named-return consistency, single-file rule.idstarts at 1 so the stored-0 seed can't be mistaken for a winner.syncmap_bench_test.go). Full public-method benchmark coverage remains with test: add benchmarks, runnable examples, and bench-regression infrastructure #15 as originally scoped.Deferrals (tracked elsewhere)
bench.txtbaseline, benchstat-regression-guard) — test: add benchmarks, runnable examples, and bench-regression infrastructure #15.Test plan
go doc CompareAndSwaprenders the interface-V caveat