This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ethereum-types; Modify features config of primitive-types Signed-off-by: koushiro <[email protected]> * Modify doc of primitive-types Signed-off-by: koushiro <[email protected]> * Update ci config Signed-off-by: koushiro <[email protected]>
- Loading branch information
Showing
11 changed files
with
668 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "ethereum-types" | ||
version = "0.5.0" | ||
authors = ["Parity Technologies <[email protected]> and koushiro <[email protected]>"] | ||
license = "MIT" | ||
homepage = "https://github.com/paritytech/parity-common" | ||
description = "Ethereum types" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
ustd = { path = "../ustd", default-features = false } | ||
primitive-types = { path = "../primitive-types", default-features = false } | ||
impl-serde = { path = "../primitive-types/impls/serde", default-features = false, optional = true } | ||
impl-codec = { path = "../primitive-types/impls/codec", default-features = false, optional = true } | ||
impl-rlp = { path = "../primitive-types/impls/rlp", default-features = false, optional = true } | ||
fixed-hash = { version = "0.3", default-features = false } | ||
tiny-keccak = "1.4" # support `no_std` | ||
crunchy = { version = "0.2.1", default-features = false, features = ["limit_256"] } | ||
|
||
[dev-dependencies] | ||
rustc-hex = { version = "2.0", default-features = false } | ||
rand = { version = "0.4" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ustd/std", | ||
"fixed-hash/std", | ||
"primitive-types/std", | ||
] | ||
serde = ["std", "impl-serde", "primitive-types/serde"] | ||
codec = ["impl-codec", "primitive-types/codec"] | ||
rlp = ["impl-rlp", "primitive-types/rlp"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Copyright 2015-2017 Parity Technologies | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! benchmarking for bigint | ||
//! should be started with: | ||
//! ```bash | ||
//! rustup run nightly cargo bench | ||
//! ``` | ||
#![feature(test)] | ||
#![feature(asm)] | ||
|
||
extern crate test; | ||
|
||
use test::{black_box, Bencher}; | ||
|
||
use ethereum_types::{U128, U256, U512}; | ||
|
||
#[bench] | ||
fn u128_mul(b: &mut Bencher) { | ||
b.iter(|| { | ||
let n = black_box(10000); | ||
(1..n).fold(U128([12345u64, 0u64]), |old, new| { | ||
old.overflowing_mul(U128::from(new | 1)).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_add(b: &mut Bencher) { | ||
b.iter(|| { | ||
let n = black_box(10000); | ||
let zero = black_box(U256::zero()); | ||
(0..n).fold(zero, |old, new| { | ||
old.overflowing_add(U256::from(black_box(new))).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_sub(b: &mut Bencher) { | ||
b.iter(|| { | ||
let n = black_box(10000); | ||
let max = black_box(U256::max_value()); | ||
(0..n).fold(max, |old, new| { | ||
old.overflowing_sub(U256::from(black_box(new))).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_mul(b: &mut Bencher) { | ||
b.iter(|| { | ||
(1..10000).fold(black_box(U256::one()), |old, new| { | ||
old.overflowing_mul(U256::from(black_box(new | 1))).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_mul_small(b: &mut Bencher) { | ||
b.iter(|| { | ||
(1..77).fold(black_box(U256::one()), |old, _| { | ||
old.overflowing_mul(U256::from(black_box(10))).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_full_mul(b: &mut Bencher) { | ||
b.iter(|| { | ||
let n = black_box(10000); | ||
let one = black_box(U256::one()); | ||
(1..n).map(|n| n | 1).fold(one, |old, new| { | ||
let new = black_box(new); | ||
let U512(ref u512words) = old.full_mul(U256([new, new, new, new])); | ||
U256([u512words[0], u512words[2], u512words[2], u512words[3]]) | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_from_le(b: &mut Bencher) { | ||
b.iter(|| { | ||
let raw = black_box([ | ||
1u8, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, | ||
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, | ||
]); | ||
let _ = U256::from_little_endian(&raw[..]); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u256_from_be(b: &mut Bencher) { | ||
b.iter(|| { | ||
let raw = black_box([ | ||
1u8, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, | ||
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, | ||
]); | ||
let _ = U256::from_big_endian(&raw[..]); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u512_add(b: &mut Bencher) { | ||
b.iter(|| { | ||
let n = black_box(10000); | ||
let zero = black_box(U512::zero()); | ||
(0..n).fold(zero, |old, new| { | ||
let new = black_box(new); | ||
old.overflowing_add(U512([new, new, new, new, new, new, new, new])) | ||
.0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u512_sub(b: &mut Bencher) { | ||
b.iter(|| { | ||
let n = black_box(10000); | ||
let max = black_box(U512::max_value()); | ||
(0..n).fold(max, |old, new| { | ||
let new = black_box(new); | ||
let p = new % 2; | ||
old.overflowing_sub(U512([p, p, p, p, p, p, p, new])).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u512_mul(b: &mut Bencher) { | ||
b.iter(|| { | ||
(1..10000).fold(black_box(U512::one()), |old, new| { | ||
old.overflowing_mul(U512::from(black_box(new | 1))).0 | ||
}) | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn u512_mul_small(b: &mut Bencher) { | ||
b.iter(|| { | ||
(1..153).fold(black_box(U512::one()), |old, _| { | ||
old.overflowing_mul(U512::from(black_box(10))).0 | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
|
||
use test::Bencher; | ||
|
||
use ethereum_types::{Bloom, BloomInput}; | ||
use tiny_keccak::keccak256; | ||
|
||
fn from_hex_str(s: &str) -> Vec<u8> { | ||
rustc_hex::FromHex::from_hex(s).unwrap() | ||
} | ||
|
||
fn test_bloom() -> Bloom { | ||
let hex = from_hex_str( | ||
"00000000000000000000000000000000\ | ||
00000000100000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000002020000000000000000000000\ | ||
00000000000000000000000800000000\ | ||
10000000000000000000000000000000\ | ||
00000000000000000000001000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000\ | ||
00000000000000000000000000000000", | ||
); | ||
Bloom::from_slice(&hex) | ||
} | ||
|
||
fn test_topic() -> Vec<u8> { | ||
from_hex_str("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc") | ||
} | ||
|
||
fn test_address() -> Vec<u8> { | ||
from_hex_str("ef2d6d194084c2de36e0dabfce45d046b37d1106") | ||
} | ||
|
||
fn test_dummy() -> Vec<u8> { | ||
b"123456".to_vec() | ||
} | ||
|
||
fn test_dummy2() -> Vec<u8> { | ||
b"654321".to_vec() | ||
} | ||
|
||
#[bench] | ||
fn accrue_raw(b: &mut Bencher) { | ||
let mut bloom = Bloom::default(); | ||
let topic = test_topic(); | ||
let address = test_address(); | ||
b.iter(|| { | ||
bloom.accrue(BloomInput::Raw(&topic)); | ||
bloom.accrue(BloomInput::Raw(&address)); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn accrue_hash(b: &mut Bencher) { | ||
let mut bloom = Bloom::default(); | ||
let topic = keccak256(&test_topic()); | ||
let address = keccak256(&test_address()); | ||
b.iter(|| { | ||
bloom.accrue(BloomInput::Hash(&topic)); | ||
bloom.accrue(BloomInput::Hash(&address)); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn contains_input_raw(b: &mut Bencher) { | ||
let bloom = test_bloom(); | ||
let topic = test_topic(); | ||
let address = test_address(); | ||
b.iter(|| { | ||
assert!(bloom.contains_input(BloomInput::Raw(&topic))); | ||
assert!(bloom.contains_input(BloomInput::Raw(&address))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn does_not_contain_raw(b: &mut Bencher) { | ||
let bloom = test_bloom(); | ||
let dummy = test_dummy(); | ||
let dummy2 = test_dummy2(); | ||
b.iter(|| { | ||
assert!(!bloom.contains_input(BloomInput::Raw(&dummy))); | ||
assert!(!bloom.contains_input(BloomInput::Raw(&dummy2))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn contains_input_hash(b: &mut Bencher) { | ||
let bloom = test_bloom(); | ||
let topic = keccak256(&test_topic()); | ||
let address = keccak256(&test_address()); | ||
b.iter(|| { | ||
assert!(bloom.contains_input(BloomInput::Hash(&topic))); | ||
assert!(bloom.contains_input(BloomInput::Hash(&address))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn does_not_contain_hash(b: &mut Bencher) { | ||
let bloom = test_bloom(); | ||
let dummy = keccak256(&test_dummy()); | ||
let dummy2 = keccak256(&test_dummy2()); | ||
b.iter(|| { | ||
assert!(!bloom.contains_input(BloomInput::Hash(&dummy))); | ||
assert!(!bloom.contains_input(BloomInput::Hash(&dummy2))); | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn does_not_contain_random_hash(b: &mut Bencher) { | ||
let bloom = test_bloom(); | ||
let dummy: Vec<_> = (0..255u8).into_iter().map(|i| keccak256(&[i])).collect(); | ||
b.iter(|| { | ||
for d in &dummy { | ||
assert!(!bloom.contains_input(BloomInput::Hash(d))); | ||
} | ||
}); | ||
} |
Oops, something went wrong.