Avoid allocations when computing triehash. (#8176)

* Avoid allocations when computing triehash.

* Bump elastic-array to 0.10
This commit is contained in:
Tomasz Drwięga 2018-03-22 09:24:46 +01:00 committed by Rando
parent d293f94a6f
commit 7c8d404cf8
10 changed files with 26 additions and 24 deletions

19
Cargo.lock generated
View File

@ -344,7 +344,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "elastic-array" name = "elastic-array"
version = "0.9.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1075,7 +1075,7 @@ dependencies = [
name = "hashdb" name = "hashdb"
version = "0.1.1" version = "0.1.1"
dependencies = [ dependencies = [
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -1398,7 +1398,7 @@ dependencies = [
name = "kvdb" name = "kvdb"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethcore-bytes 0.1.0", "ethcore-bytes 0.1.0",
] ]
@ -1415,7 +1415,7 @@ dependencies = [
name = "kvdb-rocksdb" name = "kvdb-rocksdb"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"interleaved-ordered 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "interleaved-ordered 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"kvdb 0.1.0", "kvdb 0.1.0",
@ -1576,7 +1576,7 @@ name = "memorydb"
version = "0.1.1" version = "0.1.1"
dependencies = [ dependencies = [
"bigint 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "bigint 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hashdb 0.1.1", "hashdb 0.1.1",
"heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "heapsize 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2353,7 +2353,7 @@ version = "0.1.0"
name = "patricia-trie" name = "patricia-trie"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethcore-bytes 0.1.0", "ethcore-bytes 0.1.0",
"ethcore-logger 1.11.0", "ethcore-logger 1.11.0",
"ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2658,7 +2658,7 @@ name = "rlp"
version = "0.2.1" version = "0.2.1"
dependencies = [ dependencies = [
"byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@ -2667,7 +2667,7 @@ dependencies = [
name = "rlp_compress" name = "rlp_compress"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rlp 0.2.1", "rlp 0.2.1",
] ]
@ -3270,6 +3270,7 @@ dependencies = [
name = "triehash" name = "triehash"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"keccak-hash 0.1.0", "keccak-hash 0.1.0",
"rlp 0.2.1", "rlp 0.2.1",
@ -3628,7 +3629,7 @@ dependencies = [
"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
"checksum edit-distance 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6a34f5204fbc13582de418611cf3a7dcdd07c6d312a5b631597ba72c06b9d9c9" "checksum edit-distance 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6a34f5204fbc13582de418611cf3a7dcdd07c6d312a5b631597ba72c06b9d9c9"
"checksum either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "740178ddf48b1a9e878e6d6509a1442a2d42fd2928aae8e7a6f8a36fb01981b3" "checksum either 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "740178ddf48b1a9e878e6d6509a1442a2d42fd2928aae8e7a6f8a36fb01981b3"
"checksum elastic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "258ff6a9a94f648d0379dbd79110e057edbb53eb85cc237e33eadf8e5a30df85" "checksum elastic-array 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "88d4851b005ef16de812ea9acdb7bece2f0a40dd86c07b85631d7dafa54537bb"
"checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" "checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b"
"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3" "checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
"checksum eth-secp256k1 0.5.7 (git+https://github.com/paritytech/rust-secp256k1)" = "<none>" "checksum eth-secp256k1 0.5.7 (git+https://github.com/paritytech/rust-secp256k1)" = "<none>"

View File

@ -6,5 +6,5 @@ description = "trait for hash-keyed databases."
license = "GPL-3.0" license = "GPL-3.0"
[dependencies] [dependencies]
elastic-array = "0.9" elastic-array = "0.10"
ethereum-types = "0.2" ethereum-types = "0.2"

View File

@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
elastic-array = "0.9" elastic-array = "0.10"
ethereum-types = "0.2" ethereum-types = "0.2"
kvdb = { path = "../kvdb" } kvdb = { path = "../kvdb" }
log = "0.3" log = "0.3"

View File

@ -4,6 +4,6 @@ version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
elastic-array = "0.9" elastic-array = "0.10"
error-chain = { version = "0.11", default-features = false } error-chain = { version = "0.11", default-features = false }
ethcore-bytes = { path = "../bytes" } ethcore-bytes = { path = "../bytes" }

View File

@ -7,7 +7,7 @@ license = "GPL-3.0"
[dependencies] [dependencies]
bigint = "4.0" bigint = "4.0"
elastic-array = "0.9" elastic-array = "0.10"
heapsize = "0.4" heapsize = "0.4"
ethereum-types = "0.2" ethereum-types = "0.2"
keccak-hash = { version = "0.1.0", path = "../hash" } keccak-hash = { version = "0.1.0", path = "../hash" }

View File

@ -6,7 +6,7 @@ description = "Merkle-Patricia Trie (Ethereum Style)"
license = "GPL-3.0" license = "GPL-3.0"
[dependencies] [dependencies]
elastic-array = "0.9" elastic-array = "0.10"
log = "0.3" log = "0.3"
rand = "0.4" rand = "0.4"
ethcore-bytes = { version = "0.1.0", path = "../bytes" } ethcore-bytes = { version = "0.1.0", path = "../bytes" }

View File

@ -7,7 +7,7 @@ version = "0.2.1"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
elastic-array = "0.9" elastic-array = "0.10"
ethereum-types = "0.2" ethereum-types = "0.2"
rustc-hex = "1.0" rustc-hex = "1.0"
byteorder = "1.0" byteorder = "1.0"

View File

@ -5,5 +5,5 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies] [dependencies]
rlp = { path = "../rlp" } rlp = { path = "../rlp" }
elastic-array = "0.9" elastic-array = "0.10"
lazy_static = "1.0" lazy_static = "1.0"

View File

@ -6,6 +6,7 @@ description = "in memory patricia trie operations"
license = "GPL-3.0" license = "GPL-3.0"
[dependencies] [dependencies]
elastic-array = "0.10"
rlp = { version = "0.2.1", path = "../rlp" } rlp = { version = "0.2.1", path = "../rlp" }
ethereum-types = "0.2" ethereum-types = "0.2"
keccak-hash = { version = "0.1", path = "../hash" } keccak-hash = { version = "0.1", path = "../hash" }

View File

@ -18,12 +18,14 @@
//! //!
//! This module should be used to generate trie root hash. //! This module should be used to generate trie root hash.
extern crate elastic_array;
extern crate ethereum_types; extern crate ethereum_types;
extern crate keccak_hash as hash; extern crate keccak_hash as hash;
extern crate rlp; extern crate rlp;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::cmp; use std::cmp;
use elastic_array::{ElasticArray4, ElasticArray8};
use ethereum_types::H256; use ethereum_types::H256;
use hash::keccak; use hash::keccak;
use rlp::RlpStream; use rlp::RlpStream;
@ -158,12 +160,10 @@ fn gen_trie_root<A: AsRef<[u8]>, B: AsRef<[u8]>>(input: &[(A, B)]) -> H256 {
/// [1,2,3,4,5,T] 0x312345 // 5 > 3 /// [1,2,3,4,5,T] 0x312345 // 5 > 3
/// [1,2,3,4,T] 0x201234 // 4 > 3 /// [1,2,3,4,T] 0x201234 // 4 > 3
/// ``` /// ```
fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec<u8> { fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> ElasticArray4<u8> {
let inlen = nibbles.len(); let inlen = nibbles.len();
let oddness_factor = inlen % 2; let oddness_factor = inlen % 2;
// next even number divided by two let mut res = ElasticArray4::new();
let reslen = (inlen + 2) >> 1;
let mut res = Vec::with_capacity(reslen);
let first_byte = { let first_byte = {
let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4; let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4;
@ -186,8 +186,8 @@ fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec<u8> {
} }
/// Converts slice of bytes to nibbles. /// Converts slice of bytes to nibbles.
fn as_nibbles(bytes: &[u8]) -> Vec<u8> { fn as_nibbles(bytes: &[u8]) -> ElasticArray8<u8> {
let mut res = Vec::with_capacity(bytes.len() * 2); let mut res = ElasticArray8::new();
for i in 0..bytes.len() { for i in 0..bytes.len() {
let byte = bytes[i]; let byte = bytes[i];
res.push(byte >> 4); res.push(byte >> 4);
@ -213,7 +213,7 @@ fn hash256rlp<A: AsRef<[u8]>, B: AsRef<[u8]>>(input: &[(A, B)], pre_len: usize,
// and then append value // and then append value
if inlen == 1 { if inlen == 1 {
stream.begin_list(2); stream.begin_list(2);
stream.append(&hex_prefix_encode(&key[pre_len..], true)); stream.append(&&*hex_prefix_encode(&key[pre_len..], true));
stream.append(&value); stream.append(&value);
return; return;
} }
@ -232,7 +232,7 @@ fn hash256rlp<A: AsRef<[u8]>, B: AsRef<[u8]>>(input: &[(A, B)], pre_len: usize,
// then recursively append suffixes of all items who had this key // then recursively append suffixes of all items who had this key
if shared_prefix > pre_len { if shared_prefix > pre_len {
stream.begin_list(2); stream.begin_list(2);
stream.append(&hex_prefix_encode(&key[pre_len..shared_prefix], false)); stream.append(&&*hex_prefix_encode(&key[pre_len..shared_prefix], false));
hash256aux(input, shared_prefix, stream); hash256aux(input, shared_prefix, stream);
return; return;
} }