From 16eb81e1f7dd5c3285daf1d64016786c58730339 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Mon, 5 Aug 2019 14:35:51 -0400 Subject: [PATCH] Replaced slow vec initialization, and dual calls to hashmap --- src/consensus/encode.rs | 4 ++-- src/network/message_blockdata.rs | 2 +- src/util/psbt/macros.rs | 12 ++++++------ src/util/psbt/map/global.rs | 23 ++++++++--------------- src/util/psbt/map/input.rs | 9 +++------ src/util/psbt/map/output.rs | 10 ++++------ 6 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 9bd8fb45..4f81c42f 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -543,8 +543,8 @@ impl Decodable for [u16; 8] { #[inline] fn consensus_decode(mut d: D) -> Result { let mut res = [0; 8]; - for i in 0..8 { - res[i] = Decodable::consensus_decode(&mut d)?; + for item in &mut res { + *item = Decodable::consensus_decode(&mut d)?; } Ok(res) } diff --git a/src/network/message_blockdata.rs b/src/network/message_blockdata.rs index 3326fb52..e2795ef0 100644 --- a/src/network/message_blockdata.rs +++ b/src/network/message_blockdata.rs @@ -111,7 +111,7 @@ impl GetBlocksMessage { pub fn new(locator_hashes: Vec, stop_hash: BlockHash) -> GetBlocksMessage { GetBlocksMessage { version: constants::PROTOCOL_VERSION, - locator_hashes: locator_hashes.clone(), + locator_hashes: locator_hashes, stop_hash: stop_hash } } diff --git a/src/util/psbt/macros.rs b/src/util/psbt/macros.rs index 7844b7ca..80f6678d 100644 --- a/src/util/psbt/macros.rs +++ b/src/util/psbt/macros.rs @@ -119,12 +119,12 @@ macro_rules! impl_psbt_insert_pair { if !$raw_key.key.is_empty() { let key_val: $keyed_key_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_key.key)?; - if $slf.$keyed_name.contains_key(&key_val) { - return Err(::util::psbt::Error::DuplicateKey($raw_key).into()); - } else { - let val: $keyed_value_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?; - - $slf.$keyed_name.insert(key_val, val); + match $slf.$keyed_name.entry(key_val) { + ::std::collections::btree_map::Entry::Vacant(empty_key) => { + let val: $keyed_value_type = ::util::psbt::serialize::Deserialize::deserialize(&$raw_value)?; + empty_key.insert(val); + } + ::std::collections::btree_map::Entry::Occupied(_) => return Err(::util::psbt::Error::DuplicateKey($raw_key).into()), } } else { return Err(::util::psbt::Error::InvalidKey($raw_key).into()); diff --git a/src/util/psbt/map/global.rs b/src/util/psbt/map/global.rs index b34b4114..f6b411d4 100644 --- a/src/util/psbt/map/global.rs +++ b/src/util/psbt/map/global.rs @@ -13,6 +13,7 @@ // use std::collections::BTreeMap; +use std::collections::btree_map::Entry; use std::io::{self, Cursor}; use blockdata::transaction::Transaction; @@ -60,15 +61,10 @@ impl Map for Global { } = pair; match raw_key.type_value { - 0u8 => { - return Err(Error::DuplicateKey(raw_key).into()); - } - _ => { - if self.unknown.contains_key(&raw_key) { - return Err(Error::DuplicateKey(raw_key).into()); - } else { - self.unknown.insert(raw_key, raw_value); - } + 0u8 => 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()), } } @@ -158,12 +154,9 @@ impl Decodable for Global { return Err(Error::InvalidKey(pair.key).into()) } } - _ => { - if unknowns.contains_key(&pair.key) { - return Err(Error::DuplicateKey(pair.key).into()); - } else { - unknowns.insert(pair.key, pair.value); - } + _ => match unknowns.entry(pair.key) { + Entry::Vacant(empty_key) => {empty_key.insert(pair.value);}, + Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()), } } } diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index 32f19bc5..05a24432 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -112,12 +112,9 @@ impl Map for Input { self.hd_keypaths <= | } } - _ => { - if self.unknown.contains_key(&raw_key) { - return Err(Error::DuplicateKey(raw_key).into()); - } else { - self.unknown.insert(raw_key, raw_value); - } + _ => match self.unknown.entry(raw_key) { + ::std::collections::btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);}, + ::std::collections::btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()), } } diff --git a/src/util/psbt/map/output.rs b/src/util/psbt/map/output.rs index 8219e812..7e32a421 100644 --- a/src/util/psbt/map/output.rs +++ b/src/util/psbt/map/output.rs @@ -13,6 +13,7 @@ // use std::collections::BTreeMap; +use std::collections::btree_map::Entry; use blockdata::script::Script; use consensus::encode; @@ -61,12 +62,9 @@ impl Map for Output { self.hd_keypaths <= | } } - _ => { - if self.unknown.contains_key(&raw_key) { - return Err(Error::DuplicateKey(raw_key).into()); - } else { - self.unknown.insert(raw_key, raw_value); - } + _ => 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()), } }