Skip to content

Commit c34fa74

Browse files
committed
Replace hashmap cache to LRU cache
1 parent 4b977ba commit c34fa74

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

Cargo.lock

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ hyper = { git = "https://github.com/paritytech/hyper", default-features = false
2424
journaldb = { path = "../util/journaldb" }
2525
linked-hash-map = "0.5"
2626
log = "0.4.6"
27+
lru-cache = "0.1.2"
2728
kvdb = { path = "../util/kvdb" }
2829
kvdb-rocksdb = { path = "../util/kvdb-rocksdb" }
2930
kvdb-memorydb = { path = "../util/kvdb-memorydb" }

core/src/blockchain/body_db.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::mem;
1919
use std::sync::Arc;
2020

2121
use kvdb::{DBTransaction, KeyValueDB};
22+
use lru_cache::LruCache;
2223
use parking_lot::{Mutex, RwLock};
2324
use primitives::{Bytes, H256};
2425
use rlp::RlpStream;
@@ -30,9 +31,11 @@ use crate::db::{self, CacheUpdatePolicy, Readable, Writable};
3031
use crate::views::BlockView;
3132
use crate::{encoded, UnverifiedTransaction};
3233

34+
const BODY_CACHE_SIZE: usize = 1000;
35+
3336
pub struct BodyDB {
3437
// block cache
35-
body_cache: RwLock<HashMap<H256, Bytes>>,
38+
body_cache: Mutex<LruCache<H256, Bytes>>,
3639
parcel_address_cache: RwLock<HashMap<H256, TransactionAddress>>,
3740
pending_parcel_addresses: RwLock<HashMap<H256, Option<TransactionAddress>>>,
3841

@@ -48,7 +51,7 @@ impl BodyDB {
4851
/// Create new instance of blockchain from given Genesis.
4952
pub fn new(genesis: &BlockView, db: Arc<KeyValueDB>) -> Self {
5053
let bdb = Self {
51-
body_cache: RwLock::new(HashMap::new()),
54+
body_cache: Mutex::new(LruCache::new(BODY_CACHE_SIZE)),
5255
parcel_address_cache: RwLock::new(HashMap::new()),
5356
pending_parcel_addresses: RwLock::new(HashMap::new()),
5457

@@ -308,8 +311,8 @@ impl BodyProvider for BodyDB {
308311
fn block_body(&self, hash: &H256) -> Option<encoded::Body> {
309312
// Check cache first
310313
{
311-
let read = self.body_cache.read();
312-
if let Some(v) = read.get(hash) {
314+
let mut lock = self.body_cache.lock();
315+
if let Some(v) = lock.get_mut(hash) {
313316
return Some(encoded::Body::new(v.clone()))
314317
}
315318
}
@@ -319,8 +322,8 @@ impl BodyProvider for BodyDB {
319322
self.db.get(db::COL_BODIES, hash).expect("Low level database error. Some issue with disk?")?;
320323

321324
let raw_body = decompress(&compressed_body, blocks_swapper()).into_vec();
322-
let mut write = self.body_cache.write();
323-
write.insert(*hash, raw_body.clone());
325+
let mut lock = self.body_cache.lock();
326+
lock.insert(*hash, raw_body.clone());
324327

325328
Some(encoded::Body::new(raw_body))
326329
}

core/src/blockchain/headerchain.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::sync::Arc;
2121
use ctypes::header::{Header, Seal};
2222
use ctypes::BlockNumber;
2323
use kvdb::{DBTransaction, KeyValueDB};
24+
use lru_cache::LruCache;
2425
use parking_lot::{Mutex, RwLock};
2526
use primitives::{Bytes, H256};
2627
use rlp_compress::{blocks_swapper, compress, decompress};
@@ -35,6 +36,7 @@ use crate::views::HeaderView;
3536

3637
const BEST_HEADER_KEY: &[u8] = b"best-header";
3738
const BEST_PROPOSAL_HEADER_KEY: &[u8] = b"best-proposal-header";
39+
const HEADER_CACHE_SIZE: usize = 1000;
3840

3941
/// Structure providing fast access to blockchain data.
4042
///
@@ -47,7 +49,7 @@ pub struct HeaderChain {
4749
best_proposal_header_hash: RwLock<H256>,
4850

4951
// cache
50-
header_cache: RwLock<HashMap<H256, Bytes>>,
52+
header_cache: Mutex<LruCache<H256, Bytes>>,
5153
detail_cache: RwLock<HashMap<H256, BlockDetails>>,
5254
hash_cache: Mutex<HashMap<BlockNumber, H256>>,
5355

@@ -99,7 +101,7 @@ impl HeaderChain {
99101
best_header_hash: RwLock::new(best_header_hash),
100102
best_proposal_header_hash: RwLock::new(best_proposal_header_hash),
101103

102-
header_cache: RwLock::new(HashMap::new()),
104+
header_cache: Mutex::new(LruCache::new(HEADER_CACHE_SIZE)),
103105
detail_cache: Default::default(),
104106
hash_cache: Default::default(),
105107

@@ -401,11 +403,11 @@ impl HeaderProvider for HeaderChain {
401403
}
402404

403405
/// Get block header data
404-
fn block_header_data(hash: &H256, header_cache: &RwLock<HashMap<H256, Bytes>>, db: &KeyValueDB) -> Option<Vec<u8>> {
406+
fn block_header_data(hash: &H256, header_cache: &Mutex<LruCache<H256, Bytes>>, db: &KeyValueDB) -> Option<Vec<u8>> {
405407
// Check cache first
406408
{
407-
let read = header_cache.read();
408-
if let Some(v) = read.get(hash) {
409+
let mut lock = header_cache.lock();
410+
if let Some(v) = lock.get_mut(hash) {
409411
return Some(v.clone())
410412
}
411413
}
@@ -414,13 +416,13 @@ fn block_header_data(hash: &H256, header_cache: &RwLock<HashMap<H256, Bytes>>, d
414416

415417
let bytes = decompress(&b, blocks_swapper()).into_vec();
416418

417-
let mut write = header_cache.write();
418-
if let Some(v) = write.get(hash) {
419+
let mut lock = header_cache.lock();
420+
if let Some(v) = lock.get_mut(hash) {
419421
assert_eq!(&bytes, v);
420422
return Some(v.clone())
421423
}
422424

423-
write.insert(*hash, bytes.clone());
425+
lock.insert(*hash, bytes.clone());
424426

425427
Some(bytes)
426428
}

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern crate kvdb;
3838
extern crate kvdb_memorydb;
3939
extern crate kvdb_rocksdb;
4040
extern crate linked_hash_map;
41+
extern crate lru_cache;
4142
extern crate memorydb;
4243
extern crate num_rational;
4344
extern crate primitives;

0 commit comments

Comments
 (0)