Replaced slow vec initialization, and dual calls to hashmap

This commit is contained in:
Elichai Turkel 2019-08-05 14:35:51 -04:00
parent a148e06736
commit 16eb81e1f7
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
6 changed files with 24 additions and 36 deletions

View File

@ -543,8 +543,8 @@ impl Decodable for [u16; 8] {
#[inline] #[inline]
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> { fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, Error> {
let mut res = [0; 8]; let mut res = [0; 8];
for i in 0..8 { for item in &mut res {
res[i] = Decodable::consensus_decode(&mut d)?; *item = Decodable::consensus_decode(&mut d)?;
} }
Ok(res) Ok(res)
} }

View File

@ -111,7 +111,7 @@ impl GetBlocksMessage {
pub fn new(locator_hashes: Vec<BlockHash>, stop_hash: BlockHash) -> GetBlocksMessage { pub fn new(locator_hashes: Vec<BlockHash>, stop_hash: BlockHash) -> GetBlocksMessage {
GetBlocksMessage { GetBlocksMessage {
version: constants::PROTOCOL_VERSION, version: constants::PROTOCOL_VERSION,
locator_hashes: locator_hashes.clone(), locator_hashes: locator_hashes,
stop_hash: stop_hash stop_hash: stop_hash
} }
} }

View File

@ -119,12 +119,12 @@ macro_rules! impl_psbt_insert_pair {
if !$raw_key.key.is_empty() { if !$raw_key.key.is_empty() {
let key_val: $keyed_key_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?; let key_val: $keyed_key_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?;
if $slf.$keyed_name.contains_key(&key_val) { match $slf.$keyed_name.entry(key_val) {
return Err(::util::psbt::Error::DuplicateKey($raw_key).into()); ::std::collections::btree_map::Entry::Vacant(empty_key) => {
} else {
let val: $keyed_value_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?; let val: $keyed_value_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?;
empty_key.insert(val);
$slf.$keyed_name.insert(key_val, val); }
::std::collections::btree_map::Entry::Occupied(_) => return Err(::util::psbt::Error::DuplicateKey($raw_key).into()),
} }
} else { } else {
return Err(::util::psbt::Error::InvalidKey($raw_key).into()); return Err(::util::psbt::Error::InvalidKey($raw_key).into());

View File

@ -13,6 +13,7 @@
// //
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::io::{self, Cursor}; use std::io::{self, Cursor};
use blockdata::transaction::Transaction; use blockdata::transaction::Transaction;
@ -60,15 +61,10 @@ impl Map for Global {
} = pair; } = pair;
match raw_key.type_value { match raw_key.type_value {
0u8 => { 0u8 => return Err(Error::DuplicateKey(raw_key).into()),
return Err(Error::DuplicateKey(raw_key).into()); _ => match self.unknown.entry(raw_key) {
} Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
_ => { Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
if self.unknown.contains_key(&raw_key) {
return Err(Error::DuplicateKey(raw_key).into());
} else {
self.unknown.insert(raw_key, raw_value);
}
} }
} }
@ -158,12 +154,9 @@ impl Decodable for Global {
return Err(Error::InvalidKey(pair.key).into()) return Err(Error::InvalidKey(pair.key).into())
} }
} }
_ => { _ => match unknowns.entry(pair.key) {
if unknowns.contains_key(&pair.key) { Entry::Vacant(empty_key) => {empty_key.insert(pair.value);},
return Err(Error::DuplicateKey(pair.key).into()); Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
} else {
unknowns.insert(pair.key, pair.value);
}
} }
} }
} }

View File

@ -112,12 +112,9 @@ impl Map for Input {
self.hd_keypaths <= <raw_key: PublicKey>|<raw_value: (Fingerprint, DerivationPath)> self.hd_keypaths <= <raw_key: PublicKey>|<raw_value: (Fingerprint, DerivationPath)>
} }
} }
_ => { _ => match self.unknown.entry(raw_key) {
if self.unknown.contains_key(&raw_key) { ::std::collections::btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
return Err(Error::DuplicateKey(raw_key).into()); ::std::collections::btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
} else {
self.unknown.insert(raw_key, raw_value);
}
} }
} }

View File

@ -13,6 +13,7 @@
// //
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use blockdata::script::Script; use blockdata::script::Script;
use consensus::encode; use consensus::encode;
@ -61,12 +62,9 @@ impl Map for Output {
self.hd_keypaths <= <raw_key: PublicKey>|<raw_value: (Fingerprint, DerivationPath)> self.hd_keypaths <= <raw_key: PublicKey>|<raw_value: (Fingerprint, DerivationPath)>
} }
} }
_ => { _ => match self.unknown.entry(raw_key) {
if self.unknown.contains_key(&raw_key) { Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
return Err(Error::DuplicateKey(raw_key).into()); Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
} else {
self.unknown.insert(raw_key, raw_value);
}
} }
} }