@@ -6,15 +6,15 @@ use crate::{
6
6
use crate :: { EdgeMutation , EdgeMutationWithValues } ;
7
7
8
8
use log:: info;
9
- use std:: sync:: { Mutex , MutexGuard } ;
9
+ use std:: sync:: { RwLock , RwLockReadGuard } ;
10
10
use std:: time:: Instant ;
11
11
12
12
use crate :: graph:: csr:: NodeValues ;
13
13
use rayon:: prelude:: * ;
14
14
15
15
#[ derive( Debug ) ]
16
16
pub struct AdjacencyList < NI , EV > {
17
- edges : Vec < Mutex < Vec < Target < NI , EV > > > > ,
17
+ edges : Vec < RwLock < Vec < Target < NI , EV > > > > ,
18
18
layout : CsrLayout ,
19
19
}
20
20
@@ -32,7 +32,7 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
32
32
}
33
33
34
34
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 :: < _ > ( ) ;
36
36
Self { edges, layout }
37
37
}
38
38
@@ -47,17 +47,17 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
47
47
NI : Send + Sync ,
48
48
EV : Send + Sync ,
49
49
{
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 ( ) )
51
51
}
52
52
53
53
#[ inline]
54
54
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 ( ) )
56
56
}
57
57
58
58
#[ inline]
59
59
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 ( ) ;
61
61
62
62
match self . layout {
63
63
CsrLayout :: Sorted => match edges. binary_search ( & target) {
@@ -75,7 +75,7 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
75
75
76
76
#[ derive( Debug ) ]
77
77
pub struct Targets < ' slice , NI : Idx > {
78
- targets : MutexGuard < ' slice , Vec < Target < NI , ( ) > > > ,
78
+ targets : RwLockReadGuard < ' slice , Vec < Target < NI , ( ) > > > ,
79
79
}
80
80
81
81
impl < ' slice , NI : Idx > Targets < ' slice , NI > {
@@ -132,15 +132,15 @@ impl<'slice, NI: Idx> Iterator for TargetsIter<'slice, NI> {
132
132
impl < NI : Idx > AdjacencyList < NI , ( ) > {
133
133
#[ inline]
134
134
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 ( ) ;
136
136
137
137
Targets { targets }
138
138
}
139
139
}
140
140
141
141
#[ derive( Debug ) ]
142
142
pub struct TargetsWithValues < ' slice , NI : Idx , EV > {
143
- targets : MutexGuard < ' slice , Vec < Target < NI , EV > > > ,
143
+ targets : RwLockReadGuard < ' slice , Vec < Target < NI , EV > > > ,
144
144
}
145
145
146
146
impl < ' slice , NI : Idx , EV > TargetsWithValues < ' slice , NI , EV > {
@@ -188,7 +188,7 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
188
188
#[ inline]
189
189
pub ( crate ) fn targets_with_values ( & self , node : NI ) -> TargetsWithValues < ' _ , NI , EV > {
190
190
TargetsWithValues {
191
- targets : self . edges [ node. index ( ) ] . lock ( ) . unwrap ( ) ,
191
+ targets : self . edges [ node. index ( ) ] . read ( ) . unwrap ( ) ,
192
192
}
193
193
}
194
194
}
@@ -206,21 +206,21 @@ where
206
206
let thread_safe_vec = edge_list
207
207
. degrees ( node_count, direction)
208
208
. 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 ( ) ) ) )
210
210
. collect :: < Vec < _ > > ( ) ;
211
211
info ! ( "Initialized adjacency list in {:?}" , start. elapsed( ) ) ;
212
212
213
213
let start = Instant :: now ( ) ;
214
214
edge_list. edges ( ) . for_each ( |( s, t, v) | {
215
215
if matches ! ( direction, Direction :: Outgoing | Direction :: Undirected ) {
216
216
thread_safe_vec[ s. index ( ) ]
217
- . lock ( )
217
+ . write ( )
218
218
. unwrap ( )
219
219
. push ( Target :: new ( t, v) ) ;
220
220
}
221
221
if matches ! ( direction, Direction :: Incoming | Direction :: Undirected ) {
222
222
thread_safe_vec[ t. index ( ) ]
223
- . lock ( )
223
+ . write ( )
224
224
. unwrap ( )
225
225
. push ( Target :: new ( s, v) ) ;
226
226
}
0 commit comments