Skip to content

Commit 39b5165

Browse files
authored
Replace Mutex with RwLock in AdjacencyList (#120)
1 parent c0f0639 commit 39b5165

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

crates/builder/src/graph/adj_list.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use crate::{
66
use crate::{EdgeMutation, EdgeMutationWithValues};
77

88
use log::info;
9-
use std::sync::{Mutex, MutexGuard};
9+
use std::sync::{RwLock, RwLockReadGuard};
1010
use std::time::Instant;
1111

1212
use crate::graph::csr::NodeValues;
1313
use rayon::prelude::*;
1414

1515
#[derive(Debug)]
1616
pub struct AdjacencyList<NI, EV> {
17-
edges: Vec<Mutex<Vec<Target<NI, EV>>>>,
17+
edges: Vec<RwLock<Vec<Target<NI, EV>>>>,
1818
layout: CsrLayout,
1919
}
2020

@@ -32,7 +32,7 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
3232
}
3333

3434
pub fn with_layout(edges: Vec<Vec<Target<NI, EV>>>, layout: CsrLayout) -> Self {
35-
let edges = edges.into_iter().map(Mutex::new).collect::<_>();
35+
let edges = edges.into_iter().map(RwLock::new).collect::<_>();
3636
Self { edges, layout }
3737
}
3838

@@ -47,17 +47,17 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
4747
NI: Send + Sync,
4848
EV: Send + Sync,
4949
{
50-
NI::new(self.edges.par_iter().map(|v| v.lock().unwrap().len()).sum())
50+
NI::new(self.edges.par_iter().map(|v| v.read().unwrap().len()).sum())
5151
}
5252

5353
#[inline]
5454
pub(crate) fn degree(&self, node: NI) -> NI {
55-
NI::new(self.edges[node.index()].lock().unwrap().len())
55+
NI::new(self.edges[node.index()].read().unwrap().len())
5656
}
5757

5858
#[inline]
5959
pub(crate) fn insert(&self, source: NI, target: Target<NI, EV>) {
60-
let mut edges = self.edges[source.index()].lock().unwrap();
60+
let mut edges = self.edges[source.index()].write().unwrap();
6161

6262
match self.layout {
6363
CsrLayout::Sorted => match edges.binary_search(&target) {
@@ -75,7 +75,7 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
7575

7676
#[derive(Debug)]
7777
pub struct Targets<'slice, NI: Idx> {
78-
targets: MutexGuard<'slice, Vec<Target<NI, ()>>>,
78+
targets: RwLockReadGuard<'slice, Vec<Target<NI, ()>>>,
7979
}
8080

8181
impl<'slice, NI: Idx> Targets<'slice, NI> {
@@ -132,15 +132,15 @@ impl<'slice, NI: Idx> Iterator for TargetsIter<'slice, NI> {
132132
impl<NI: Idx> AdjacencyList<NI, ()> {
133133
#[inline]
134134
pub(crate) fn targets(&self, node: NI) -> Targets<'_, NI> {
135-
let targets = self.edges[node.index()].lock().unwrap();
135+
let targets = self.edges[node.index()].read().unwrap();
136136

137137
Targets { targets }
138138
}
139139
}
140140

141141
#[derive(Debug)]
142142
pub struct TargetsWithValues<'slice, NI: Idx, EV> {
143-
targets: MutexGuard<'slice, Vec<Target<NI, EV>>>,
143+
targets: RwLockReadGuard<'slice, Vec<Target<NI, EV>>>,
144144
}
145145

146146
impl<'slice, NI: Idx, EV> TargetsWithValues<'slice, NI, EV> {
@@ -188,7 +188,7 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
188188
#[inline]
189189
pub(crate) fn targets_with_values(&self, node: NI) -> TargetsWithValues<'_, NI, EV> {
190190
TargetsWithValues {
191-
targets: self.edges[node.index()].lock().unwrap(),
191+
targets: self.edges[node.index()].read().unwrap(),
192192
}
193193
}
194194
}
@@ -206,21 +206,21 @@ where
206206
let thread_safe_vec = edge_list
207207
.degrees(node_count, direction)
208208
.into_par_iter()
209-
.map(|degree| Mutex::new(Vec::with_capacity(degree.into_inner().index())))
209+
.map(|degree| RwLock::new(Vec::with_capacity(degree.into_inner().index())))
210210
.collect::<Vec<_>>();
211211
info!("Initialized adjacency list in {:?}", start.elapsed());
212212

213213
let start = Instant::now();
214214
edge_list.edges().for_each(|(s, t, v)| {
215215
if matches!(direction, Direction::Outgoing | Direction::Undirected) {
216216
thread_safe_vec[s.index()]
217-
.lock()
217+
.write()
218218
.unwrap()
219219
.push(Target::new(t, v));
220220
}
221221
if matches!(direction, Direction::Incoming | Direction::Undirected) {
222222
thread_safe_vec[t.index()]
223-
.lock()
223+
.write()
224224
.unwrap()
225225
.push(Target::new(s, v));
226226
}

0 commit comments

Comments
 (0)