Skip to content

Commit 86f7dc0

Browse files
committed
Merge #182: benchmarks: restore benchmarks
e0b95f1 benchmarks: add benchmarks for checksum param generation (Andrew Poelstra) cce8a7a benchmarks: restore benchmarks (Andrew Poelstra) 72421dc clippy: remove a couple redundant test imports (Andrew Poelstra) Pull request description: In #128 we dropped the benchmarks. They can be restored with only minor tweaks, so do so. ACKs for top commit: clarkmoody: ACK e0b95f1 Tree-SHA512: 6a23ba658ac8536ef6feb9400feddfbc4a561ff5ef1e10eb90bab6cf637c631f73901e7d8a738f8bad3389340fb532d74fe6129e2eb3d115ad01436c2ee298e5
2 parents b03c2cc + e0b95f1 commit 86f7dc0

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

src/lib.rs

+78-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ impl From<std::io::Error> for EncodeIoError {
580580
#[cfg(feature = "alloc")]
581581
mod tests {
582582
use super::*;
583-
use crate::{Bech32, Bech32m};
584583

585584
// Tests below using this data, are based on the test vector (from BIP-173):
586585
// BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4: 0014751e76e8199196d454941c45d1b3a323f1433bd6
@@ -706,3 +705,81 @@ mod tests {
706705
assert!(decode(s).is_ok());
707706
}
708707
}
708+
#[cfg(bench)]
709+
mod benches {
710+
use test::{black_box, Bencher};
711+
712+
use crate::{Bech32, Bech32m};
713+
714+
#[bench]
715+
fn bech32_parse_address(bh: &mut Bencher) {
716+
let addr = black_box("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq");
717+
718+
bh.iter(|| {
719+
let tuple = crate::decode(&addr).expect("address is well formed");
720+
black_box(&tuple);
721+
})
722+
}
723+
724+
#[bench]
725+
fn bech32m_parse_address(bh: &mut Bencher) {
726+
let addr = black_box("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297");
727+
728+
bh.iter(|| {
729+
let tuple = crate::decode(&addr).expect("address is well formed");
730+
black_box(&tuple);
731+
})
732+
}
733+
734+
// Encode with allocation.
735+
#[bench]
736+
fn encode_bech32_address(bh: &mut Bencher) {
737+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
738+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
739+
740+
bh.iter(|| {
741+
let s = crate::encode::<Bech32>(hrp, &data).expect("failed to encode");
742+
black_box(&s);
743+
});
744+
}
745+
746+
// Encode without allocation.
747+
#[bench]
748+
fn encode_to_fmt_bech32_address(bh: &mut Bencher) {
749+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
750+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
751+
let mut buf = String::with_capacity(64);
752+
753+
bh.iter(|| {
754+
let res =
755+
crate::encode_to_fmt::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
756+
black_box(&res);
757+
});
758+
}
759+
760+
// Encode with allocation.
761+
#[bench]
762+
fn encode_bech32m_address(bh: &mut Bencher) {
763+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
764+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
765+
766+
bh.iter(|| {
767+
let s = crate::encode::<Bech32m>(hrp, &data).expect("failed to encode");
768+
black_box(&s);
769+
});
770+
}
771+
772+
// Encode without allocation.
773+
#[bench]
774+
fn encode_to_fmt_bech32m_address(bh: &mut Bencher) {
775+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
776+
let (hrp, data) = crate::decode(&addr).expect("address is well formed");
777+
let mut buf = String::with_capacity(64);
778+
779+
bh.iter(|| {
780+
let res =
781+
crate::encode_to_fmt::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
782+
black_box(&res);
783+
});
784+
}
785+
}

src/primitives/checksum.rs

+37
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,40 @@ mod tests {
588588
println!("{}", _s);
589589
}
590590
}
591+
592+
#[cfg(bench)]
593+
mod benches {
594+
use std::io::{sink, Write};
595+
596+
use test::{black_box, Bencher};
597+
598+
use crate::{Fe1024, Fe32, Fe32768, PrintImpl};
599+
600+
#[bench]
601+
fn compute_bech32_params(bh: &mut Bencher) {
602+
bh.iter(|| {
603+
let im = PrintImpl::<Fe1024>::new(
604+
"Bech32",
605+
&[Fe32::A, Fe32::K, Fe32::_5, Fe32::_4, Fe32::A, Fe32::J],
606+
&[Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::P],
607+
);
608+
let res = write!(sink(), "{}", im);
609+
black_box(&im);
610+
black_box(&res);
611+
})
612+
}
613+
614+
#[bench]
615+
fn compute_descriptor_params(bh: &mut Bencher) {
616+
bh.iter(|| {
617+
let im = PrintImpl::<Fe32768>::new(
618+
"DescriptorChecksum",
619+
&[Fe32::_7, Fe32::H, Fe32::_0, Fe32::W, Fe32::_2, Fe32::X, Fe32::V, Fe32::F],
620+
&[Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::Q, Fe32::P],
621+
);
622+
let res = write!(sink(), "{}", im);
623+
black_box(&im);
624+
black_box(&res);
625+
})
626+
}
627+
}

src/segwit.rs

-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ impl From<fmt::Error> for EncodeError {
441441
#[cfg(all(test, feature = "alloc"))]
442442
mod tests {
443443
use super::*;
444-
use crate::primitives::decode::{SegwitCodeLengthError, SegwitHrpstringError};
445444
use crate::primitives::hrp;
446445

447446
#[test]

0 commit comments

Comments
 (0)