Conversation
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
1 task
Contributor
Author
|
@spekary PTAL |
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
Concurrent first writes or
Clearcan race with unlocked backing-map checks inSafeMapandSafeSliceMap.SafeMap.Copycan also race withClearwhile initializing its destination, potentially copying into a nil map.Use an atomic initialization flag for the existing empty fast paths, with all backing-map access protected by the mutex. Publish initialization/reset during writes, decoding, cloning, and collection. Move
SafeMap.Copyinitialization under its write lock. Preserve zero-value use and existing nil-receiver iteration behavior.Add concurrent regression tests, initialization/clear/reuse and decoding coverage, benchmarks, and race detection to CI.
Performance
Local Go 1.23 benchmarks on linux/amd64 (Ryzen 9 7900), median of three 150ms runs:
These microbenchmarks show the empty-path locking penalty is avoided; results depend on hardware and workload. The flag adds 8 bytes per map instance on amd64, and initialization/reset paths publish atomic stores. Maps emptied through deletion retain their allocated backing map and use the locked path, as before.
Reproduce with
go test -run '^$' -bench Benchmark -benchmem -benchtime=150ms -count=3 -cpu=1,4.Validation
All passed with
GOTOOLCHAIN=go1.23.0:go test -race ./... -count=20 -cpu=1,2,4go test -race -cover ./... -coverprofile /tmp/maps-fastpath-coverage.out -coverpkg ./...(99.9% coverage)go vet ./...go build ./...Regression coverage includes concurrent Set/Clear/Copy/decoding/read/iteration, constructor and clone/collect publication, partial decode errors, and reuse after a recovered SafeSliceMap key panic. No dependency or Go version changes.