Skip to content

Commit 2705b95

Browse files
author
Ludo Galabru
committed
feat: revisit caching strategy
1 parent e794542 commit 2705b95

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

components/chainhook-event-observer/src/hord/db/mod.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
714714
let mut cursor = start_block as usize;
715715
let mut inbox = HashMap::new();
716716
let mut num_writes = 0;
717-
let traversals_cache = Arc::new(new_traversals_lazy_cache());
717+
let traversals_cache = Arc::new(new_traversals_lazy_cache(hord_config.cache_size));
718718

719719
while let Ok(Some((block_height, compacted_block, raw_block))) = block_compressed_rx.recv() {
720720
insert_entry_in_blocks(block_height, &compacted_block, &blocks_db_rw, &ctx);
@@ -792,18 +792,20 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
792792
return Ok(());
793793
}
794794

795-
if num_writes % 24 == 0 {
796-
ctx.try_log(|logger| {
797-
slog::info!(
798-
logger,
799-
"Flushing traversals cache (#{} entries)",
800-
traversals_cache.len()
801-
);
802-
});
803-
traversals_cache.clear();
795+
if !traversals_cache.is_empty() {
796+
if num_writes % 128 == 0 {
797+
ctx.try_log(|logger| {
798+
slog::info!(
799+
logger,
800+
"Flushing traversals cache (#{} entries)",
801+
traversals_cache.len()
802+
);
803+
});
804+
traversals_cache.shrink_to_fit();
805+
}
804806
}
805807

806-
if num_writes % 128 == 0 {
808+
if num_writes % 512 == 0 {
807809
ctx.try_log(|logger| {
808810
slog::info!(logger, "Flushing DB to disk ({num_writes} inserts)");
809811
});

components/chainhook-event-observer/src/hord/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rocksdb::DB;
1818
use rusqlite::Connection;
1919
use std::collections::{BTreeMap, HashMap, VecDeque};
2020
use std::hash::BuildHasherDefault;
21+
use std::ops::Div;
2122
use std::path::PathBuf;
2223
use std::sync::mpsc::channel;
2324
use std::sync::Arc;
@@ -208,9 +209,14 @@ pub fn new_traversals_cache(
208209
}
209210

210211
pub fn new_traversals_lazy_cache(
212+
cache_size: usize,
211213
) -> DashMap<(u32, [u8; 8]), LazyBlockTransaction, BuildHasherDefault<FxHasher>> {
212214
let hasher = FxBuildHasher::default();
213-
DashMap::with_hasher(hasher)
215+
DashMap::with_capacity_and_hasher(
216+
((cache_size.saturating_sub(500)) * 1000 * 1000)
217+
.div(LazyBlockTransaction::get_average_bytes_size()),
218+
hasher,
219+
)
214220
}
215221

216222
pub fn retrieve_inscribed_satoshi_points_from_block(

0 commit comments

Comments
 (0)