Skip to content

Commit 76ae0b9

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

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-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: 7 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;
@@ -32,7 +33,7 @@ use crate::{encoded, UnverifiedTransaction};
3233

3334
pub struct BodyDB {
3435
// block cache
35-
body_cache: RwLock<HashMap<H256, Bytes>>,
36+
body_cache: Mutex<LruCache<H256, Bytes>>,
3637
parcel_address_cache: RwLock<HashMap<H256, TransactionAddress>>,
3738
pending_parcel_addresses: RwLock<HashMap<H256, Option<TransactionAddress>>>,
3839

@@ -48,7 +49,7 @@ impl BodyDB {
4849
/// Create new instance of blockchain from given Genesis.
4950
pub fn new(genesis: &BlockView, db: Arc<KeyValueDB>) -> Self {
5051
let bdb = Self {
51-
body_cache: RwLock::new(HashMap::new()),
52+
body_cache: Mutex::new(LruCache::new(1000)),
5253
parcel_address_cache: RwLock::new(HashMap::new()),
5354
pending_parcel_addresses: RwLock::new(HashMap::new()),
5455

@@ -308,8 +309,8 @@ impl BodyProvider for BodyDB {
308309
fn block_body(&self, hash: &H256) -> Option<encoded::Body> {
309310
// Check cache first
310311
{
311-
let read = self.body_cache.read();
312-
if let Some(v) = read.get(hash) {
312+
let mut lock = self.body_cache.lock();
313+
if let Some(v) = lock.get_mut(hash) {
313314
return Some(encoded::Body::new(v.clone()))
314315
}
315316
}
@@ -319,8 +320,8 @@ impl BodyProvider for BodyDB {
319320
self.db.get(db::COL_BODIES, hash).expect("Low level database error. Some issue with disk?")?;
320321

321322
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());
323+
let mut lock = self.body_cache.lock();
324+
lock.insert(*hash, raw_body.clone());
324325

325326
Some(encoded::Body::new(raw_body))
326327
}

core/src/blockchain/headerchain.rs

Lines changed: 9 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};
@@ -47,7 +48,7 @@ pub struct HeaderChain {
4748
best_proposal_header_hash: RwLock<H256>,
4849

4950
// cache
50-
header_cache: RwLock<HashMap<H256, Bytes>>,
51+
header_cache: Mutex<LruCache<H256, Bytes>>,
5152
detail_cache: RwLock<HashMap<H256, BlockDetails>>,
5253
hash_cache: Mutex<HashMap<BlockNumber, H256>>,
5354

@@ -99,7 +100,7 @@ impl HeaderChain {
99100
best_header_hash: RwLock::new(best_header_hash),
100101
best_proposal_header_hash: RwLock::new(best_proposal_header_hash),
101102

102-
header_cache: RwLock::new(HashMap::new()),
103+
header_cache: Mutex::new(LruCache::new(1000)),
103104
detail_cache: Default::default(),
104105
hash_cache: Default::default(),
105106

@@ -401,11 +402,11 @@ impl HeaderProvider for HeaderChain {
401402
}
402403

403404
/// Get block header data
404-
fn block_header_data(hash: &H256, header_cache: &RwLock<HashMap<H256, Bytes>>, db: &KeyValueDB) -> Option<Vec<u8>> {
405+
fn block_header_data(hash: &H256, header_cache: &Mutex<LruCache<H256, Bytes>>, db: &KeyValueDB) -> Option<Vec<u8>> {
405406
// Check cache first
406407
{
407-
let read = header_cache.read();
408-
if let Some(v) = read.get(hash) {
408+
let mut lock = header_cache.lock();
409+
if let Some(v) = lock.get_mut(hash) {
409410
return Some(v.clone())
410411
}
411412
}
@@ -414,13 +415,13 @@ fn block_header_data(hash: &H256, header_cache: &RwLock<HashMap<H256, Bytes>>, d
414415

415416
let bytes = decompress(&b, blocks_swapper()).into_vec();
416417

417-
let mut write = header_cache.write();
418-
if let Some(v) = write.get(hash) {
418+
let mut lock = header_cache.lock();
419+
if let Some(v) = lock.get_mut(hash) {
419420
assert_eq!(&bytes, v);
420421
return Some(v.clone())
421422
}
422423

423-
write.insert(*hash, bytes.clone());
424+
lock.insert(*hash, bytes.clone());
424425

425426
Some(bytes)
426427
}

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)