@@ -193,8 +193,8 @@ impl Graph {
193
193
}
194
194
195
195
pub fn add_edge ( & mut self , source : ( AudioNodeId , usize ) , dest : ( AudioNodeId , usize ) ) {
196
- self . nodes [ source . 0 ]
197
- . get_mut ( )
196
+ self . nodes
197
+ . get_unchecked_mut ( source . 0 )
198
198
. outgoing_edges
199
199
. push ( OutgoingEdge {
200
200
self_index : source. 1 ,
@@ -206,8 +206,8 @@ impl Graph {
206
206
}
207
207
208
208
pub fn remove_edge ( & mut self , source : AudioNodeId , dest : AudioNodeId ) {
209
- self . nodes [ source ]
210
- . get_mut ( )
209
+ self . nodes
210
+ . get_unchecked_mut ( source )
211
211
. outgoing_edges
212
212
. retain ( |edge| edge. other_id != dest) ;
213
213
@@ -216,7 +216,7 @@ impl Graph {
216
216
217
217
pub fn remove_edges_from ( & mut self , source : AudioNodeId ) {
218
218
// Remove outgoing edges
219
- self . nodes [ source ] . get_mut ( ) . outgoing_edges . clear ( ) ;
219
+ self . nodes . get_unchecked_mut ( source ) . outgoing_edges . clear ( ) ;
220
220
221
221
// Remove incoming edges - we need to traverse all nodes
222
222
self . nodes . values_mut ( ) . for_each ( |node| {
@@ -241,21 +241,27 @@ impl Graph {
241
241
}
242
242
243
243
pub fn mark_cycle_breaker ( & mut self , index : AudioNodeId ) {
244
- self . nodes [ index ] . get_mut ( ) . cycle_breaker = true ;
244
+ self . nodes . get_unchecked_mut ( index ) . cycle_breaker = true ;
245
245
}
246
246
247
247
pub fn set_channel_count ( & mut self , index : AudioNodeId , v : usize ) {
248
- self . nodes [ index ] . get_mut ( ) . channel_config . count = v;
248
+ self . nodes . get_unchecked_mut ( index ) . channel_config . count = v;
249
249
}
250
250
pub fn set_channel_count_mode ( & mut self , index : AudioNodeId , v : ChannelCountMode ) {
251
- self . nodes [ index] . get_mut ( ) . channel_config . count_mode = v;
251
+ self . nodes
252
+ . get_unchecked_mut ( index)
253
+ . channel_config
254
+ . count_mode = v;
252
255
}
253
256
pub fn set_channel_interpretation ( & mut self , index : AudioNodeId , v : ChannelInterpretation ) {
254
- self . nodes [ index] . get_mut ( ) . channel_config . interpretation = v;
257
+ self . nodes
258
+ . get_unchecked_mut ( index)
259
+ . channel_config
260
+ . interpretation = v;
255
261
}
256
262
257
263
pub fn route_message ( & mut self , index : AudioNodeId , msg : & mut dyn Any ) {
258
- self . nodes [ index ] . get_mut ( ) . processor . onmessage ( msg) ;
264
+ self . nodes . get_unchecked_mut ( index ) . processor . onmessage ( msg) ;
259
265
}
260
266
261
267
/// Helper function for `order_nodes` - traverse node and outgoing edges
@@ -278,7 +284,7 @@ impl Graph {
278
284
let cycle_breaker_node = marked_temp
279
285
. iter ( )
280
286
. skip ( pos)
281
- . find ( |& & node_id| self . nodes [ node_id] . borrow ( ) . cycle_breaker ) ;
287
+ . find ( |& & node_id| self . nodes . get_unchecked ( node_id) . borrow ( ) . cycle_breaker ) ;
282
288
283
289
match cycle_breaker_node {
284
290
Some ( & node_id) => {
@@ -307,7 +313,13 @@ impl Graph {
307
313
marked_temp. push ( node_id) ;
308
314
309
315
// Visit outgoing nodes, and call `visit` on them recursively
310
- for edge in self . nodes [ node_id] . borrow ( ) . outgoing_edges . iter ( ) {
316
+ for edge in self
317
+ . nodes
318
+ . get_unchecked ( node_id)
319
+ . borrow ( )
320
+ . outgoing_edges
321
+ . iter ( )
322
+ {
311
323
let cycle_breaker_applied = self . visit (
312
324
edge. other_id ,
313
325
marked,
@@ -386,9 +398,11 @@ impl Graph {
386
398
387
399
if cycle_breaker_applied {
388
400
// clear the outgoing edges of the nodes that have been recognized as cycle breaker
389
- let nodes = & mut self . nodes ;
390
401
cycle_breakers. iter ( ) . for_each ( |node_id| {
391
- nodes[ * node_id] . get_mut ( ) . outgoing_edges . clear ( ) ;
402
+ self . nodes
403
+ . get_unchecked_mut ( * node_id)
404
+ . outgoing_edges
405
+ . clear ( ) ;
392
406
} ) ;
393
407
394
408
continue ;
@@ -423,16 +437,13 @@ impl Graph {
423
437
// keep track of end-of-lifecyle nodes
424
438
let mut nodes_dropped = false ;
425
439
426
- // for borrow-checker reasons, move mutable borrow of nodes out of self
427
- let nodes = & mut self . nodes ;
428
-
429
440
// process every node, in topological sorted order
430
441
self . ordered . iter ( ) . for_each ( |index| {
431
442
// acquire a mutable borrow of the current processing node
432
- let mut node = nodes[ * index] . borrow_mut ( ) ;
443
+ let mut node = self . nodes . get_unchecked ( * index) . borrow_mut ( ) ;
433
444
434
445
// let the current node process (catch any panics that may occur)
435
- let params = AudioParamValues :: from ( nodes) ;
446
+ let params = AudioParamValues :: from ( & self . nodes ) ;
436
447
scope. node_id . set ( * index) ;
437
448
let ( success, tail_time) = {
438
449
// We are abusing AssertUnwindSafe here, we cannot guarantee it upholds.
@@ -456,7 +467,7 @@ impl Graph {
456
467
// audio params are connected to the 'hidden' usize::MAX output, ignore them here
457
468
. filter ( |edge| edge. other_index != usize:: MAX )
458
469
. for_each ( |edge| {
459
- let mut output_node = nodes[ edge. other_id ] . borrow_mut ( ) ;
470
+ let mut output_node = self . nodes . get_unchecked ( edge. other_id ) . borrow_mut ( ) ;
460
471
output_node. has_inputs_connected = true ;
461
472
let signal = & node. outputs [ edge. self_index ] ;
462
473
let channel_config = & output_node. channel_config . clone ( ) ;
@@ -482,7 +493,7 @@ impl Graph {
482
493
// Check if we can decommission this node (end of life)
483
494
if can_free {
484
495
// Node is dropped, remove it from the node list
485
- let mut node = nodes. remove ( * index) . into_inner ( ) ;
496
+ let mut node = self . nodes . remove ( * index) . into_inner ( ) ;
486
497
self . reclaim_id_channel
487
498
. push ( node. reclaim_id . take ( ) . unwrap ( ) ) ;
488
499
drop ( node) ;
@@ -492,7 +503,7 @@ impl Graph {
492
503
493
504
// Nodes are only dropped when they do not have incoming connections.
494
505
// But they may have AudioParams feeding into them, these can de dropped too.
495
- nodes. retain ( |id, node| {
506
+ self . nodes . retain ( |id, node| {
496
507
let node = node. get_mut ( ) ; // unwrap the RefCell
497
508
498
509
// Check if this node was connected to the dropped node. In that case, it is
@@ -524,7 +535,7 @@ impl Graph {
524
535
if nodes_dropped {
525
536
let mut i = 0 ;
526
537
while i < self . ordered . len ( ) {
527
- if nodes. get ( self . ordered [ i] ) . is_none ( ) {
538
+ if ! self . nodes . contains ( self . ordered [ i] ) {
528
539
self . ordered . remove ( i) ;
529
540
} else {
530
541
i += 1 ;
@@ -533,7 +544,7 @@ impl Graph {
533
544
}
534
545
535
546
// Return the output buffer of destination node
536
- & self . nodes [ AudioNodeId ( 0 ) ] . get_mut ( ) . outputs [ 0 ]
547
+ & self . nodes . get_unchecked_mut ( AudioNodeId ( 0 ) ) . outputs [ 0 ]
537
548
}
538
549
}
539
550
@@ -735,7 +746,10 @@ mod tests {
735
746
// dropped and the AudioNodeId(3) should be reclaimed
736
747
add_node ( & mut graph, 2 , node. clone ( ) ) ;
737
748
// Mark the node as 'detached from the control thread', so it is allowed to drop
738
- graph. nodes [ AudioNodeId ( 2 ) ] . get_mut ( ) . control_handle_dropped = true ;
749
+ graph
750
+ . nodes
751
+ . get_unchecked_mut ( AudioNodeId ( 2 ) )
752
+ . control_handle_dropped = true ;
739
753
740
754
// Connect the regular node to the AudioDestinationNode
741
755
add_edge ( & mut graph, 2 , 0 ) ;
@@ -777,7 +791,10 @@ mod tests {
777
791
// dropped and the AudioNodeId(3) should be reclaimed
778
792
add_node ( & mut graph, 2 , node. clone ( ) ) ;
779
793
// Mark the node as 'detached from the control thread', so it is allowed to drop
780
- graph. nodes [ AudioNodeId ( 2 ) ] . get_mut ( ) . control_handle_dropped = true ;
794
+ graph
795
+ . nodes
796
+ . get_unchecked_mut ( AudioNodeId ( 2 ) )
797
+ . control_handle_dropped = true ;
781
798
782
799
// Connect the regular node to the AudioDestinationNode
783
800
add_edge ( & mut graph, 2 , 0 ) ;
@@ -786,7 +803,10 @@ mod tests {
786
803
let param = Box :: new ( TestNode { tail_time : true } ) ; // audio params have tail time true
787
804
add_node ( & mut graph, 3 , param) ;
788
805
// Mark the node as 'detached from the control thread', so it is allowed to drop
789
- graph. nodes [ AudioNodeId ( 3 ) ] . get_mut ( ) . control_handle_dropped = true ;
806
+ graph
807
+ . nodes
808
+ . get_unchecked_mut ( AudioNodeId ( 3 ) )
809
+ . control_handle_dropped = true ;
790
810
791
811
// Connect the audioparam to the regular node
792
812
add_audioparam ( & mut graph, 3 , 2 ) ;
@@ -828,7 +848,10 @@ mod tests {
828
848
add_node ( & mut graph, 2 , node) ;
829
849
830
850
// Mark the node as 'detached from the control thread', so it is allowed to drop
831
- graph. nodes [ AudioNodeId ( 2 ) ] . get_mut ( ) . control_handle_dropped = true ;
851
+ graph
852
+ . nodes
853
+ . get_unchecked_mut ( AudioNodeId ( 2 ) )
854
+ . control_handle_dropped = true ;
832
855
833
856
// Render a single quantum
834
857
let scope = AudioWorkletGlobalScope {
0 commit comments