Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

LenCachingMutex #9988

Merged
merged 6 commits into from
Dec 4, 2018
Merged

LenCachingMutex #9988

merged 6 commits into from
Dec 4, 2018

Conversation

mattrutherford
Copy link
Contributor

While working on another issue I developed a way to query the size of a collection held in a Mutex without requiring a lock. Ultimately I found a a different way to solve the issue without needing this, but it may be useful anyway. I've included a commit to show it used in Verification. If another thread has the lock while load_len() is called, the cached value may not be accurate. However this is often not too different to normal operation - stored len() may be stale at any point after the Guard is dropped.

@parity-cla-bot
Copy link

It looks like @mattrutherford signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@sorpaas sorpaas added the A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. label Nov 29, 2018
@5chdn 5chdn added the M4-core ⛓ Core client code / Rust. label Nov 29, 2018
@5chdn 5chdn added this to the 2.3 milestone Nov 29, 2018
Copy link
Collaborator

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

Awesome!

util/len-caching-mutex/src/lib.rs Outdated Show resolved Hide resolved
LenCachingMutex can be used in place of a Mutex, when working
with collections, or other types with a len() method. When
the Guard is dropped, the value returned from len() is stored
into an AtomicUsize and can be queried using load_len() without
needing to lock the Mutex.
@ordian ordian added A8-looksgood 🦄 Pull request is reviewed well. and removed A3-inprogress ⏳ Pull request is in progress. No review needed at this stage. labels Dec 4, 2018
@mattrutherford mattrutherford changed the title [WIP] LenCachingMutex LenCachingMutex Dec 4, 2018
@mattrutherford mattrutherford merged commit ec886dd into master Dec 4, 2018
@mattrutherford mattrutherford deleted the mr/len-caching-mutex branch December 4, 2018 14:33
self.len.load(Ordering::SeqCst)
}

pub fn lock(&self) -> Guard<T> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remember to add docs for public functions in the future!

niklasad1 pushed a commit that referenced this pull request Dec 16, 2018
New util LenCachingMutex can be used in place of a Mutex, when working
with collections, or other types with a len() method. When
the Guard is dropped, the value returned from len() is stored
into an AtomicUsize and can be queried using load_len() without
needing to lock the Mutex. Implementations for ```Vec``` and
```VecDeque```.

Now used in [Verification](https://github.com/paritytech/parity-ethereum/blob/4ded4181a651fe9c26850f908e4ab63bf952cab6/ethcore/src/verification/queue/mod.rs#L196) so that calls to ```VerificationQueue.queue_info()```
no longer require locks.
insipx pushed a commit to insipx/parity-ethereum that referenced this pull request Dec 17, 2018
New util LenCachingMutex can be used in place of a Mutex, when working
with collections, or other types with a len() method. When
the Guard is dropped, the value returned from len() is stored
into an AtomicUsize and can be queried using load_len() without
needing to lock the Mutex. Implementations for ```Vec``` and
```VecDeque```.

Now used in [Verification](https://github.com/paritytech/parity-ethereum/blob/4ded4181a651fe9c26850f908e4ab63bf952cab6/ethcore/src/verification/queue/mod.rs#L196) so that calls to ```VerificationQueue.queue_info()```
no longer require locks.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants