Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove large amounts of unsafe from Refs via 'detached' lock guards #323

Merged
merged 6 commits into from
Feb 1, 2025

Conversation

conradludgate
Copy link
Contributor

(stacked on top of #322)

I came up with this novel abstraction for reducing the dependencies on pointers within dashmap. We need to hold the lock guard for the shard.

Since Ref/Mut wants to get proper reference values eventually, it doesn't matter whether you convert pointers early or later. It seems to work all the same.

Thus, we can convert

pub struct Ref<'a, K, V> {
    _guard: RwLockReadGuard<'a, HashMap<K, V>>,
    k: *const K,
    v: *const V,
}

into

pub struct Ref<'a, K, V> {
    _guard: RwLockReadGuardDetached<'a>,
    k: &'a K,
    v: &'a V,
}

Since the detached guard does not own the data, it's inherently unsafe, but from my understanding and from asking others, it is still sound as long as the data is still coupled with the guard. Ref/Entry APIs still hold the guard, which upholds this invariant.

As a by-product of using this in mapref::multiple, I noticed that the RwLock guard is marked as GuardNotSend, meanwhile every iterator/ref is manually marked as Send/Sync. This seemed redundant, so I've additionally made the guard Send and then removed all manual send/sync impls.

@conradludgate conradludgate mentioned this pull request Feb 1, 2025
Copy link
Owner

@xacrimon xacrimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@xacrimon xacrimon merged commit 7fb4eb4 into xacrimon:master Feb 1, 2025
6 checks passed
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.

2 participants