From 5d48ae1eec8fa4dda1cbd1194e0ecebc9a0373fb Mon Sep 17 00:00:00 2001 From: junderw Date: Sun, 25 Jun 2023 19:04:01 -0700 Subject: [PATCH] Use U32HasherState for HashSet --- backend/rust-gbt/src/audit_transaction.rs | 15 +++++++++------ backend/rust-gbt/src/gbt.rs | 10 ++++++---- backend/rust-gbt/src/u32_hasher_types.rs | 8 +++++++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/backend/rust-gbt/src/audit_transaction.rs b/backend/rust-gbt/src/audit_transaction.rs index 0867a317b..79697a150 100644 --- a/backend/rust-gbt/src/audit_transaction.rs +++ b/backend/rust-gbt/src/audit_transaction.rs @@ -1,4 +1,7 @@ -use crate::thread_transaction::ThreadTransaction; +use crate::{ + thread_transaction::ThreadTransaction, + u32_hasher_types::{u32hashset_new, U32HasherState}, +}; use std::{ cmp::Ordering, collections::HashSet, @@ -17,8 +20,8 @@ pub struct AuditTransaction { pub dependency_rate: f64, pub inputs: Vec, pub relatives_set_flag: bool, - pub ancestors: HashSet, - pub children: HashSet, + pub ancestors: HashSet, + pub children: HashSet, pub ancestor_fee: u64, pub ancestor_weight: u32, pub ancestor_sigops: u32, @@ -76,8 +79,8 @@ impl AuditTransaction { dependency_rate: f64::INFINITY, inputs: tx.inputs.clone(), relatives_set_flag: false, - ancestors: HashSet::new(), - children: HashSet::new(), + ancestors: u32hashset_new(), + children: u32hashset_new(), ancestor_fee: tx.fee, ancestor_weight: tx.weight, ancestor_sigops: tx.sigops, @@ -107,7 +110,7 @@ impl AuditTransaction { #[inline] pub fn set_ancestors( &mut self, - ancestors: HashSet, + ancestors: HashSet, total_fee: u64, total_weight: u32, total_sigops: u32, diff --git a/backend/rust-gbt/src/gbt.rs b/backend/rust-gbt/src/gbt.rs index 46b763e2c..faaf27e4e 100644 --- a/backend/rust-gbt/src/gbt.rs +++ b/backend/rust-gbt/src/gbt.rs @@ -6,7 +6,9 @@ use std::{ use crate::{ audit_transaction::AuditTransaction, - u32_hasher_types::{u32hashmap_with_capacity, u32priority_queue_with_capacity, U32HasherState}, + u32_hasher_types::{ + u32hashmap_with_capacity, u32hashset_new, u32priority_queue_with_capacity, U32HasherState, + }, GbtResult, ThreadTransactionsMap, STARTING_CAPACITY, }; @@ -217,7 +219,7 @@ pub fn gbt(mempool: &mut ThreadTransactionsMap) -> Option { } fn set_relatives(txid: u32, audit_pool: &mut AuditPool) { - let mut parents: HashSet = HashSet::new(); + let mut parents: HashSet = u32hashset_new(); if let Some(tx) = audit_pool.get(&txid) { if tx.relatives_set_flag { return; @@ -229,7 +231,7 @@ fn set_relatives(txid: u32, audit_pool: &mut AuditPool) { return; } - let mut ancestors: HashSet = HashSet::new(); + let mut ancestors: HashSet = u32hashset_new(); for parent_id in &parents { set_relatives(*parent_id, audit_pool); @@ -268,7 +270,7 @@ fn update_descendants( modified: &mut ModifiedQueue, cluster_rate: f64, ) { - let mut visited: HashSet = HashSet::new(); + let mut visited: HashSet = u32hashset_new(); let mut descendant_stack: Vec = Vec::new(); let root_fee: u64; let root_weight: u32; diff --git a/backend/rust-gbt/src/u32_hasher_types.rs b/backend/rust-gbt/src/u32_hasher_types.rs index 4735844f3..1995bcad2 100644 --- a/backend/rust-gbt/src/u32_hasher_types.rs +++ b/backend/rust-gbt/src/u32_hasher_types.rs @@ -1,6 +1,6 @@ use priority_queue::PriorityQueue; use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, hash::{BuildHasher, Hasher}, }; @@ -16,7 +16,13 @@ pub fn u32priority_queue_with_capacity( PriorityQueue::with_capacity_and_hasher(capacity, U32HasherState(())) } +/// This is the only way to create a `HashSet` with the `U32HasherState` +pub fn u32hashset_new() -> HashSet { + HashSet::with_hasher(U32HasherState(())) +} + /// A private unit type is contained so no one can make an instance of it. +#[derive(Clone)] pub struct U32HasherState(()); impl BuildHasher for U32HasherState {