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

Add benchmarks and tests for RlpNodeCodec decoding #11287

Merged
merged 1 commit into from
Dec 3, 2019
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
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions util/patricia-trie-ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ elastic-array = "0.10"
memory-db = "0.15.0"
keccak-hash = "0.4.0"
journaldb = { path = "../journaldb" }
criterion = "0.3"

[[bench]]
name = "rlp_node_codec"
harness = false
83 changes: 83 additions & 0 deletions util/patricia-trie-ethereum/benches/rlp_node_codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.

// Parity Ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

//! Benchmarking RlpNodeCodec decoding performance

extern crate criterion;
extern crate patricia_trie_ethereum as ethtrie;
extern crate trie_db;
extern crate ethereum_types;
extern crate rlp;

use criterion::{Criterion, criterion_group, criterion_main};
use ethereum_types::H256;
use ethtrie::RlpNodeCodec;
use rlp::RlpStream;
use trie_db::NodeCodec;

fn decoding(c: &mut Criterion) {
c.bench_function("decode leaf (inline)", |b| {
let mut stream = RlpStream::new_list(2);
stream.append(&"cat").append(&"dog");
let data = stream.out();
b.iter(|| { RlpNodeCodec::decode(&data) });
});

c.bench_function("decode extension (inline)", |b| {
let mut stream = RlpStream::new_list(2);
let payload = vec![0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9u8];
stream.append(&"").append(&payload);
let data = stream.out();
b.iter(|| { RlpNodeCodec::decode(&data) });
});

c.bench_function("decode extension (hash)", |b| {
let mut stream = RlpStream::new_list(2);
let payload = H256::random();
stream.append(&"").append(&payload);
let data = stream.out();
b.iter(|| { RlpNodeCodec::decode(&data) });
});

c.bench_function("decode branch (hash)", |b| {
let mut stream = RlpStream::new_list(17);
for _ in 0..17 {
stream.append(&H256::random());

}
let data = stream.out();
b.iter(|| { RlpNodeCodec::decode(&data) });
});

c.bench_function("decode branch (inline)", |b| {
let mut stream = RlpStream::new_list(17);
for _ in 0..17 {
stream.append(&[&H256::random().as_bytes(), H256::random().as_bytes()].concat());
}
let data = stream.out();
b.iter(|| { RlpNodeCodec::decode(&data) });
});

c.bench_function("decode empty data", |b| {
let mut stream = RlpStream::new();
stream.append_empty_data();
let data = stream.out();
b.iter(|| { RlpNodeCodec::decode(&data)});
});
}

criterion_group!(benches, decoding);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions util/patricia-trie-ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ pub type Result<T> = trie::Result<T, H256, DecoderError>;

#[cfg(test)]
mod tests {

use ethereum_types::H256;
use crate::{TrieDB, TrieDBMut, trie::TrieMut};
use trie::Trie;

use crate::{TrieDB, TrieDBMut, trie::TrieMut};

#[test]
fn test_inline_encoding_branch() {
let mut memdb = journaldb::new_memory_db();
Expand Down
55 changes: 51 additions & 4 deletions util/patricia-trie-ethereum/src/rlp_node_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ use std::marker::PhantomData;
use std::borrow::Borrow;
use std::ops::Range;
use trie::{
NodeCodec, ChildReference, Partial, node::{NibbleSlicePlan, NodePlan, NodeHandlePlan},
NodeCodec, ChildReference, Partial,
node::{NibbleSlicePlan, NodePlan, NodeHandlePlan},
};



/// Concrete implementation of a `NodeCodec` with Rlp encoding, generic over the `Hasher`
#[derive(Default, Clone)]
pub struct RlpNodeCodec<H: Hasher> {mark: PhantomData<H>}
Expand Down Expand Up @@ -98,7 +97,7 @@ impl NodeCodec for RlpNodeCodec<KeccakHasher> {
HASHED_NULL_NODE
}

fn decode_plan(data: &[u8]) -> ::std::result::Result<NodePlan, Self::Error> {
fn decode_plan(data: &[u8]) -> Result<NodePlan, Self::Error> {
let r = Rlp::new(data);
match r.prototype()? {
// either leaf or extension - decode first item with NibbleSlice::???
Expand Down Expand Up @@ -230,3 +229,51 @@ impl NodeCodec for RlpNodeCodec<KeccakHasher> {
}

}

#[cfg(test)]
mod tests {
use trie::{NodeCodec, node::{Node, NodeHandle}, NibbleSlice};
use rlp::RlpStream;
use RlpNodeCodec;

#[test]
fn decode_leaf() {
let mut stream = RlpStream::new_list(2);
stream.append(&"cat").append(&"dog");
let data = stream.out();
let r = RlpNodeCodec::decode(&data);
assert!(r.is_ok());
// "c" & 16 != 16 => `start` == 1
let cat_nib = NibbleSlice::new(&b"at"[..]);
assert_eq!(r.unwrap(), Node::Leaf(cat_nib, &b"dog"[..]));
}

#[test]
fn decode_ext() {
let mut stream = RlpStream::new_list(2);
let payload = vec![0x1, 0x2, 0x3u8];
stream.append(&"").append(&payload);
let data = stream.out();
let decoded = RlpNodeCodec::decode(&data);

assert!(decoded.is_ok());
assert_eq!(
decoded.unwrap(),
Node::Extension(
NibbleSlice::new(&[]),
NodeHandle::Inline(&[0x80 + 0x3, 0x1, 0x2, 0x3])
)
);
}

#[test]
fn decode_empty_data() {
let mut stream = RlpStream::new();
stream.append_empty_data();
let data = stream.out();
assert_eq!(
RlpNodeCodec::decode(&data),
Ok(Node::Empty),
);
}
}