Skip to content

Commit 5bd75ef

Browse files
committed
Decrease block cache size, increase execution state cache size
1 parent d61401d commit 5bd75ef

File tree

3 files changed

+35
-54
lines changed

3 files changed

+35
-54
lines changed

linera-core/src/unit_tests/value_cache_tests.rs

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ use linera_base::{
1111
};
1212
use linera_chain::types::Timeout;
1313

14-
use super::{ValueCache, DEFAULT_VALUE_CACHE_SIZE};
14+
use super::ValueCache;
15+
16+
/// Test cache size for unit tests.
17+
const TEST_CACHE_SIZE: usize = 10;
1518

1619
/// Tests attempt to retrieve non-existent value.
1720
#[test]
1821
fn test_retrieve_missing_value() {
19-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
22+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
2023
let hash = CryptoHash::test_hash("Missing value");
2124

2225
assert!(cache.get(&hash).is_none());
@@ -26,7 +29,7 @@ fn test_retrieve_missing_value() {
2629
/// Tests inserting a certificate value in the cache.
2730
#[test]
2831
fn test_insert_single_certificate_value() {
29-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
32+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
3033
let value = create_dummy_certificate_value(0);
3134
let hash = value.hash();
3235

@@ -39,9 +42,8 @@ fn test_insert_single_certificate_value() {
3942
/// Tests inserting many certificate values in the cache, one-by-one.
4043
#[test]
4144
fn test_insert_many_certificate_values_individually() {
42-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
43-
let values =
44-
create_dummy_certificate_values(0..(DEFAULT_VALUE_CACHE_SIZE as u64)).collect::<Vec<_>>();
45+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
46+
let values = create_dummy_certificate_values(0..(TEST_CACHE_SIZE as u64)).collect::<Vec<_>>();
4547

4648
for value in &values {
4749
assert!(cache.insert(Cow::Borrowed(value)));
@@ -61,9 +63,8 @@ fn test_insert_many_certificate_values_individually() {
6163
/// Tests inserting many values in the cache, all-at-once.
6264
#[test]
6365
fn test_insert_many_values_together() {
64-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
65-
let values =
66-
create_dummy_certificate_values(0..(DEFAULT_VALUE_CACHE_SIZE as u64)).collect::<Vec<_>>();
66+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
67+
let values = create_dummy_certificate_values(0..(TEST_CACHE_SIZE as u64)).collect::<Vec<_>>();
6768

6869
cache.insert_all(values.iter().map(Cow::Borrowed));
6970

@@ -81,9 +82,8 @@ fn test_insert_many_values_together() {
8182
/// Tests re-inserting many values in the cache, all-at-once.
8283
#[test]
8384
fn test_reinsertion_of_values() {
84-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
85-
let values =
86-
create_dummy_certificate_values(0..(DEFAULT_VALUE_CACHE_SIZE as u64)).collect::<Vec<_>>();
85+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
86+
let values = create_dummy_certificate_values(0..(TEST_CACHE_SIZE as u64)).collect::<Vec<_>>();
8787

8888
cache.insert_all(values.iter().map(Cow::Borrowed));
8989

@@ -105,9 +105,8 @@ fn test_reinsertion_of_values() {
105105
/// Tests eviction of one entry.
106106
#[test]
107107
fn test_one_eviction() {
108-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
109-
let values =
110-
create_dummy_certificate_values(0..=(DEFAULT_VALUE_CACHE_SIZE as u64)).collect::<Vec<_>>();
108+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
109+
let values = create_dummy_certificate_values(0..=(TEST_CACHE_SIZE as u64)).collect::<Vec<_>>();
111110

112111
cache.insert_all(values.iter().map(Cow::Borrowed));
113112

@@ -128,18 +127,12 @@ fn test_one_eviction() {
128127
/// Tests eviction of the second entry.
129128
#[test]
130129
fn test_eviction_of_second_entry() {
131-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
132-
let values =
133-
create_dummy_certificate_values(0..=(DEFAULT_VALUE_CACHE_SIZE as u64)).collect::<Vec<_>>();
134-
135-
cache.insert_all(
136-
values
137-
.iter()
138-
.take(DEFAULT_VALUE_CACHE_SIZE)
139-
.map(Cow::Borrowed),
140-
);
130+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
131+
let values = create_dummy_certificate_values(0..=(TEST_CACHE_SIZE as u64)).collect::<Vec<_>>();
132+
133+
cache.insert_all(values.iter().take(TEST_CACHE_SIZE).map(Cow::Borrowed));
141134
cache.get(&values[0].hash());
142-
assert!(cache.insert(Cow::Borrowed(&values[DEFAULT_VALUE_CACHE_SIZE])));
135+
assert!(cache.insert(Cow::Borrowed(&values[TEST_CACHE_SIZE])));
143136

144137
assert!(cache.contains(&values[0].hash()));
145138
assert_eq!(cache.get(&values[0].hash()).as_ref(), Some(&values[0]));
@@ -167,18 +160,12 @@ fn test_eviction_of_second_entry() {
167160
/// Tests if reinsertion of the first entry promotes it so that it's not evicted so soon.
168161
#[test]
169162
fn test_promotion_of_reinsertion() {
170-
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::default();
171-
let values =
172-
create_dummy_certificate_values(0..=(DEFAULT_VALUE_CACHE_SIZE as u64)).collect::<Vec<_>>();
173-
174-
cache.insert_all(
175-
values
176-
.iter()
177-
.take(DEFAULT_VALUE_CACHE_SIZE)
178-
.map(Cow::Borrowed),
179-
);
163+
let cache = ValueCache::<CryptoHash, Hashed<Timeout>>::new(TEST_CACHE_SIZE);
164+
let values = create_dummy_certificate_values(0..=(TEST_CACHE_SIZE as u64)).collect::<Vec<_>>();
165+
166+
cache.insert_all(values.iter().take(TEST_CACHE_SIZE).map(Cow::Borrowed));
180167
assert!(!cache.insert(Cow::Borrowed(&values[0])));
181-
assert!(cache.insert(Cow::Borrowed(&values[DEFAULT_VALUE_CACHE_SIZE])));
168+
assert!(cache.insert(Cow::Borrowed(&values[TEST_CACHE_SIZE])));
182169

183170
assert!(cache.contains(&values[0].hash()));
184171
assert_eq!(cache.get(&values[0].hash()).as_ref(), Some(&values[0]));

linera-core/src/value_cache.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ use std::{borrow::Cow, hash::Hash, num::NonZeroUsize, sync::Mutex};
1414
use linera_base::{crypto::CryptoHash, hashed::Hashed};
1515
use lru::LruCache;
1616

17-
/// The default cache size.
18-
pub const DEFAULT_VALUE_CACHE_SIZE: usize = 10_000;
19-
2017
/// A counter metric for the number of cache hits in the [`ValueCache`].
2118
#[cfg(with_metrics)]
2219
mod metrics {
@@ -51,24 +48,18 @@ where
5148
cache: Mutex<LruCache<K, V>>,
5249
}
5350

54-
impl<K, V> Default for ValueCache<K, V>
51+
impl<K, V> ValueCache<K, V>
5552
where
5653
K: Hash + Eq + PartialEq + Copy,
5754
{
58-
fn default() -> Self {
59-
let size = NonZeroUsize::try_from(DEFAULT_VALUE_CACHE_SIZE)
60-
.expect("Default cache size is larger than zero");
61-
55+
/// Creates a new `ValueCache` with the given size.
56+
pub fn new(size: usize) -> Self {
57+
let size = NonZeroUsize::try_from(size).expect("Cache size is larger than zero");
6258
ValueCache {
6359
cache: Mutex::new(LruCache::new(size)),
6460
}
6561
}
66-
}
6762

68-
impl<K, V> ValueCache<K, V>
69-
where
70-
K: Hash + Eq + PartialEq + Copy,
71-
{
7263
/// Inserts a `V` into the cache, if it's not already present.
7364
pub fn insert_owned(&self, key: &K, value: V) -> bool {
7465
let mut cache = self.cache.lock().unwrap();

linera-core/src/worker.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ use crate::{
4343
value_cache::ValueCache,
4444
};
4545

46+
const BLOCK_CACHE_SIZE: usize = 5_000;
47+
const EXECUTION_STATE_CACHE_SIZE: usize = 10_000;
48+
4649
#[cfg(test)]
4750
#[path = "unit_tests/worker_tests.rs"]
4851
mod worker_tests;
@@ -345,8 +348,8 @@ where
345348
nickname,
346349
storage,
347350
chain_worker_config: ChainWorkerConfig::default().with_key_pair(key_pair),
348-
block_cache: Arc::new(ValueCache::default()),
349-
execution_state_cache: Arc::new(ValueCache::default()),
351+
block_cache: Arc::new(ValueCache::new(BLOCK_CACHE_SIZE)),
352+
execution_state_cache: Arc::new(ValueCache::new(EXECUTION_STATE_CACHE_SIZE)),
350353
tracked_chains: None,
351354
delivery_notifiers: Arc::default(),
352355
chain_worker_tasks: Arc::default(),
@@ -364,8 +367,8 @@ where
364367
nickname,
365368
storage,
366369
chain_worker_config: ChainWorkerConfig::default(),
367-
block_cache: Arc::new(ValueCache::default()),
368-
execution_state_cache: Arc::new(ValueCache::default()),
370+
block_cache: Arc::new(ValueCache::new(BLOCK_CACHE_SIZE)),
371+
execution_state_cache: Arc::new(ValueCache::new(EXECUTION_STATE_CACHE_SIZE)),
369372
tracked_chains: Some(tracked_chains),
370373
delivery_notifiers: Arc::default(),
371374
chain_worker_tasks: Arc::default(),

0 commit comments

Comments
 (0)