Skip to content

docs(skiplist): document SkipMap::compare_insert concurrency - #1305

Open
teddytennant wants to merge 1 commit into
crossbeam-rs:mainfrom
teddytennant:docs/skipmap-compare-insert-1122
Open

teddytennant wants to merge 1 commit into
crossbeam-rs:mainfrom
teddytennant:docs/skipmap-compare-insert-1122

Conversation

@teddytennant

Copy link
Copy Markdown
Contributor

Problem

Issue #1122 asks whether SkipMap::compare_insert is 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 on get_or_insert_with was also missing from get_or_insert.

Root cause

compare_insert is implemented via the shared lock-free insert_internal path. 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_fn may run more than once, lost races drop the unused value, and the returned Entry is 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 as get_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 -- compare
  • cargo test --features std get_or_insert
  • cargo test --doc --features std compare_insert

Fixes #1122

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Document the semantics of SkipMap::compare_insert

2 participants