updated tests for rlp and indention in benches
This commit is contained in:
parent
111fc70d0b
commit
07f3e6d5a5
105
benches/rlp.rs
105
benches/rlp.rs
@ -16,80 +16,81 @@ use ethcore_util::uint::U256;
|
|||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_stream_u64_value(b: &mut Bencher) {
|
fn bench_stream_u64_value(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
//1029
|
// u64
|
||||||
let mut stream = RlpStream::new();
|
let mut stream = RlpStream::new();
|
||||||
stream.append(&0x1023456789abcdefu64);
|
stream.append(&0x1023456789abcdefu64);
|
||||||
let _ = stream.out().unwrap();
|
let _ = stream.out().unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_decode_u64_value(b: &mut Bencher) {
|
fn bench_decode_u64_value(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
// 1029
|
// u64
|
||||||
let data = vec![0x88, 0x10, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
|
let data = vec![0x88, 0x10, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
|
||||||
let rlp = Rlp::new(&data);
|
let rlp = Rlp::new(&data);
|
||||||
let _ = u64::decode(&rlp).unwrap();
|
let _ = u64::decode(&rlp).unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_stream_u256_value(b: &mut Bencher) {
|
fn bench_stream_u256_value(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
//u256
|
// u256
|
||||||
let mut stream = RlpStream::new();
|
let mut stream = RlpStream::new();
|
||||||
stream.append(&U256::from_str("8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0").unwrap());
|
stream.append(&U256::from_str("8090a0b0c0d0e0f009102030405060770000000000000001000000000\
|
||||||
let _ = stream.out().unwrap();
|
00012f0")
|
||||||
});
|
.unwrap());
|
||||||
|
let _ = stream.out().unwrap();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_decode_u256_value(b: &mut Bencher) {
|
fn bench_decode_u256_value(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
// u256
|
// u256
|
||||||
let data = vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
|
let data = vec![0xa0, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0x09, 0x10, 0x20,
|
||||||
0x09, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x77,
|
0x30, 0x40, 0x50, 0x60, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
|
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xf0];
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xf0];
|
let rlp = Rlp::new(&data);
|
||||||
let rlp = Rlp::new(&data);
|
let _ = U256::decode(&rlp).unwrap();
|
||||||
let _ = U256::decode(&rlp).unwrap();
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_stream_nested_empty_lists(b: &mut Bencher) {
|
fn bench_stream_nested_empty_lists(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
// [ [], [[]], [ [], [[]] ] ]
|
// [ [], [[]], [ [], [[]] ] ]
|
||||||
let mut stream = RlpStream::new_list(3);
|
let mut stream = RlpStream::new_list(3);
|
||||||
stream.append_list(0);
|
stream.append_list(0);
|
||||||
stream.append_list(1).append_list(0);
|
stream.append_list(1).append_list(0);
|
||||||
stream.append_list(2).append_list(0).append_list(1).append_list(0);
|
stream.append_list(2).append_list(0).append_list(1).append_list(0);
|
||||||
let _ = stream.out().unwrap();
|
let _ = stream.out().unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_decode_nested_empty_lists(b: &mut Bencher) {
|
fn bench_decode_nested_empty_lists(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
// [ [], [[]], [ [], [[]] ] ]
|
// [ [], [[]], [ [], [[]] ] ]
|
||||||
let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0];
|
let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0];
|
||||||
let rlp = Rlp::new(&data);
|
let rlp = Rlp::new(&data);
|
||||||
let _v0: Vec<u8> = Decodable::decode(&rlp.at(0).unwrap()).unwrap();
|
let _v0: Vec<u8> = Decodable::decode(&rlp.at(0).unwrap()).unwrap();
|
||||||
let _v1: Vec<Vec<u8>> = Decodable::decode(&rlp.at(1).unwrap()).unwrap();
|
let _v1: Vec<Vec<u8>> = Decodable::decode(&rlp.at(1).unwrap()).unwrap();
|
||||||
let nested_rlp = rlp.at(2).unwrap();
|
let nested_rlp = rlp.at(2).unwrap();
|
||||||
let _v2a: Vec<u8> = Decodable::decode(&nested_rlp.at(0).unwrap()).unwrap();
|
let _v2a: Vec<u8> = Decodable::decode(&nested_rlp.at(0).unwrap()).unwrap();
|
||||||
let _v2b: Vec<Vec<u8>> = Decodable::decode(&nested_rlp.at(1).unwrap()).unwrap();
|
let _v2b: Vec<Vec<u8>> = Decodable::decode(&nested_rlp.at(1).unwrap()).unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
fn bench_stream_1000_empty_lists(b: &mut Bencher) {
|
fn bench_stream_1000_empty_lists(b: &mut Bencher) {
|
||||||
b.iter( || {
|
b.iter(|| {
|
||||||
let mut stream = RlpStream::new_list(1000);
|
let mut stream = RlpStream::new_list(1000);
|
||||||
for _ in 0..1000 {
|
for _ in 0..1000 {
|
||||||
stream.append_list(0);
|
stream.append_list(0);
|
||||||
}
|
}
|
||||||
let _ = stream.out().unwrap();
|
let _ = stream.out().unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
79
src/rlp.rs
79
src/rlp.rs
@ -7,38 +7,75 @@
|
|||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! extern crate ethcore_util;
|
//! extern crate ethcore_util;
|
||||||
//! use ethcore_util::rlp::{RlpStream};
|
//! use ethcore_util::rlp::{Rlp, RlpStream, Decodable};
|
||||||
//!
|
//!
|
||||||
//! fn encode_value() {
|
//! fn encode_value() {
|
||||||
//! // 1029
|
//! // 1029
|
||||||
//! let mut stream = RlpStream::new();
|
//! let mut stream = RlpStream::new();
|
||||||
//! stream.append(&1029u32);
|
//! stream.append(&1029u32);
|
||||||
//! let out = stream.out().unwrap();
|
//! let out = stream.out().unwrap();
|
||||||
//! assert_eq!(out, vec![0x82, 0x04, 0x05]);
|
//! assert_eq!(out, vec![0x82, 0x04, 0x05]);
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn encode_list() {
|
//! fn encode_list() {
|
||||||
//! // [ "cat", "dog" ]
|
//! // [ "cat", "dog" ]
|
||||||
//! let mut stream = RlpStream::new_list(2);
|
//! let mut stream = RlpStream::new_list(2);
|
||||||
//! stream.append(&"cat").append(&"dog");
|
//! stream.append(&"cat").append(&"dog");
|
||||||
//! let out = stream.out().unwrap();
|
//! let out = stream.out().unwrap();
|
||||||
//! assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
//! assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn encode_list2() {
|
//! fn encode_list2() {
|
||||||
//! // [ [], [[]], [ [], [[]] ] ]
|
//! // [ [], [[]], [ [], [[]] ] ]
|
||||||
//! let mut stream = RlpStream::new_list(3);
|
//! let mut stream = RlpStream::new_list(3);
|
||||||
//! stream.append_list(0);
|
//! stream.append_list(0);
|
||||||
//! stream.append_list(1).append_list(0);
|
//! stream.append_list(1).append_list(0);
|
||||||
//! stream.append_list(2).append_list(0).append_list(1).append_list(0);
|
//! stream.append_list(2).append_list(0).append_list(1).append_list(0);
|
||||||
//! let out = stream.out().unwrap();
|
//! let out = stream.out().unwrap();
|
||||||
//! assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]);
|
//! assert_eq!(out, vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]);
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn decode_value() {
|
||||||
|
//! // 0x102456789abcdef
|
||||||
|
//! let data = vec![0x88, 0x10, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
|
||||||
|
//! let rlp = Rlp::new(&data);
|
||||||
|
//! let _ = u64::decode(&rlp).unwrap();
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn decode_string() {
|
||||||
|
//! // "cat"
|
||||||
|
//! let data = vec![0x83, b'c', b'a', b't'];
|
||||||
|
//! let rlp = Rlp::new(&data);
|
||||||
|
//! let _ = String::decode(&rlp).unwrap();
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn decode_list() {
|
||||||
|
//! // ["cat", "dog"]
|
||||||
|
//! let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g'];
|
||||||
|
//! let rlp = Rlp::new(&data);
|
||||||
|
//! let _ : Vec<String> = Decodable::decode(&rlp).unwrap();
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn decode_list2() {
|
||||||
|
//! // [ [], [[]], [ [], [[]] ] ]
|
||||||
|
//! let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0];
|
||||||
|
//! let rlp = Rlp::new(&data);
|
||||||
|
//! let _v0: Vec<u8> = Decodable::decode(&rlp.at(0).unwrap()).unwrap();
|
||||||
|
//! let _v1: Vec<Vec<u8>> = Decodable::decode(&rlp.at(1).unwrap()).unwrap();
|
||||||
|
//! let nested_rlp = rlp.at(2).unwrap();
|
||||||
|
//! let _v2a: Vec<u8> = Decodable::decode(&nested_rlp.at(0).unwrap()).unwrap();
|
||||||
|
//! let _v2b: Vec<Vec<u8>> = Decodable::decode(&nested_rlp.at(1).unwrap()).unwrap();
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! encode_value();
|
//! encode_value();
|
||||||
//! encode_list();
|
//! encode_list();
|
||||||
//! encode_list2();
|
//! encode_list2();
|
||||||
|
//!
|
||||||
|
//! decode_value();
|
||||||
|
//! decode_string();
|
||||||
|
//! decode_list();
|
||||||
|
//! decode_list2();
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
Loading…
Reference in New Issue
Block a user