Skip to content
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
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ script:
- cargo build --all
- cargo test --all --exclude uint --exclude fixed-hash
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then
cd fixed-hash/ && cargo test --no-default-features --features="rustc-hex,byteorder" && cd ..;
cd keccak-hash/ && cargo test --no-default-features && cd ..;
cd contract-address/ && cargo test --features=external_doc && cd ..;
fi
- cd ethbloom/ && cargo test --no-default-features --features="rustc-hex" && cargo test --benches && cd ..
- cd fixed-hash/ && cargo test --all-features && cd ..
- cd uint/ && cargo test --features=std,quickcheck --release && cd ..
- cd ethbloom/ && cargo test --no-default-features --features="rustc-hex" && cargo check --benches && cd ..
- cd fixed-hash/ && cargo test --all-features && cargo test --no-default-features --features="byteorder,rustc-hex" && cd ..
- cd uint/ && cargo test --all-features && cargo test --no-default-features && cd ..
- cd keccak-hash/ && cargo test --no-default-features && cd ..
- cd plain_hasher/ && cargo test --no-default-features && cargo check --benches && cd ..
- cd parity-bytes/ && cargo test --no-default-features && cd ..
- cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd ..
- cd parity-util-mem/ && cargo test --features=jemalloc-global && cd ..
- cd parity-util-mem/ && cargo test --features=mimalloc-global && cd ..
- cd rlp/ && cargo test --no-default-features && cargo check --benches && cd ..
2 changes: 1 addition & 1 deletion primitive-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ libc = ["fixed-hash/libc"]
rustc-hex = ["fixed-hash/rustc-hex"]
serde = ["std", "impl-serde"]
codec = ["impl-codec"]
rlp = ["std", "impl-rlp"]
rlp = ["impl-rlp"]
6 changes: 5 additions & 1 deletion primitive-types/impls/rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ homepage = "https://github.com/paritytech/parity-common"
description = "RLP serialization support for uint and fixed hash."

[dependencies]
rlp = { version = "0.4", path = "../../../rlp" }
rlp = { version = "0.4", path = "../../../rlp", default-features = false }

[features]
default = ["std"]
std = ["rlp/std"]
2 changes: 2 additions & 0 deletions primitive-types/impls/rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

//! RLP serialization support for uint and fixed hash.

#![cfg_attr(not(feature = "std"), no_std)]

#[doc(hidden)]
pub extern crate rlp;

Expand Down
12 changes: 11 additions & 1 deletion rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[dependencies]
rustc-hex = {version = "2.0", default-features = false }
rustc-hex = { version = "2.0", default-features = false }

[dev-dependencies]
criterion = "0.3"
hex-literal = "0.2"
primitive-types = { path = "../primitive-types", version = "0.5", features = ["impl-rlp"] }

[features]
default = ["std"]
std = ["rustc-hex/std"]

[[bench]]
name = "rlp"
path = "benches/rlp.rs"
harness = false
152 changes: 69 additions & 83 deletions rlp/benches/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,111 +7,97 @@
// except according to those terms.

//! benchmarking for rlp
//! should be started with:
//! ```bash
//! multirust run nightly cargo bench
//! ```

#![feature(test)]
use criterion::{criterion_group, criterion_main, Criterion};

// TODO: get rid of this one fine day: https://doc.rust-lang.org/nightly/edition-guide/rust-2018/module-system/path-clarity.html#an-exception
extern crate test;

use primitive_types::U256;
use rlp::{RlpStream, Rlp};
use test::Bencher;

#[bench]
fn bench_stream_u64_value(b: &mut Bencher) {
b.iter(|| {
// u64
let mut stream = RlpStream::new();
stream.append(&0x1023456789abcdefu64);
fn bench_encode(c: &mut Criterion) {
c.bench_function("encode_u64", |b| b.iter(|| {
let mut stream = rlp::RlpStream::new();
stream.append(&0x1023_4567_89ab_cdefu64);
let _ = stream.out();
});
}

#[bench]
fn bench_decode_u64_value(b: &mut Bencher) {
b.iter(|| {
// u64
let data = vec![0x88, 0x10, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
let rlp = Rlp::new(&data);
let _: u64 = rlp.as_val().unwrap();
});
}

#[bench]
fn bench_stream_u256_value(b: &mut Bencher) {
b.iter(|| {
// u256
let mut stream = RlpStream::new();
let uint: U256 = "8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0".into();
}));
c.bench_function("encode_u256", |b| b.iter(|| {
let mut stream = rlp::RlpStream::new();
let uint: primitive_types::U256 = "8090a0b0c0d0e0f00910203040506077000000000000000100000000000012f0".into();
stream.append(&uint);
let _ = stream.out();
});
}

#[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 = rlp.as_val().unwrap();
});
}

#[bench]
fn bench_stream_nested_empty_lists(b: &mut Bencher) {
b.iter(|| {
}));
c.bench_function("encode_1000_u64", |b| b.iter(|| {
let mut stream = rlp::RlpStream::new_list(1000);
for i in 0..1000u64 {
stream.append(&i);
}
let _ = stream.out();
}));
c.bench_function("encode_nested_empty_lists", |b| b.iter(|| {
// [ [], [[]], [ [], [[]] ] ]
let mut stream = RlpStream::new_list(3);
let mut stream = rlp::RlpStream::new_list(3);
stream.begin_list(0);
stream.begin_list(1).begin_list(0);
stream.begin_list(2).begin_list(0).begin_list(1).begin_list(0);
let _ = stream.out();
});
}));
c.bench_function("encode_1000_empty_lists", |b| b.iter(|| {
let mut stream = rlp::RlpStream::new_list(1000);
for _ in 0..1000 {
stream.begin_list(0);
}
let _ = stream.out();
}));
}

#[bench]
fn bench_decode_nested_empty_lists(b: &mut Bencher) {
b.iter(|| {
fn bench_decode(c: &mut Criterion) {
c.bench_function("decode_u64", |b| b.iter(|| {
let data = vec![0x88, 0x10, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
let rlp = rlp::Rlp::new(&data);
let _: u64 = rlp.as_val().unwrap();
}));
c.bench_function("decode_u256", |b| b.iter(|| {
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::Rlp::new(&data);
let _ : primitive_types::U256 = rlp.as_val().unwrap();
}));
c.bench_function("decode_1000_u64", |b| {
let mut stream = rlp::RlpStream::new_list(1000);
for i in 0..1000u64 {
stream.append(&i);
}
let data= stream.out();
b.iter(|| {
let rlp = rlp::Rlp::new(&data);
for i in 0..1000 {
let _: u64 = rlp.val_at(i).unwrap();
}
});
});
c.bench_function("decode_nested_empty_lists", |b| b.iter(|| {
// [ [], [[]], [ [], [[]] ] ]
let data = vec![0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0];
let rlp = Rlp::new(&data);
let rlp = rlp::Rlp::new(&data);
let _v0: Vec<u16> = rlp.at(0).unwrap().as_list().unwrap();
let _v1: Vec<u16> = rlp.at(1).unwrap().at(0).unwrap().as_list().unwrap();
let nested_rlp = rlp.at(2).unwrap();
let _v2a: Vec<u16> = nested_rlp.at(0).unwrap().as_list().unwrap();
let _v2b: Vec<u16> = nested_rlp.at(1).unwrap().at(0).unwrap().as_list().unwrap();
});
}

#[bench]
fn bench_stream_1000_empty_lists(b: &mut Bencher) {
b.iter(|| {
let mut stream = RlpStream::new_list(1000);
}));
c.bench_function("decode_1000_empty_lists", |b| {
let mut stream = rlp::RlpStream::new_list(1000);
for _ in 0..1000 {
stream.begin_list(0);
}
let _ = stream.out();
let data = stream.out();
b.iter(|| {
let rlp = rlp::Rlp::new(&data);
for i in 0..1000 {
let _: Vec<u8> = rlp.at(i).unwrap().as_list().unwrap();
}
});
});
}

#[bench]
fn bench_decode_1000_values(b: &mut Bencher) {
let mut stream = RlpStream::new_list(1000);
for _ in 0..1000 {
stream.append(&U256::from(1));
}
let data= stream.out();
b.iter(|| {
let rlp = Rlp::new(&data);
for i in 0..1000 {
let _: U256 = rlp.val_at(i).unwrap();
}
});
}
criterion_group!(benches, bench_encode, bench_decode);
criterion_main!(benches);
4 changes: 3 additions & 1 deletion rlp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::fmt;
use core::fmt;
#[cfg(feature = "std")]
use std::error::Error as StdError;

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -36,6 +37,7 @@ pub enum DecoderError {
Custom(&'static str),
}

#[cfg(feature = "std")]
impl StdError for DecoderError {
fn description(&self) -> &str {
"builder error"
Expand Down
20 changes: 12 additions & 8 deletions rlp/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{mem, str};
use std::iter::{once, empty};
use crate::traits::{Encodable, Decodable};
#[cfg(not(feature = "std"))]
use alloc::{borrow::ToOwned, vec::Vec, string::String};
use core::{mem, str};
use core::iter::{once, empty};

use crate::error::DecoderError;
use crate::rlpin::Rlp;
use crate::stream::RlpStream;
use crate::{Rlp, DecoderError};
use crate::traits::{Encodable, Decodable};

pub fn decode_usize(bytes: &[u8]) -> Result<usize, DecoderError> {
match bytes.len() {
Expand All @@ -19,9 +23,9 @@ pub fn decode_usize(bytes: &[u8]) -> Result<usize, DecoderError> {
return Err(DecoderError::RlpInvalidIndirection);
}
let mut res = 0usize;
for i in 0..l {
for (i, byte) in bytes.iter().enumerate().take(l) {
let shift = (l - 1 - i) * 8;
res = res + ((bytes[i] as usize) << shift);
res += (*byte as usize) << shift;
}
Ok(res)
}
Expand Down Expand Up @@ -139,9 +143,9 @@ macro_rules! impl_decodable_for_u {
return Err(DecoderError::RlpInvalidIndirection);
}
let mut res = 0 as $name;
for i in 0..l {
for (i, byte) in bytes.iter().enumerate().take(l) {
let shift = (l - 1 - i) * 8;
res = res + ((bytes[i] as $name) << shift);
res += (*byte as $name) << shift;
}
Ok(res)
}
Expand Down
17 changes: 12 additions & 5 deletions rlp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@
//! * You want to get view onto rlp-slice.
//! * You don't want to decode whole rlp at once.

use std::borrow::Borrow;
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

mod traits;
mod error;
mod rlpin;
mod stream;
mod impls;

pub use crate::error::DecoderError;
pub use crate::traits::{Decodable, Encodable};
pub use crate::rlpin::{Rlp, RlpIterator, PayloadInfo, Prototype};
pub use crate::stream::RlpStream;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::borrow::Borrow;

pub use self::error::DecoderError;
pub use self::rlpin::{Rlp, RlpIterator, PayloadInfo, Prototype};
pub use self::stream::RlpStream;
pub use self::traits::{Decodable, Encodable};

/// The RLP encoded empty data (used to mean "null value").
pub const NULL_RLP: [u8; 1] = [0x80; 1];
Expand Down
Loading