Skip to content

Replace hashmap cache to LRU cache #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hyper = { git = "https://github.com/paritytech/hyper", default-features = false
journaldb = { path = "../util/journaldb" }
linked-hash-map = "0.5"
log = "0.4.6"
lru-cache = "0.1.2"
kvdb = { path = "../util/kvdb" }
kvdb-rocksdb = { path = "../util/kvdb-rocksdb" }
kvdb-memorydb = { path = "../util/kvdb-memorydb" }
Expand Down
15 changes: 9 additions & 6 deletions core/src/blockchain/body_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::mem;
use std::sync::Arc;

use kvdb::{DBTransaction, KeyValueDB};
use lru_cache::LruCache;
use parking_lot::{Mutex, RwLock};
use primitives::{Bytes, H256};
use rlp::RlpStream;
Expand All @@ -30,9 +31,11 @@ use crate::db::{self, CacheUpdatePolicy, Readable, Writable};
use crate::views::BlockView;
use crate::{encoded, UnverifiedTransaction};

const BODY_CACHE_SIZE: usize = 1000;

pub struct BodyDB {
// block cache
body_cache: RwLock<HashMap<H256, Bytes>>,
body_cache: Mutex<LruCache<H256, Bytes>>,
parcel_address_cache: RwLock<HashMap<H256, TransactionAddress>>,
pending_parcel_addresses: RwLock<HashMap<H256, Option<TransactionAddress>>>,

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

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

let raw_body = decompress(&compressed_body, blocks_swapper()).into_vec();
let mut write = self.body_cache.write();
write.insert(*hash, raw_body.clone());
let mut lock = self.body_cache.lock();
lock.insert(*hash, raw_body.clone());

Some(encoded::Body::new(raw_body))
}
Expand Down
18 changes: 10 additions & 8 deletions core/src/blockchain/headerchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::sync::Arc;
use ctypes::header::{Header, Seal};
use ctypes::BlockNumber;
use kvdb::{DBTransaction, KeyValueDB};
use lru_cache::LruCache;
use parking_lot::{Mutex, RwLock};
use primitives::{Bytes, H256};
use rlp_compress::{blocks_swapper, compress, decompress};
Expand All @@ -35,6 +36,7 @@ use crate::views::HeaderView;

const BEST_HEADER_KEY: &[u8] = b"best-header";
const BEST_PROPOSAL_HEADER_KEY: &[u8] = b"best-proposal-header";
const HEADER_CACHE_SIZE: usize = 1000;

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

// cache
header_cache: RwLock<HashMap<H256, Bytes>>,
header_cache: Mutex<LruCache<H256, Bytes>>,
detail_cache: RwLock<HashMap<H256, BlockDetails>>,
hash_cache: Mutex<HashMap<BlockNumber, H256>>,

Expand Down Expand Up @@ -99,7 +101,7 @@ impl HeaderChain {
best_header_hash: RwLock::new(best_header_hash),
best_proposal_header_hash: RwLock::new(best_proposal_header_hash),

header_cache: RwLock::new(HashMap::new()),
header_cache: Mutex::new(LruCache::new(HEADER_CACHE_SIZE)),
detail_cache: Default::default(),
hash_cache: Default::default(),

Expand Down Expand Up @@ -401,11 +403,11 @@ impl HeaderProvider for HeaderChain {
}

/// Get block header data
fn block_header_data(hash: &H256, header_cache: &RwLock<HashMap<H256, Bytes>>, db: &KeyValueDB) -> Option<Vec<u8>> {
fn block_header_data(hash: &H256, header_cache: &Mutex<LruCache<H256, Bytes>>, db: &KeyValueDB) -> Option<Vec<u8>> {
// Check cache first
{
let read = header_cache.read();
if let Some(v) = read.get(hash) {
let mut lock = header_cache.lock();
if let Some(v) = lock.get_mut(hash) {
return Some(v.clone())
}
}
Expand All @@ -414,13 +416,13 @@ fn block_header_data(hash: &H256, header_cache: &RwLock<HashMap<H256, Bytes>>, d

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

let mut write = header_cache.write();
if let Some(v) = write.get(hash) {
let mut lock = header_cache.lock();
if let Some(v) = lock.get_mut(hash) {
assert_eq!(&bytes, v);
return Some(v.clone())
}

write.insert(*hash, bytes.clone());
lock.insert(*hash, bytes.clone());

Some(bytes)
}
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern crate kvdb;
extern crate kvdb_memorydb;
extern crate kvdb_rocksdb;
extern crate linked_hash_map;
extern crate lru_cache;
extern crate memorydb;
extern crate num_rational;
extern crate primitives;
Expand Down