docs(skiplist): document SkipMap::compare_insert concurrency - #1305
Open
teddytennant wants to merge 1 commit into
Open
teddytennant wants to merge 1 commit into
teddytennant wants to merge 1 commit into
Conversation
Explain that compare_insert installs a successful replacement without leaving the key absent, that concurrent callers can still race, how the return entry is chosen, and that the operation is not an in-place value CAS. Mirror the existing get_or_insert_with race note on get_or_insert. Fixes crossbeam-rs#1122
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.
Problem
Issue #1122 asks whether
SkipMap::compare_insertis atomic, what happens under contention, and what the return value means. The existing docs only said that a matching entry is "removed before inserting the new one", which looked like two separate steps and did not cover races between concurrent callers. The same race note already present onget_or_insert_withwas also missing fromget_or_insert.Root cause
compare_insertis implemented via the shared lock-freeinsert_internalpath. A successful replacement links a new node and only then marks the previous node removed, so lookups do not see a gap where the key is absent. Concurrent callers can still race:compare_fnmay run more than once, lost races drop the unused value, and the returnedEntryis either the newly published node or the surviving existing one. That is not the same as an in-place atomic CAS on the stored value.Fix
Document the observable semantics on:
SkipMap::compare_insert(public map API)SkipList::compare_insert(base API)insert_internal(shared implementation note)SkipMap::get_or_insert/SkipList::get_or_insert(same race note style asget_or_insert_with, as requested in the issue)Docs only; no behavior change. Distinct from open PR #1124, which only reworded "using CAS" without describing the semantics maintainers asked for.
Test plan
cargo test --features std --test map -- comparecargo test --features std get_or_insertcargo test --doc --features std compare_insertFixes #1122