Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion beacon_node/beacon_chain/src/attester_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,19 @@ impl AttesterCache {
spec: &ChainSpec,
) -> Result<(), Error> {
let key = AttesterCacheKey::new(state.current_epoch(), state, latest_block_root)?;

// Fast path: check with read lock first to avoid write lock contention.
if self.cache.read().contains_key(&key) {
return Ok(());
}

// Slow path: create cache item outside of lock to avoid holding write lock during
// expensive committee cache initialization.
let cache_item = AttesterCacheValue::new(state, spec)?;

let mut cache = self.cache.write();
// Check again in case another thread inserted while we were creating the cache item.
if !cache.contains_key(&key) {
let cache_item = AttesterCacheValue::new(state, spec)?;
Self::insert_respecting_max_len(&mut cache, key, cache_item);
}
Ok(())
Expand Down