Skip to content

Commit a44a447

Browse files
committed
Add benchmarks
Add benchmarks for parsing and encoding bitcoin addresses. Also add two unit tests that parse the same address string used in the benchmarks. Specifically benchmark: - decoding an address - encoding an address with allocation - encoding an address without allocation Do so for both Bitcoin bech32 addresses and Bitcoin bech32m addresses.
1 parent 8f6d5d9 commit a44a447

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ git config --local core.hooksPath githooks/
2727
```
2828

2929
Alternatively add symlinks in your `.git/hooks` directory to any of the githooks we provide.
30+
31+
32+
## Benchmarks
33+
34+
We use a custom Rust compiler configuration conditional to guard the benchmark code. To run the
35+
benchmarks use: `RUSTFLAGS='--cfg=bench' cargo +nightly bench`.

src/lib.rs

+98
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ assert_eq!(variant, Variant::Bech32);
3535
// Coding conventions
3636
#![deny(missing_docs)]
3737

38+
#[cfg(bench)]
39+
extern crate test;
40+
3841
#[cfg(feature = "alloc")]
3942
extern crate alloc;
4043

@@ -1399,4 +1402,99 @@ mod tests {
13991402
assert_eq!(&res, [0x00, 0x01, 0x02].as_ref());
14001403
assert_eq!(variant, Variant::Bech32);
14011404
}
1405+
1406+
#[test]
1407+
#[cfg(feature = "alloc")]
1408+
fn decode_bitcoin_bech32_address() {
1409+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
1410+
let (hrp, _data, variant) = crate::decode(addr).expect("address is well formed");
1411+
assert_eq!(hrp, "bc");
1412+
assert_eq!(variant, Variant::Bech32)
1413+
}
1414+
1415+
#[test]
1416+
#[cfg(feature = "alloc")]
1417+
fn decode_bitcoin_bech32m_address() {
1418+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
1419+
let (hrp, _data, variant) = crate::decode(addr).expect("address is well formed");
1420+
assert_eq!(hrp, "bc");
1421+
assert_eq!(variant, Variant::Bech32m)
1422+
}
1423+
}
1424+
1425+
#[cfg(bench)]
1426+
mod benches {
1427+
use test::{black_box, Bencher};
1428+
1429+
#[bench]
1430+
fn bech32_parse_address(bh: &mut Bencher) {
1431+
let addr = black_box("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq");
1432+
1433+
bh.iter(|| {
1434+
let tuple = crate::decode(&addr).expect("address is well formed");
1435+
black_box(&tuple);
1436+
})
1437+
}
1438+
1439+
#[bench]
1440+
fn bech32m_parse_address(bh: &mut Bencher) {
1441+
let addr = black_box("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297");
1442+
1443+
bh.iter(|| {
1444+
let tuple = crate::decode(&addr).expect("address is well formed");
1445+
black_box(&tuple);
1446+
})
1447+
}
1448+
1449+
// Encode with allocation.
1450+
#[bench]
1451+
fn encode_bech32_address(bh: &mut Bencher) {
1452+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
1453+
let (hrp, data, variant) = crate::decode(&addr).expect("address is well formed");
1454+
1455+
bh.iter(|| {
1456+
let s = crate::encode(&hrp, &data, variant).expect("failed to encode");
1457+
black_box(&s);
1458+
});
1459+
}
1460+
1461+
// Encode without allocation.
1462+
#[bench]
1463+
fn encode_to_fmt_bech32_address(bh: &mut Bencher) {
1464+
let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
1465+
let (hrp, data, variant) = crate::decode(&addr).expect("address is well formed");
1466+
let mut buf = String::with_capacity(64);
1467+
1468+
bh.iter(|| {
1469+
let res =
1470+
crate::encode_to_fmt(&mut buf, &hrp, &data, variant).expect("failed to encode");
1471+
black_box(&res);
1472+
});
1473+
}
1474+
1475+
// Encode with allocation.
1476+
#[bench]
1477+
fn encode_bech32m_address(bh: &mut Bencher) {
1478+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
1479+
let (hrp, data, variant) = crate::decode(&addr).expect("address is well formed");
1480+
1481+
bh.iter(|| {
1482+
let s = crate::encode(&hrp, &data, variant).expect("failed to encode");
1483+
black_box(&s);
1484+
});
1485+
}
1486+
1487+
// Encode without allocation.
1488+
#[bench]
1489+
fn encode_to_fmt_bech32m_address(bh: &mut Bencher) {
1490+
let addr = "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297";
1491+
let (hrp, data, variant) = crate::decode(&addr).expect("address is well formed");
1492+
let mut buf = String::with_capacity(64);
1493+
1494+
bh.iter(|| {
1495+
let res =
1496+
crate::encode_to_fmt(&mut buf, &hrp, &data, variant).expect("failed to encode");
1497+
black_box(&res);
1498+
});
1499+
}
14021500
}

0 commit comments

Comments
 (0)