diff --git a/src/lib.rs b/src/lib.rs index 41e474af4..afc8cd990 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -580,7 +580,6 @@ impl From for EncodeIoError { #[cfg(feature = "alloc")] mod tests { use super::*; - use crate::{Bech32, Bech32m}; // Tests below using this data, are based on the test vector (from BIP-173): // BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4: 0014751e76e8199196d454941c45d1b3a323f1433bd6 @@ -706,3 +705,81 @@ mod tests { assert!(decode(s).is_ok()); } } +#[cfg(bench)] +mod benches { + use test::{black_box, Bencher}; + + use crate::{Bech32, Bech32m}; + + #[bench] + fn bech32_parse_address(bh: &mut Bencher) { + let addr = black_box("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"); + + bh.iter(|| { + let tuple = crate::decode(&addr).expect("address is well formed"); + black_box(&tuple); + }) + } + + #[bench] + fn bech32m_parse_address(bh: &mut Bencher) { + let addr = black_box("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297"); + + bh.iter(|| { + let tuple = crate::decode(&addr).expect("address is well formed"); + black_box(&tuple); + }) + } + + // Encode with allocation. + #[bench] + fn encode_bech32_address(bh: &mut Bencher) { + let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"; + let (hrp, data) = crate::decode(&addr).expect("address is well formed"); + + bh.iter(|| { + let s = crate::encode::(hrp, &data).expect("failed to encode"); + black_box(&s); + }); + } + + // Encode without allocation. + #[bench] + fn encode_to_fmt_bech32_address(bh: &mut Bencher) { + let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"; + let (hrp, data) = crate::decode(&addr).expect("address is well formed"); + let mut buf = String::with_capacity(64); + + bh.iter(|| { + let res = + crate::encode_to_fmt::(&mut buf, hrp, &data).expect("failed to encode"); + black_box(&res); + }); + } + + // Encode with allocation. + #[bench] + fn encode_bech32m_address(bh: &mut Bencher) { + let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297"; + let (hrp, data) = crate::decode(&addr).expect("address is well formed"); + + bh.iter(|| { + let s = crate::encode::(hrp, &data).expect("failed to encode"); + black_box(&s); + }); + } + + // Encode without allocation. + #[bench] + fn encode_to_fmt_bech32m_address(bh: &mut Bencher) { + let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297"; + let (hrp, data) = crate::decode(&addr).expect("address is well formed"); + let mut buf = String::with_capacity(64); + + bh.iter(|| { + let res = + crate::encode_to_fmt::(&mut buf, hrp, &data).expect("failed to encode"); + black_box(&res); + }); + } +} diff --git a/src/primitives/checksum.rs b/src/primitives/checksum.rs index 544851166..3e5f82f7d 100644 --- a/src/primitives/checksum.rs +++ b/src/primitives/checksum.rs @@ -588,3 +588,40 @@ mod tests { println!("{}", _s); } } + +#[cfg(bench)] +mod benches { + use std::io::{sink, Write}; + + use test::{black_box, Bencher}; + + use crate::{Fe1024, Fe32, Fe32768, PrintImpl}; + + #[bench] + fn compute_bech32_params(bh: &mut Bencher) { + bh.iter(|| { + let im = PrintImpl::::new( + "Bech32", + &[Fe32::A, Fe32::K, Fe32::_5, Fe32::_4, Fe32::A, Fe32::J], + &[Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::P], + ); + let res = write!(sink(), "{}", im); + black_box(&im); + black_box(&res); + }) + } + + #[bench] + fn compute_descriptor_params(bh: &mut Bencher) { + bh.iter(|| { + let im = PrintImpl::::new( + "DescriptorChecksum", + &[Fe32::_7, Fe32::H, Fe32::_0, Fe32::W, Fe32::_2, Fe32::X, Fe32::V, Fe32::F], + &[Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::P], + ); + let res = write!(sink(), "{}", im); + black_box(&im); + black_box(&res); + }) + } +} diff --git a/src/segwit.rs b/src/segwit.rs index f0ed3bf01..d23f02ea5 100644 --- a/src/segwit.rs +++ b/src/segwit.rs @@ -441,7 +441,6 @@ impl From for EncodeError { #[cfg(all(test, feature = "alloc"))] mod tests { use super::*; - use crate::primitives::decode::{SegwitCodeLengthError, SegwitHrpstringError}; use crate::primitives::hrp; #[test]