It is possible to have two concurrently modified sets A, B, such that neither version of A is equal to any version of B, but == returns true.
Set equality is implemented like this:
|
impl<K: Eq + Hash, S: BuildHasher + Clone> PartialEq for DashSet<K, S> { |
|
fn eq(&self, other: &Self) -> bool { |
|
self.len() == other.len() && self.iter().all(|r| other.contains(r.key())) |
|
} |
|
} |
Consider the following sequence of operations:
- We start with
A = {1}, B = {2}.
- Thread 1 starts the comparison, verifying
A.len() == B.len().
- Thread 2 inserts
1 into B, resulting in B = {1, 2}.
- Thread 1 finishes the comparison, verifying that the sole element
1 of A is present in B.
A similar counterexample exists for maps.
This means that PartialEq is not just inefficient, but strictly speaking incorrect. If the intention is testing code, perhaps eq should lock the whole map/set before performing comparison? I'm guessing consistently locking the shards in order should avoid deadlocks.
It is possible to have two concurrently modified sets
A,B, such that neither version ofAis equal to any version ofB, but==returnstrue.Set equality is implemented like this:
dashmap/src/set.rs
Lines 386 to 390 in 366ce7e
Consider the following sequence of operations:
A = {1}, B = {2}.A.len() == B.len().1intoB, resulting inB = {1, 2}.1ofAis present inB.A similar counterexample exists for maps.
This means that
PartialEqis not just inefficient, but strictly speaking incorrect. If the intention is testing code, perhapseqshould lock the whole map/set before performing comparison? I'm guessing consistently locking the shards in order should avoid deadlocks.