From 72080f23637bd5de72af3e0d2075d480a30421ca Mon Sep 17 00:00:00 2001 From: Ashley Date: Sat, 19 Oct 2019 18:36:43 +1300 Subject: [PATCH 1/7] Change DefaultMaxDepth from 1024 to 32 --- srml/contracts/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srml/contracts/src/lib.rs b/srml/contracts/src/lib.rs index 3d8a455ad7b83..e52d059992275 100644 --- a/srml/contracts/src/lib.rs +++ b/srml/contracts/src/lib.rs @@ -325,7 +325,7 @@ parameter_types! { /// A reasonable default value for [`Trait::InstantiateBaseFee`]. pub const DefaultInstantiateBaseFee: u32 = 1000; /// A reasonable default value for [`Trait::MaxDepth`]. - pub const DefaultMaxDepth: u32 = 1024; + pub const DefaultMaxDepth: u32 = 32; /// A reasonable default value for [`Trait::MaxValueSize`]. pub const DefaultMaxValueSize: u32 = 16_384; /// A reasonable default value for [`Trait::BlockGasLimit`]. From ce0cae90811354fc3fbcbb33f29692dde06b8b8e Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 24 Oct 2019 19:53:31 +1300 Subject: [PATCH 2/7] Switch sr-arithmetic benchmarking to criterion --- Cargo.lock | 55 +++++++++++++ core/primitives/Cargo.toml | 2 +- .../benches/{benches.rs => bench.rs} | 0 core/sr-arithmetic/Cargo.toml | 6 +- core/sr-arithmetic/benches/bench.rs | 76 ++++++++++++++++++ core/sr-arithmetic/src/biguint.rs | 80 ------------------- core/sr-arithmetic/src/lib.rs | 4 - 7 files changed, 137 insertions(+), 86 deletions(-) rename core/primitives/benches/{benches.rs => bench.rs} (100%) create mode 100644 core/sr-arithmetic/benches/bench.rs diff --git a/Cargo.lock b/Cargo.lock index a87da6586cc6e..e33ca23d03092 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,6 +539,30 @@ dependencies = [ "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "criterion" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion-plot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "csv 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xoshiro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", + "tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "criterion-plot" version = "0.3.1" @@ -549,6 +573,15 @@ dependencies = [ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "criterion-plot" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-channel" version = "0.3.9" @@ -3317,6 +3350,15 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_os" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand_pcg" version = "0.1.2" @@ -3343,6 +3385,14 @@ dependencies = [ "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand_xoshiro" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rayon" version = "1.2.0" @@ -3845,6 +3895,7 @@ dependencies = [ name = "sr-arithmetic" version = "2.0.0" dependencies = [ + "criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "integer-sqrt 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -6826,7 +6877,9 @@ dependencies = [ "checksum core-foundation-sys 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0363053954f3e679645fc443321ca128b7b950a6fe288cf5f9335cc22ee58394" +"checksum criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "938703e165481c8d612ea3479ac8342e5615185db37765162e762ec3523e2fc6" "checksum criterion-plot 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76f9212ddf2f4a9eb2d401635190600656a1f88a932ef53d06e7fa4c7e02fb8e" +"checksum criterion-plot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eccdc6ce8bbe352ca89025bee672aa6d24f4eb8c53e3a8b5d1bc58011da072a2" "checksum crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c8ec7fcd21571dc78f96cc96243cab8d8f035247c3efd16c687be154c3fa9efa" "checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71" "checksum crossbeam-epoch 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fedcd6772e37f3da2a9af9bf12ebe046c0dfe657992377b4df982a2b54cd37a9" @@ -7081,9 +7134,11 @@ dependencies = [ "checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" "checksum rand_jitter 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b" "checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_os 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a788ae3edb696cfcba1c19bfd388cc4b8c21f8a408432b199c072825084da58a" "checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" "checksum rand_xoshiro 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "03b418169fb9c46533f326efd6eed2576699c44ca92d3052a066214a8d828929" +"checksum rand_xoshiro 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0e18c91676f670f6f0312764c759405f13afb98d5d73819840cf72a518487bff" "checksum rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "83a27732a533a1be0a0035a111fe76db89ad312f6f0347004c220c57f209a123" "checksum rayon-core 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "98dcf634205083b17d0861252431eb2acbfb698ab7478a2d20de07954f47ec7b" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 557ce55b88315..82f614be2b75b 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -43,7 +43,7 @@ rand = "0.7.2" criterion = "0.2.11" [[bench]] -name = "benches" +name = "bench" harness = false [lib] diff --git a/core/primitives/benches/benches.rs b/core/primitives/benches/bench.rs similarity index 100% rename from core/primitives/benches/benches.rs rename to core/primitives/benches/bench.rs diff --git a/core/sr-arithmetic/Cargo.toml b/core/sr-arithmetic/Cargo.toml index 3962daf493146..6456c65aedd45 100644 --- a/core/sr-arithmetic/Cargo.toml +++ b/core/sr-arithmetic/Cargo.toml @@ -15,9 +15,9 @@ substrate-debug-derive = { path = "../primitives/debug-derive", default-features [dev-dependencies] primitive-types = "0.6.0" rand = "0.7.2" +criterion = "0.3" [features] -bench = [] default = ["std"] std = [ "codec/std", @@ -26,3 +26,7 @@ std = [ "serde", "substrate-debug-derive/std", ] + +[[bench]] +name = "bench" +harness = false \ No newline at end of file diff --git a/core/sr-arithmetic/benches/bench.rs b/core/sr-arithmetic/benches/bench.rs new file mode 100644 index 0000000000000..a1840a0446507 --- /dev/null +++ b/core/sr-arithmetic/benches/bench.rs @@ -0,0 +1,76 @@ +use criterion::{Criterion, criterion_group, criterion_main}; +use sr_arithmetic::biguint::{BigUint, Single}; + +fn random_big_uint(size: usize) -> BigUint { + use rand::Rng; + let mut rng = rand::thread_rng(); + let digits: Vec<_> = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect(); + BigUint::from_limbs(&digits) +} + +fn bench_addition_2_digit(c: &mut Criterion) { + let a = random_big_uint(2); + let b = random_big_uint(2); + c.bench_function("addition 2 digit", |bencher| bencher.iter(|| { + let _ = a.clone().add(&b); + })); +} + +fn bench_addition_4_digit(c: &mut Criterion) { + let a = random_big_uint(4); + let b = random_big_uint(4); + c.bench_function("addition 4 digit", |bencher| bencher.iter(|| { + let _ = a.clone().add(&b); + })); +} + +fn bench_subtraction_2_digit(c: &mut Criterion) { + let a = random_big_uint(2); + let b = random_big_uint(2); + c.bench_function("subtraction 2 digit", |bencher| bencher.iter(|| { + let _ = a.clone().sub(&b); + })); +} + +fn bench_subtraction_4_digit(c: &mut Criterion) { + let a = random_big_uint(4); + let b = random_big_uint(4); + c.bench_function("subtraction 4 digit", |bencher| bencher.iter(|| { + let _ = a.clone().sub(&b); + })); +} + +fn bench_multiplication_2_digit(c: &mut Criterion) { + let a = random_big_uint(2); + let b = random_big_uint(2); + c.bench_function("multiplication 2 digit", |bencher| bencher.iter(|| { + let _ = a.clone().mul(&b); + })); +} + +fn bench_multiplication_4_digit(c: &mut Criterion) { + let a = random_big_uint(4); + let b = random_big_uint(4); + c.bench_function("multiplication 4 digit", |bencher| bencher.iter(|| { + let _ = a.clone().mul(&b); + })); +} + +fn bench_division_4_digit(c: &mut Criterion) { + let a = random_big_uint(4); + let b = random_big_uint(2); + c.bench_function("division 4 digit", |bencher| bencher.iter(|| { + let _ = a.clone().div(&b, true); + })); +} + +criterion_group!{ + name = benches; + config = Criterion::default(); + targets = + bench_addition_2_digit, bench_addition_4_digit, + bench_subtraction_2_digit, bench_subtraction_4_digit, + bench_multiplication_2_digit, bench_multiplication_4_digit, + bench_division_4_digit +} +criterion_main!(benches); \ No newline at end of file diff --git a/core/sr-arithmetic/src/biguint.rs b/core/sr-arithmetic/src/biguint.rs index b7d93c2f0d3ca..cc2d13850e15b 100644 --- a/core/sr-arithmetic/src/biguint.rs +++ b/core/sr-arithmetic/src/biguint.rs @@ -561,8 +561,6 @@ impl From for BigUint { #[cfg(test)] pub mod tests { use super::*; - #[cfg(feature = "bench")] - use test::Bencher; fn with_limbs(n: usize) -> BigUint { BigUint { digits: vec![1; n] } @@ -734,82 +732,4 @@ pub mod tests { assert_eq!(b.clone().div_unit(7), BigUint::from(((B + 100) / 7) as Single)); } - - #[cfg(feature = "bench")] - fn random_big_uint(size: usize) -> BigUint { - use rand::Rng; - let mut rng = rand::thread_rng(); - let digits = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect(); - BigUint { digits } - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_addition_2_digit(bencher: &mut Bencher) { - let a = random_big_uint(2); - let b = random_big_uint(2); - bencher.iter(|| { - let _ = a.clone().add(&b); - }); - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_addition_4_digit(bencher: &mut Bencher) { - let a = random_big_uint(4); - let b = random_big_uint(4); - bencher.iter(|| { - let _ = a.clone().add(&b); - }); - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_subtraction_2_digit(bencher: &mut Bencher) { - let a = random_big_uint(2); - let b = random_big_uint(2); - bencher.iter(|| { - let _ = a.clone().sub(&b); - }); - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_subtraction_4_digit(bencher: &mut Bencher) { - let a = random_big_uint(4); - let b = random_big_uint(4); - bencher.iter(|| { - let _ = a.clone().sub(&b); - }); - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_multiplication_2_digit(bencher: &mut Bencher) { - let a = random_big_uint(2); - let b = random_big_uint(2); - bencher.iter(|| { - let _ = a.clone().mul(&b); - }); - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_multiplication_4_digit(bencher: &mut Bencher) { - let a = random_big_uint(4); - let b = random_big_uint(4); - bencher.iter(|| { - let _ = a.clone().mul(&b); - }); - } - - #[cfg(feature = "bench")] - #[bench] - fn bench_division_4_digit(bencher: &mut Bencher) { - let a = random_big_uint(4); - let b = random_big_uint(2); - bencher.iter(|| { - let _ = a.clone().div(&b, true); - }); - } } diff --git a/core/sr-arithmetic/src/lib.rs b/core/sr-arithmetic/src/lib.rs index 847ca9e797cb0..7b285002e5400 100644 --- a/core/sr-arithmetic/src/lib.rs +++ b/core/sr-arithmetic/src/lib.rs @@ -18,10 +18,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -// to allow benchmarking -#![cfg_attr(feature = "bench", feature(test))] -#[cfg(feature = "bench")] extern crate test; - /// Copied from `sr-primitives` and documented there. #[cfg(test)] macro_rules! assert_eq_error_rate { From 59dc5778887195469625ada85f2f844a527816e3 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 24 Oct 2019 09:02:28 +0100 Subject: [PATCH 3/7] Update core/sr-arithmetic/Cargo.toml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- core/sr-arithmetic/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/sr-arithmetic/Cargo.toml b/core/sr-arithmetic/Cargo.toml index 6456c65aedd45..fd484671dd848 100644 --- a/core/sr-arithmetic/Cargo.toml +++ b/core/sr-arithmetic/Cargo.toml @@ -29,4 +29,4 @@ std = [ [[bench]] name = "bench" -harness = false \ No newline at end of file +harness = false From 4d05c62ce5b6e23ffa5dd5dd773298a236ada336 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 24 Oct 2019 09:02:39 +0100 Subject: [PATCH 4/7] Update core/sr-arithmetic/benches/bench.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- core/sr-arithmetic/benches/bench.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/sr-arithmetic/benches/bench.rs b/core/sr-arithmetic/benches/bench.rs index a1840a0446507..0cf62e979f164 100644 --- a/core/sr-arithmetic/benches/bench.rs +++ b/core/sr-arithmetic/benches/bench.rs @@ -73,4 +73,4 @@ criterion_group!{ bench_multiplication_2_digit, bench_multiplication_4_digit, bench_division_4_digit } -criterion_main!(benches); \ No newline at end of file +criterion_main!(benches); From 49faba4b4145326058ba742cc884b950f1d382d8 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 24 Oct 2019 21:26:36 +1300 Subject: [PATCH 5/7] Test on variable limb lengths --- core/sr-arithmetic/benches/bench.rs | 92 +++++++++++++---------------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/core/sr-arithmetic/benches/bench.rs b/core/sr-arithmetic/benches/bench.rs index 0cf62e979f164..706b18ec1a5f8 100644 --- a/core/sr-arithmetic/benches/bench.rs +++ b/core/sr-arithmetic/benches/bench.rs @@ -1,4 +1,18 @@ -use criterion::{Criterion, criterion_group, criterion_main}; +// Copyright 2019 Parity Technologies +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use criterion::{Criterion, Throughput, BenchmarkId, criterion_group, criterion_main}; use sr_arithmetic::biguint::{BigUint, Single}; fn random_big_uint(size: usize) -> BigUint { @@ -8,69 +22,47 @@ fn random_big_uint(size: usize) -> BigUint { BigUint::from_limbs(&digits) } -fn bench_addition_2_digit(c: &mut Criterion) { - let a = random_big_uint(2); - let b = random_big_uint(2); - c.bench_function("addition 2 digit", |bencher| bencher.iter(|| { - let _ = a.clone().add(&b); - })); -} +fn bench_op(c: &mut Criterion, name: &str, op: F) { + let mut group = c.benchmark_group(name); -fn bench_addition_4_digit(c: &mut Criterion) { - let a = random_big_uint(4); - let b = random_big_uint(4); - c.bench_function("addition 4 digit", |bencher| bencher.iter(|| { - let _ = a.clone().add(&b); - })); -} + for size in [2, 4, 6, 8, 10].iter() { + group.throughput(Throughput::Elements(*size)); + group.bench_with_input(BenchmarkId::from_parameter(size), size, |bencher, &size| { + let a = random_big_uint(size as usize); + let b = random_big_uint(size as usize); -fn bench_subtraction_2_digit(c: &mut Criterion) { - let a = random_big_uint(2); - let b = random_big_uint(2); - c.bench_function("subtraction 2 digit", |bencher| bencher.iter(|| { - let _ = a.clone().sub(&b); - })); + bencher.iter(|| op(&a, &b)); + }); + } } -fn bench_subtraction_4_digit(c: &mut Criterion) { - let a = random_big_uint(4); - let b = random_big_uint(4); - c.bench_function("subtraction 4 digit", |bencher| bencher.iter(|| { - let _ = a.clone().sub(&b); - })); +fn bench_addition(c: &mut Criterion) { + bench_op(c, "addition", |a, b| { + a.clone().add(&b); + }); } -fn bench_multiplication_2_digit(c: &mut Criterion) { - let a = random_big_uint(2); - let b = random_big_uint(2); - c.bench_function("multiplication 2 digit", |bencher| bencher.iter(|| { - let _ = a.clone().mul(&b); - })); +fn bench_subtraction(c: &mut Criterion) { + bench_op(c, "subtraction", |a, b| { + let _ = a.clone().sub(&b); + }); } -fn bench_multiplication_4_digit(c: &mut Criterion) { - let a = random_big_uint(4); - let b = random_big_uint(4); - c.bench_function("multiplication 4 digit", |bencher| bencher.iter(|| { - let _ = a.clone().mul(&b); - })); +fn bench_multiplication(c: &mut Criterion) { + bench_op(c, "multiplication", |a, b| { + a.clone().mul(&b); + }); } -fn bench_division_4_digit(c: &mut Criterion) { - let a = random_big_uint(4); - let b = random_big_uint(2); - c.bench_function("division 4 digit", |bencher| bencher.iter(|| { - let _ = a.clone().div(&b, true); - })); +fn bench_division(c: &mut Criterion) { + bench_op(c, "division", |a, b| { + a.clone().div(&b, true); + }); } criterion_group!{ name = benches; config = Criterion::default(); - targets = - bench_addition_2_digit, bench_addition_4_digit, - bench_subtraction_2_digit, bench_subtraction_4_digit, - bench_multiplication_2_digit, bench_multiplication_4_digit, - bench_division_4_digit + targets = bench_addition, bench_subtraction, bench_multiplication, bench_division } criterion_main!(benches); From f0130daf5f857a7bb69fe63b6d8a28b5257d4460 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 24 Oct 2019 21:59:05 +1300 Subject: [PATCH 6/7] Change license --- core/sr-arithmetic/benches/bench.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/core/sr-arithmetic/benches/bench.rs b/core/sr-arithmetic/benches/bench.rs index 706b18ec1a5f8..d18e699341c75 100644 --- a/core/sr-arithmetic/benches/bench.rs +++ b/core/sr-arithmetic/benches/bench.rs @@ -1,16 +1,18 @@ -// Copyright 2019 Parity Technologies -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Copyright 2019 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . use criterion::{Criterion, Throughput, BenchmarkId, criterion_group, criterion_main}; use sr_arithmetic::biguint::{BigUint, Single}; From da54522bd6e003709280348b47f89e6564da291e Mon Sep 17 00:00:00 2001 From: Ashley Date: Sun, 27 Oct 2019 20:33:05 +1300 Subject: [PATCH 7/7] Rework division --- core/sr-arithmetic/benches/bench.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/core/sr-arithmetic/benches/bench.rs b/core/sr-arithmetic/benches/bench.rs index d18e699341c75..22c0ce6f566e9 100644 --- a/core/sr-arithmetic/benches/bench.rs +++ b/core/sr-arithmetic/benches/bench.rs @@ -16,9 +16,9 @@ use criterion::{Criterion, Throughput, BenchmarkId, criterion_group, criterion_main}; use sr_arithmetic::biguint::{BigUint, Single}; +use rand::Rng; fn random_big_uint(size: usize) -> BigUint { - use rand::Rng; let mut rng = rand::thread_rng(); let digits: Vec<_> = (0..size).map(|_| rng.gen_range(0, Single::max_value())).collect(); BigUint::from_limbs(&digits) @@ -40,7 +40,7 @@ fn bench_op(c: &mut Criterion, name: &str, op: F) { fn bench_addition(c: &mut Criterion) { bench_op(c, "addition", |a, b| { - a.clone().add(&b); + let _ = a.clone().add(&b); }); } @@ -52,14 +52,24 @@ fn bench_subtraction(c: &mut Criterion) { fn bench_multiplication(c: &mut Criterion) { bench_op(c, "multiplication", |a, b| { - a.clone().mul(&b); + let _ = a.clone().mul(&b); }); } fn bench_division(c: &mut Criterion) { - bench_op(c, "division", |a, b| { - a.clone().div(&b, true); - }); + let mut group = c.benchmark_group("division"); + + for size in [4, 6, 8, 10].iter() { + group.throughput(Throughput::Elements(*size)); + group.bench_with_input(BenchmarkId::from_parameter(size), size, |bencher, &size| { + let a = random_big_uint(size as usize); + let b = random_big_uint(rand::thread_rng().gen_range(2, size as usize)); + + bencher.iter(|| { + let _ = a.clone().div(&b, true); + }); + }); + } } criterion_group!{