benchmars. optimised inserting empty list

This commit is contained in:
debris 2015-11-26 01:22:33 +01:00
parent 1582088759
commit f5d2d7a2d4
2 changed files with 47 additions and 2 deletions

46
benches/rlp.rs Normal file
View File

@ -0,0 +1,46 @@
//! benchmarking for rlp
//! should be started with:
//! ```bash
//! multirust run nightly cargo bench
//! ```
#![feature(test)]
extern crate test;
extern crate ethcore_util;
use test::Bencher;
use ethcore_util::rlp::{RlpStream};
#[bench]
fn bench_stream_value(b: &mut Bencher) {
b.iter( || {
//1029
let mut stream = RlpStream::new();
stream.append(&1029u32);
let _ = stream.out().unwrap();
});
}
#[bench]
fn bench_stream_nested_empty_lists(b: &mut Bencher) {
b.iter( || {
// [ [], [[]], [ [], [[]] ] ]
let mut stream = RlpStream::new_list(3);
stream.append_list(0);
stream.append_list(1).append_list(0);
stream.append_list(2).append_list(0).append_list(1).append_list(0);
let _ = stream.out().unwrap();
});
}
#[bench]
fn bench_stream_1000_empty_lists(b: &mut Bencher) {
b.iter( || {
let mut stream = RlpStream::new();
for _ in 0..1000 {
stream.append_list(0);
}
let _ = stream.out().unwrap();
});
}

View File

@ -286,14 +286,13 @@ impl RlpStream {
}
/// declare appending the list of given size
/// TODO: optimise insertion of empty list
pub fn append_list<'a>(&'a mut self, len: usize) -> &'a mut RlpStream {
// push new list
let position = self.encoder.bytes.len();
match len {
0 => {
// we may finish, if the appended list len is equal 0
self.encoder.insert_list_len_at_pos(0, position);
self.encoder.bytes.push(0xc0u8);
self.try_to_finish();
},
_ => self.unfinished_lists.push_back(ListInfo::new(position, len))