-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Uses SeqLock for CachedAccountInner::hash #33696
Uses SeqLock for CachedAccountInner::hash #33696
Conversation
73427be
to
18de3ac
Compare
Codecov Report
@@ Coverage Diff @@
## master #33696 +/- ##
=======================================
Coverage 81.8% 81.8%
=======================================
Files 806 806
Lines 217894 217893 -1
=======================================
+ Hits 178252 178262 +10
+ Misses 39642 39631 -11 |
Great stuff, this seems like the ideal case for a seqlock: the hash is small so even in the write contended case readers will likely never spin more than once or twice. Death to syscalls! |
Sorry I'm doing a lot of cargo build today so I have time to kill 😂 You could make the bench more realistic by holding [u8; 32] inside the locks, and making sure the rwlock benches copy the array too |
Yep! I thought about doing this, but initially wanted the seqlock repo to just benchmark the locking and not any effects from copying. I agree that using a |
Thank you for taking the time to look/review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. lgtm.
Now that solana-labs/solana-program-library#5597 has merged, SPL builds should be good too. |
Problem
The
CachedAccountInner::hash
field is protected with an RwLock to ensure the cached account hash is read/written safely. SinceHash
isCopy
, we can use a lighter-weight synchronization mechanism that is faster than RwLock.Summary of Changes
Use
SeqLock
instead ofRwLock
forCachedAccountInner::hash
.A sequence lock
read
needs just two atomic loads, and awrite
locks a mutex plus does two atomic stores. Readers will retry if there is a write in between their two loads. Since we always need a hash value (i.e. non-reference), we're not wasting any time copying bytes around either.Performance Comparisons
Here's some results micro-benchmarking[1] results from my macbook. This compares SeqLock with RwLock from std and parking_lot.
[1]: Here's the code for the benchmark: https://github.com/brooksprumo/seqlock/blob/master/benches/bench_seqlock.rs
First is when there are only readers:
And here's about 50/50 readers and writers:
I know micro-benchmarks are not really indicative of true speedups, but hopefully this shows that SeqLock is orders of magnitude faster than std's RwLock. Any reduction in lock contention will likely have a positive result on the system as a whole.