From 21a5d5418bdb9b3b8cc60149a801d300bf08e2e5 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 26 Nov 2015 15:43:52 +0100 Subject: [PATCH] added benchmarks and few optimisations for rlp encoding --- benches/rlp.rs | 50 ++++++++++++++++++++++++++++++++++++++++++-------- src/bytes.rs | 2 ++ src/uint.rs | 1 - 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/benches/rlp.rs b/benches/rlp.rs index 10397db09..8c1fa8d58 100644 --- a/benches/rlp.rs +++ b/benches/rlp.rs @@ -10,19 +10,53 @@ extern crate test; extern crate ethcore_util; use test::Bencher; -use ethcore_util::rlp; +use std::str::FromStr; use ethcore_util::rlp::{RlpStream, Rlp, Decodable}; +use ethcore_util::uint::U256; #[bench] -fn bench_stream_value(b: &mut Bencher) { +fn bench_stream_u64_value(b: &mut Bencher) { b.iter( || { //1029 let mut stream = RlpStream::new(); - stream.append(&1029u32); + stream.append(&1029u64); let _ = stream.out().unwrap(); }); } +#[bench] +fn bench_decode_u64_value(b: &mut Bencher) { + b.iter( || { + // 1029 + let data = vec![0x82, 0x04, 0x05]; + let rlp = Rlp::new(&data); + let _ = u64::decode(&rlp).unwrap(); + }); +} + +#[bench] +fn bench_stream_u256_value(b: &mut Bencher) { + b.iter( || { + //u256 + let mut stream = RlpStream::new(); + stream.append(&U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").unwrap()); + let _ = stream.out().unwrap(); + }); +} + +#[bench] +fn bench_decode_u256_value(b: &mut Bencher) { + b.iter( || { + // u256 + let data = vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, + 0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xf0]; + let rlp = Rlp::new(&data); + let _ = U256::decode(&rlp).unwrap(); + }); +} + #[bench] fn bench_stream_nested_empty_lists(b: &mut Bencher) { b.iter( || { @@ -41,17 +75,17 @@ fn bench_decode_nested_empty_lists(b: &mut Bencher) { // [ [], [[]], [ [], [[]] ] ] let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]; let rlp = Rlp::new(&data); - let v0: Vec = Decodable::decode(&rlp.at(0).unwrap()).unwrap(); - let v1: Vec> = Decodable::decode(&rlp.at(1).unwrap()).unwrap(); - let v2a: Vec = Decodable::decode(&rlp.at(2).unwrap().at(0).unwrap()).unwrap(); - let v2b: Vec> = Decodable::decode(&rlp.at(2).unwrap().at(1).unwrap()).unwrap(); + let _v0: Vec = Decodable::decode(&rlp.at(0).unwrap()).unwrap(); + let _v1: Vec> = Decodable::decode(&rlp.at(1).unwrap()).unwrap(); + let _v2a: Vec = Decodable::decode(&rlp.at(2).unwrap().at(0).unwrap()).unwrap(); + let _v2b: Vec> = Decodable::decode(&rlp.at(2).unwrap().at(1).unwrap()).unwrap(); }); } #[bench] fn bench_stream_1000_empty_lists(b: &mut Bencher) { b.iter( || { - let mut stream = RlpStream::new(); + let mut stream = RlpStream::new_list(1000); for _ in 0..1000 { stream.append_list(0); } diff --git a/src/bytes.rs b/src/bytes.rs index 52dad3cb9..1b331cf15 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -60,6 +60,7 @@ impl ToBytes for u64 { fn to_bytes(&self) -> Vec { let mut res= vec![]; let count = self.to_bytes_len(); + res.reserve(count); for i in 0..count { let j = count - 1 - i; res.push((*self >> (j * 8)) as u8); @@ -89,6 +90,7 @@ macro_rules! impl_uint_to_bytes { fn to_bytes(&self) -> Vec { let mut res= vec![]; let count = self.to_bytes_len(); + res.reserve(count); for i in 0..count { let j = count - 1 - i; res.push(self.byte(j)); diff --git a/src/uint.rs b/src/uint.rs index daba98b63..bc333631b 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -233,7 +233,6 @@ macro_rules! construct_uint { type Err = FromHexError; fn from_str(value: &str) -> Result<$name, Self::Err> { - println!("{}", value); let bytes: &[u8] = &try!(value.from_hex()); Ok(From::from(bytes)) }