Skip to content

Commit 78ee7ec

Browse files
committed
feat: shared LRU block cache across all three DBs
Single shared cache for txstore, history, and cache DBs instead of per-DB caches. --db-block-cache-mb now controls the total cache size. Eliminates wasted allocation on the tiny cache_db and lets txstore and history claim cache proportional to actual usage via LRU eviction.
1 parent 74e4b61 commit 78ee7ec

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/new_index/db.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ pub enum DBFlush {
9191

9292
impl DB {
9393
pub fn open(path: &Path, config: &Config, verify_compat: bool) -> DB {
94+
Self::open_with_cache(path, config, verify_compat, None)
95+
}
96+
97+
pub fn open_with_cache(path: &Path, config: &Config, verify_compat: bool, shared_cache: Option<&rocksdb::Cache>) -> DB {
9498
debug!("opening DB at {:?}", path);
9599
let mut db_opts = rocksdb::Options::default();
96100
db_opts.create_if_missing(true);
@@ -148,8 +152,16 @@ impl DB {
148152

149153
// Configure block cache and table options
150154
let mut block_opts = rocksdb::BlockBasedOptions::default();
151-
let cache_size_bytes = config.db_block_cache_mb * 1024 * 1024;
152-
block_opts.set_block_cache(&rocksdb::Cache::new_lru_cache(cache_size_bytes));
155+
let owned_cache;
156+
let cache = match shared_cache {
157+
Some(c) => c,
158+
None => {
159+
let cache_size_bytes = config.db_block_cache_mb * 1024 * 1024;
160+
owned_cache = rocksdb::Cache::new_lru_cache(cache_size_bytes);
161+
&owned_cache
162+
}
163+
};
164+
block_opts.set_block_cache(cache);
153165
// Store index and filter blocks inside the block cache so their memory is
154166
// bounded by --db-block-cache-mb. Without this, RocksDB allocates table-reader
155167
// memory (index + filter blocks) on the heap separately for every open SST file.

src/new_index/schema.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,24 @@ impl Store {
6161
pub fn open(config: &Config, metrics: &Metrics, verify_compat: bool) -> Self {
6262
let path = config.db_path.join("newindex");
6363

64-
let txstore_db = DB::open(&path.join("txstore"), config, verify_compat);
64+
// Create a single shared LRU cache for all three DBs. The total size is
65+
// --db-block-cache-mb (not multiplied by 3). RocksDB's LRU cache is
66+
// thread-safe, so all DBs share one eviction pool. This lets the
67+
// txstore (which holds the bulk of the data) claim as much cache as it
68+
// needs without being artificially capped at 1/3 of the total.
69+
let cache_size_bytes = config.db_block_cache_mb * 1024 * 1024;
70+
let shared_cache = rocksdb::Cache::new_lru_cache(cache_size_bytes);
71+
info!("shared LRU block cache: db_block_cache_mb='{}'", config.db_block_cache_mb);
72+
73+
let txstore_db = DB::open_with_cache(&path.join("txstore"), config, verify_compat, Some(&shared_cache));
6574
let added_blockhashes = load_blockhashes(&txstore_db, &BlockRow::done_filter());
6675
debug!("{} blocks were added", added_blockhashes.len());
6776

68-
let history_db = DB::open(&path.join("history"), config, verify_compat);
77+
let history_db = DB::open_with_cache(&path.join("history"), config, verify_compat, Some(&shared_cache));
6978
let indexed_blockhashes = load_blockhashes(&history_db, &BlockRow::done_filter());
7079
debug!("{} blocks were indexed", indexed_blockhashes.len());
7180

72-
let cache_db = DB::open(&path.join("cache"), config, verify_compat);
81+
let cache_db = DB::open_with_cache(&path.join("cache"), config, verify_compat, Some(&shared_cache));
7382

7483
let db_metrics = Arc::new(RocksDbMetrics::new(&metrics));
7584
txstore_db.start_stats_exporter(Arc::clone(&db_metrics), "txstore_db");

0 commit comments

Comments
 (0)