Skip to content

Commit

Permalink
Merge #182: benchmarks: restore benchmarks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
apoelstra committed Jul 6, 2024
2 parents b03c2cc + e0b95f1 commit 86f7dc0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
79 changes: 78 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@ impl From<std::io::Error> 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
Expand Down Expand Up @@ -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::<Bech32>(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::<Bech32, _>(&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::<Bech32m>(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::<Bech32, _>(&mut buf, hrp, &data).expect("failed to encode");
black_box(&res);
});
}
}
37 changes: 37 additions & 0 deletions src/primitives/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Fe1024>::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::<Fe32768>::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);
})
}
}
1 change: 0 additions & 1 deletion src/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ impl From<fmt::Error> for EncodeError {
#[cfg(all(test, feature = "alloc"))]
mod tests {
use super::*;
use crate::primitives::decode::{SegwitCodeLengthError, SegwitHrpstringError};
use crate::primitives::hrp;

#[test]
Expand Down

0 comments on commit 86f7dc0

Please sign in to comment.