Merge rust-bitcoin/rust-bitcoin#1576: bip158: Replace usage of HashSet with BTreeSet

6e56feed57 bip158: Replace usage of HashSet with BTreeSet (Tobin C. Harding)

Pull request description:

  The `bip158` module uses a `HashSet` and in order to do so requires the `hashbrown` dependency for "no-std" builds.

  We can replace the usage of `HashSet` with a `BTreeSet` in `bip158` and remove the `hashbrown` dependency entirely.

  This patch makes no claims about performance cost or benefit of this change. The patch also makes no claims about the validity of the current `HashSet` usage.

  The `hashbrown` dependency and `HashSet` usage can be trivially added back in if someone comes up with perf data to back it up.

ACKs for top commit:
  Kixunil:
    ACK 6e56feed57
  apoelstra:
    ACK 6e56feed57

Tree-SHA512: 6e8d6af7ccf22031a22ea19b731fbe5f6cbfdfb1e510119a7dbdd2e8521eeb3b0d2c7b9e000641deae8960287099dbf58515341720cf7c41d1fcab08308b0b74
This commit is contained in:
Andrew Poelstra 2023-01-23 13:28:37 +00:00
commit 47027081a5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 5 additions and 13 deletions

View File

@ -29,7 +29,7 @@ rust_v_1_53 = []
# Instead no-std enables additional features required for this crate to be usable without std. # Instead no-std enables additional features required for this crate to be usable without std.
# As a result, both can be enabled without conflict. # As a result, both can be enabled without conflict.
std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std", "bitcoin-internals/std"] std = ["secp256k1/std", "bitcoin_hashes/std", "bech32/std", "bitcoin-internals/std"]
no-std = ["hashbrown", "core2/alloc", "bitcoin_hashes/alloc", "secp256k1/alloc"] no-std = ["core2/alloc", "bitcoin_hashes/alloc", "secp256k1/alloc"]
[package.metadata.docs.rs] [package.metadata.docs.rs]
features = [ "std", "secp-recovery", "base64", "rand", "serde", "bitcoinconsensus" ] features = [ "std", "secp-recovery", "base64", "rand", "serde", "bitcoinconsensus" ]
@ -46,7 +46,6 @@ base64 = { version = "0.13.0", optional = true }
bitcoinconsensus = { version = "0.20.2-0.5.0", optional = true, default-features = false } bitcoinconsensus = { version = "0.20.2-0.5.0", optional = true, default-features = false }
# Do NOT use this as a feature! Use the `serde` feature instead. # Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1.0.103", default-features = false, features = [ "derive", "alloc" ], optional = true } actual-serde = { package = "serde", version = "1.0.103", default-features = false, features = [ "derive", "alloc" ], optional = true }
hashbrown = { version = "0.8", optional = true }
[dev-dependencies] [dev-dependencies]
serde_json = "1.0.0" serde_json = "1.0.0"

View File

@ -363,14 +363,14 @@ fn map_to_range(hash: u64, nm: u64) -> u64 { ((hash as u128 * nm as u128) >> 64)
pub struct GcsFilterWriter<'a, W> { pub struct GcsFilterWriter<'a, W> {
filter: GcsFilter, filter: GcsFilter,
writer: &'a mut W, writer: &'a mut W,
elements: HashSet<Vec<u8>>, elements: BTreeSet<Vec<u8>>,
m: u64, m: u64,
} }
impl<'a, W: io::Write> GcsFilterWriter<'a, W> { impl<'a, W: io::Write> GcsFilterWriter<'a, W> {
/// Creates a new [`GcsFilterWriter`] wrapping a generic writer, with specific seed to siphash. /// Creates a new [`GcsFilterWriter`] wrapping a generic writer, with specific seed to siphash.
pub fn new(writer: &'a mut W, k0: u64, k1: u64, m: u64, p: u8) -> GcsFilterWriter<'a, W> { pub fn new(writer: &'a mut W, k0: u64, k1: u64, m: u64, p: u8) -> GcsFilterWriter<'a, W> {
GcsFilterWriter { filter: GcsFilter::new(k0, k1, p), writer, elements: HashSet::new(), m } GcsFilterWriter { filter: GcsFilter::new(k0, k1, p), writer, elements: BTreeSet::new(), m }
} }
/// Adds data to the filter. /// Adds data to the filter.
@ -634,7 +634,7 @@ mod test {
#[test] #[test]
fn test_filter() { fn test_filter() {
let mut patterns = HashSet::new(); let mut patterns = BTreeSet::new();
patterns.insert(hex!("000000")); patterns.insert(hex!("000000"));
patterns.insert(hex!("111111")); patterns.insert(hex!("111111"));

View File

@ -24,8 +24,7 @@
//! deserialization. //! deserialization.
//! * `secp-lowmemory` - optimizations for low-memory devices. //! * `secp-lowmemory` - optimizations for low-memory devices.
//! * `no-std` - enables additional features required for this crate to be usable //! * `no-std` - enables additional features required for this crate to be usable
//! without std. Does **not** disable `std`. Depends on `hashbrown` //! without std. Does **not** disable `std`. Depends on `core2`.
//! and `core2`.
//! * `bitcoinconsensus-std` - enables `std` in `bitcoinconsensus` and communicates it //! * `bitcoinconsensus-std` - enables `std` in `bitcoinconsensus` and communicates it
//! to this crate so it knows how to implement //! to this crate so it knows how to implement
//! `std::error::Error`. At this time there's a hack to //! `std::error::Error`. At this time there's a hack to
@ -182,12 +181,6 @@ mod prelude {
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
pub use crate::io_extras::sink; pub use crate::io_extras::sink;
#[cfg(feature = "hashbrown")]
pub use hashbrown::HashSet;
#[cfg(not(feature = "hashbrown"))]
pub use std::collections::HashSet;
pub use bitcoin_internals::hex::display::DisplayHex; pub use bitcoin_internals::hex::display::DisplayHex;
} }