Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Avoid allocations when computing triehash. #8176

Merged
merged 2 commits into from
Mar 22, 2018
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
19 changes: 10 additions & 9 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion util/hashdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ description = "trait for hash-keyed databases."
license = "GPL-3.0"

[dependencies]
elastic-array = "0.9"
elastic-array = "0.10"
ethereum-types = "0.2"
2 changes: 1 addition & 1 deletion util/kvdb-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]

[dependencies]
elastic-array = "0.9"
elastic-array = "0.10"
ethereum-types = "0.2"
kvdb = { path = "../kvdb" }
log = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion util/kvdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version = "0.1.0"
authors = ["Parity Technologies <[email protected]>"]

[dependencies]
elastic-array = "0.9"
elastic-array = "0.10"
error-chain = { version = "0.11", default-features = false }
ethcore-bytes = { path = "../bytes" }
2 changes: 1 addition & 1 deletion util/memorydb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "GPL-3.0"

[dependencies]
bigint = "4.0"
elastic-array = "0.9"
elastic-array = "0.10"
heapsize = "0.4"
ethereum-types = "0.2"
keccak-hash = { version = "0.1.0", path = "../hash" }
Expand Down
2 changes: 1 addition & 1 deletion util/patricia_trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Merkle-Patricia Trie (Ethereum Style)"
license = "GPL-3.0"

[dependencies]
elastic-array = "0.9"
elastic-array = "0.10"
log = "0.3"
rand = "0.4"
ethcore-bytes = { version = "0.1.0", path = "../bytes" }
Expand Down
2 changes: 1 addition & 1 deletion util/rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "0.2.1"
authors = ["Parity Technologies <[email protected]>"]

[dependencies]
elastic-array = "0.9"
elastic-array = "0.10"
ethereum-types = "0.2"
rustc-hex = "1.0"
byteorder = "1.0"
2 changes: 1 addition & 1 deletion util/rlp_compress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["Parity Technologies <[email protected]>"]

[dependencies]
rlp = { path = "../rlp" }
elastic-array = "0.9"
elastic-array = "0.10"
lazy_static = "1.0"
1 change: 1 addition & 0 deletions util/triehash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "in memory patricia trie operations"
license = "GPL-3.0"

[dependencies]
elastic-array = "0.10"
rlp = { version = "0.2.1", path = "../rlp" }
ethereum-types = "0.2"
keccak-hash = { version = "0.1", path = "../hash" }
Expand Down
16 changes: 8 additions & 8 deletions util/triehash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
//!
//! This module should be used to generate trie root hash.

extern crate elastic_array;
extern crate ethereum_types;
extern crate keccak_hash as hash;
extern crate rlp;

use std::collections::BTreeMap;
use std::cmp;
use elastic_array::{ElasticArray4, ElasticArray8};
use ethereum_types::H256;
use hash::keccak;
use rlp::RlpStream;
Expand Down Expand Up @@ -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,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 oddness_factor = inlen % 2;
// next even number divided by two
let reslen = (inlen + 2) >> 1;
let mut res = Vec::with_capacity(reslen);
let mut res = ElasticArray4::new();

let first_byte = {
let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4;
Expand All @@ -186,8 +186,8 @@ fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec<u8> {
}

/// Converts slice of bytes to nibbles.
fn as_nibbles(bytes: &[u8]) -> Vec<u8> {
let mut res = Vec::with_capacity(bytes.len() * 2);
fn as_nibbles(bytes: &[u8]) -> ElasticArray8<u8> {
let mut res = ElasticArray8::new();
for i in 0..bytes.len() {
let byte = bytes[i];
res.push(byte >> 4);
Expand All @@ -213,7 +213,7 @@ fn hash256rlp<A: AsRef<[u8]>, B: AsRef<[u8]>>(input: &[(A, B)], pre_len: usize,
// and then append value
if inlen == 1 {
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);
return;
}
Expand All @@ -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
if shared_prefix > pre_len {
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);
return;
}
Expand Down