More performance optimizations (#1814)

* Buffered DB

* Use identity hash for MemoryDB

* Various tweaks

* Delayed DB compression

* Reduce last_hashes cloning

* Keep state cache

* Updating tests

* Optimized to_big_int

* Fixing build with stable

* Safer code
This commit is contained in:
Arkadiy Paronyan
2016-08-03 22:03:40 +02:00
committed by Gav Wood
parent deceb5fd56
commit 7093651d70
20 changed files with 322 additions and 90 deletions

View File

@@ -246,13 +246,13 @@ impl Client {
}
}
fn build_last_hashes(&self, parent_hash: H256) -> LastHashes {
fn build_last_hashes(&self, parent_hash: H256) -> Arc<LastHashes> {
{
let hashes = self.last_hashes.read();
if hashes.front().map_or(false, |h| h == &parent_hash) {
let mut res = Vec::from(hashes.clone());
res.resize(256, H256::default());
return res;
return Arc::new(res);
}
}
let mut last_hashes = LastHashes::new();
@@ -268,7 +268,7 @@ impl Client {
}
let mut cached_hashes = self.last_hashes.write();
*cached_hashes = VecDeque::from(last_hashes.clone());
last_hashes
Arc::new(last_hashes)
}
fn check_and_close_block(&self, block: &PreverifiedBlock) -> Result<LockedBlock, ()> {
@@ -413,6 +413,7 @@ impl Client {
}
}
self.db.flush().expect("DB flush failed.");
imported
}
@@ -440,7 +441,7 @@ impl Client {
// CHECK! I *think* this is fine, even if the state_root is equal to another
// already-imported block of the same number.
// TODO: Prove it with a test.
block.drain().commit(&batch, number, hash, ancient).expect("State DB commit failed.");
block.drain().commit(&batch, number, hash, ancient).expect("DB commit failed.");
let route = self.chain.insert_block(&batch, block_data, receipts);
self.tracedb.import(&batch, TraceImportRequest {
@@ -451,7 +452,7 @@ impl Client {
retracted: route.retracted.len()
});
// Final commit to the DB
self.db.write(batch).expect("State DB write failed.");
self.db.write_buffered(batch).expect("DB write failed.");
self.chain.commit();
self.update_last_hashes(&parent, hash);
@@ -975,7 +976,7 @@ impl BlockChainClient for Client {
}
fn last_hashes(&self) -> LastHashes {
self.build_last_hashes(self.chain.best_block_hash())
(*self.build_last_hashes(self.chain.best_block_hash())).clone()
}
fn queue_transactions(&self, transactions: Vec<Bytes>) {
@@ -1059,6 +1060,7 @@ impl MiningBlockChainClient for Client {
precise_time_ns() - start,
);
});
self.db.flush().expect("DB flush failed.");
Ok(h)
}
}

View File

@@ -272,7 +272,7 @@ impl MiningBlockChainClient for TestBlockChainClient {
false,
db,
&genesis_header,
last_hashes,
Arc::new(last_hashes),
author,
gas_range_target,
extra_data