Skip to content

Commit 696cc25

Browse files
committed
[DK] Remove unnecessary locks in protocol level
1 parent b9c063a commit 696cc25

1 file changed

Lines changed: 2 additions & 60 deletions

File tree

‎consensus/src/processes/dagknight/protocol.rs‎

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
use std::{
2-
cell::Cell,
3-
collections::HashMap,
4-
sync::{Arc, OnceLock},
5-
};
1+
use std::{cell::Cell, collections::HashMap, sync::Arc};
62

7-
use dashmap::DashMap;
83
use itertools::Itertools;
94
use kaspa_consensus_core::{BlockHashMap, BlockHashSet, HashMapCustomHasher, KType};
10-
use kaspa_core::{debug, trace};
5+
use kaspa_core::debug;
116
use kaspa_hashes::Hash;
127
use kaspa_math::Uint192;
138
use parking_lot::RwLock;
@@ -87,51 +82,6 @@ use crate::{
8782
3. switch GD/k-coloring to committed coloring
8883
*/
8984

90-
/// TODO[DK]: If writes here are moved out or batched, revisit this
91-
/// Global lock map for conflict genesis level locking
92-
/// Maps conflict genesis hashes to their respective locks
93-
static CONFLICT_LOCKS: OnceLock<DashMap<Hash, Arc<RwLock<()>>>> = OnceLock::new();
94-
95-
fn get_conflict_locks() -> &'static DashMap<Hash, Arc<RwLock<()>>> {
96-
CONFLICT_LOCKS.get_or_init(DashMap::new)
97-
}
98-
99-
/// Cleans up unused locks from the global lock map.
100-
/// A lock is considered unused if its Arc strong count is 1 (only the map holds a reference).
101-
/// This should be called periodically or opportunistically to prevent unbounded growth.
102-
pub fn cleanup_conflict_locks() {
103-
let locks = get_conflict_locks();
104-
let mut to_remove = Vec::new();
105-
106-
// First pass: identify locks with no external references
107-
for entry in locks.iter() {
108-
let hash = *entry.key();
109-
let arc = entry.value();
110-
// If strong count is 1, only the DashMap holds a reference
111-
if Arc::strong_count(arc) == 1 {
112-
to_remove.push(hash);
113-
}
114-
}
115-
116-
// Second pass: remove identified locks
117-
// Note: by the time we remove, another thread might have acquired the lock,
118-
// so we double-check the count before removing
119-
let mut removed_count = 0;
120-
for hash in to_remove {
121-
if let Some(entry) = locks.get(&hash)
122-
&& Arc::strong_count(entry.value()) == 1
123-
{
124-
drop(entry); // Release the reference before removing
125-
locks.remove(&hash);
126-
removed_count += 1;
127-
}
128-
}
129-
130-
if removed_count > 0 {
131-
trace!("Cleaned up {} unused conflict locks, {} remaining", removed_count, locks.len());
132-
}
133-
}
134-
13585
/// A struct encapsulating the logic and algorithms of the DAGKNIGHT protocol
13686
#[derive(Clone)]
13787
pub struct DagknightExecutor<
@@ -261,9 +211,6 @@ impl<
261211

262212
debug!("dk::sp: {} | conflict_ordered_parents: {:?}", curr_subgroup[0], conflict_ordered_parents);
263213

264-
// Opportunistically cleanup unused locks after processing
265-
cleanup_conflict_locks();
266-
267214
DagknightData { selected_parent: curr_subgroup[0], conflict_ordered_parents }
268215
}
269216

@@ -505,11 +452,6 @@ impl<
505452
reachability_service.clone(),
506453
);
507454

508-
// Acquire a lock for this conflict_genesis to prevent concurrent writes
509-
let locks = get_conflict_locks();
510-
let lock_arc = locks.entry(conflict_genesis).or_insert_with(|| Arc::new(RwLock::new(()))).clone();
511-
let _lock = lock_arc.write();
512-
513455
conflict_zone_manager.fill_zone_data(all_tips);
514456

515457
// selected a parent in this subgroup => Conditioned upon virtual agreeing with this subgroup

0 commit comments

Comments
 (0)