73
73
//! aren't needed anymore.
74
74
75
75
use crate :: fx:: { FxHashMap , FxHashSet } ;
76
- use crate :: indexed_vec:: Idx ;
77
- use crate :: newtype_index;
78
76
79
77
use std:: cell:: { Cell , RefCell } ;
80
78
use std:: collections:: hash_map:: Entry ;
@@ -87,10 +85,6 @@ mod graphviz;
87
85
#[ cfg( test) ]
88
86
mod tests;
89
87
90
- newtype_index ! {
91
- pub struct NodeIndex { .. }
92
- }
93
-
94
88
pub trait ForestObligation : Clone + Debug {
95
89
type Predicate : Clone + hash:: Hash + Eq + Debug ;
96
90
@@ -143,9 +137,10 @@ pub struct ObligationForest<O: ForestObligation> {
143
137
/// At the end of processing, those nodes will be removed by a
144
138
/// call to `compress`.
145
139
///
146
- /// Ideally, this would be an `IndexVec<NodeIndex, Node<O>>`. But that is
147
- /// slower, because this vector is accessed so often that the
148
- /// `u32`-to-`usize` conversions required for accesses are significant.
140
+ /// `usize` indices are used here and throughout this module, rather than
141
+ /// `newtype_index!` indices, because this code is hot enough that the
142
+ /// `u32`-to-`usize` conversions that would be required are significant,
143
+ /// and space considerations are not important.
149
144
nodes : Vec < Node < O > > ,
150
145
151
146
/// A cache of predicates that have been successfully completed.
@@ -154,7 +149,7 @@ pub struct ObligationForest<O: ForestObligation> {
154
149
/// A cache of the nodes in `nodes`, indexed by predicate. Unfortunately,
155
150
/// its contents are not guaranteed to match those of `nodes`. See the
156
151
/// comments in `process_obligation` for details.
157
- waiting_cache : FxHashMap < O :: Predicate , NodeIndex > ,
152
+ waiting_cache : FxHashMap < O :: Predicate , usize > ,
158
153
159
154
/// A scratch vector reused in various operations, to avoid allocating new
160
155
/// vectors.
@@ -179,12 +174,12 @@ struct Node<O> {
179
174
180
175
/// Obligations that depend on this obligation for their completion. They
181
176
/// must all be in a non-pending state.
182
- dependents : Vec < NodeIndex > ,
177
+ dependents : Vec < usize > ,
183
178
184
179
/// If true, dependents[0] points to a "parent" node, which requires
185
180
/// special treatment upon error but is otherwise treated the same.
186
181
/// (It would be more idiomatic to store the parent node in a separate
187
- /// `Option<NodeIndex >` field, but that slows down the common case of
182
+ /// `Option<usize >` field, but that slows down the common case of
188
183
/// iterating over the parent and other descendants together.)
189
184
has_parent : bool ,
190
185
@@ -194,7 +189,7 @@ struct Node<O> {
194
189
195
190
impl < O > Node < O > {
196
191
fn new (
197
- parent : Option < NodeIndex > ,
192
+ parent : Option < usize > ,
198
193
obligation : O ,
199
194
obligation_tree_id : ObligationTreeId
200
195
) -> Node < O > {
@@ -303,9 +298,7 @@ impl<O: ForestObligation> ObligationForest<O> {
303
298
}
304
299
305
300
// Returns Err(()) if we already know this obligation failed.
306
- fn register_obligation_at ( & mut self , obligation : O , parent : Option < NodeIndex > )
307
- -> Result < ( ) , ( ) >
308
- {
301
+ fn register_obligation_at ( & mut self , obligation : O , parent : Option < usize > ) -> Result < ( ) , ( ) > {
309
302
if self . done_cache . contains ( obligation. as_predicate ( ) ) {
310
303
return Ok ( ( ) ) ;
311
304
}
@@ -314,7 +307,7 @@ impl<O: ForestObligation> ObligationForest<O> {
314
307
Entry :: Occupied ( o) => {
315
308
debug ! ( "register_obligation_at({:?}, {:?}) - duplicate of {:?}!" ,
316
309
obligation, parent, o. get( ) ) ;
317
- let node = & mut self . nodes [ o. get ( ) . index ( ) ] ;
310
+ let node = & mut self . nodes [ * o. get ( ) ] ;
318
311
if let Some ( parent_index) = parent {
319
312
// If the node is already in `waiting_cache`, it has
320
313
// already had its chance to be marked with a parent. So if
@@ -335,10 +328,8 @@ impl<O: ForestObligation> ObligationForest<O> {
335
328
obligation, parent, self . nodes. len( ) ) ;
336
329
337
330
let obligation_tree_id = match parent {
338
- Some ( parent_index) => {
339
- self . nodes [ parent_index. index ( ) ] . obligation_tree_id
340
- }
341
- None => self . obligation_tree_id_generator . next ( ) . unwrap ( )
331
+ Some ( parent_index) => self . nodes [ parent_index] . obligation_tree_id ,
332
+ None => self . obligation_tree_id_generator . next ( ) . unwrap ( ) ,
342
333
} ;
343
334
344
335
let already_failed =
@@ -351,7 +342,7 @@ impl<O: ForestObligation> ObligationForest<O> {
351
342
if already_failed {
352
343
Err ( ( ) )
353
344
} else {
354
- v. insert ( NodeIndex :: new ( self . nodes . len ( ) ) ) ;
345
+ v. insert ( self . nodes . len ( ) ) ;
355
346
self . nodes . push ( Node :: new ( parent, obligation, obligation_tree_id) ) ;
356
347
Ok ( ( ) )
357
348
}
@@ -437,7 +428,7 @@ impl<O: ForestObligation> ObligationForest<O> {
437
428
for child in children {
438
429
let st = self . register_obligation_at (
439
430
child,
440
- Some ( NodeIndex :: new ( i ) )
431
+ Some ( i )
441
432
) ;
442
433
if let Err ( ( ) ) = st {
443
434
// Error already reported - propagate it
@@ -522,8 +513,8 @@ impl<O: ForestObligation> ObligationForest<O> {
522
513
NodeState :: Success => {
523
514
node. state . set ( NodeState :: OnDfsStack ) ;
524
515
stack. push ( i) ;
525
- for index in node. dependents . iter ( ) {
526
- self . find_cycles_from_node ( stack, processor, index. index ( ) ) ;
516
+ for & index in node. dependents . iter ( ) {
517
+ self . find_cycles_from_node ( stack, processor, index) ;
527
518
}
528
519
stack. pop ( ) ;
529
520
node. state . set ( NodeState :: Done ) ;
@@ -551,11 +542,11 @@ impl<O: ForestObligation> ObligationForest<O> {
551
542
if node. has_parent {
552
543
// The first dependent is the parent, which is treated
553
544
// specially.
554
- error_stack. extend ( node. dependents . iter ( ) . skip ( 1 ) . map ( |index| index . index ( ) ) ) ;
555
- i = node. dependents [ 0 ] . index ( ) ;
545
+ error_stack. extend ( node. dependents . iter ( ) . skip ( 1 ) ) ;
546
+ i = node. dependents [ 0 ] ;
556
547
} else {
557
548
// No parent; treat all dependents non-specially.
558
- error_stack. extend ( node. dependents . iter ( ) . map ( |index| index . index ( ) ) ) ;
549
+ error_stack. extend ( node. dependents . iter ( ) ) ;
559
550
break ;
560
551
}
561
552
}
@@ -567,7 +558,7 @@ impl<O: ForestObligation> ObligationForest<O> {
567
558
_ => node. state . set ( NodeState :: Error ) ,
568
559
}
569
560
570
- error_stack. extend ( node. dependents . iter ( ) . map ( |index| index . index ( ) ) ) ;
561
+ error_stack. extend ( node. dependents . iter ( ) ) ;
571
562
}
572
563
573
564
self . scratch . replace ( error_stack) ;
@@ -577,8 +568,8 @@ impl<O: ForestObligation> ObligationForest<O> {
577
568
// This always-inlined function is for the hot call site.
578
569
#[ inline( always) ]
579
570
fn inlined_mark_neighbors_as_waiting_from ( & self , node : & Node < O > ) {
580
- for dependent in node. dependents . iter ( ) {
581
- self . mark_as_waiting_from ( & self . nodes [ dependent. index ( ) ] ) ;
571
+ for & dependent in node. dependents . iter ( ) {
572
+ self . mark_as_waiting_from ( & self . nodes [ dependent] ) ;
582
573
}
583
574
}
584
575
@@ -708,15 +699,15 @@ impl<O: ForestObligation> ObligationForest<O> {
708
699
for node in & mut self . nodes {
709
700
let mut i = 0 ;
710
701
while i < node. dependents . len ( ) {
711
- let new_i = node_rewrites[ node. dependents [ i] . index ( ) ] ;
702
+ let new_i = node_rewrites[ node. dependents [ i] ] ;
712
703
if new_i >= nodes_len {
713
704
node. dependents . swap_remove ( i) ;
714
705
if i == 0 && node. has_parent {
715
706
// We just removed the parent.
716
707
node. has_parent = false ;
717
708
}
718
709
} else {
719
- node. dependents [ i] = NodeIndex :: new ( new_i) ;
710
+ node. dependents [ i] = new_i;
720
711
i += 1 ;
721
712
}
722
713
}
@@ -725,11 +716,11 @@ impl<O: ForestObligation> ObligationForest<O> {
725
716
// This updating of `self.waiting_cache` is necessary because the
726
717
// removal of nodes within `compress` can fail. See above.
727
718
self . waiting_cache . retain ( |_predicate, index| {
728
- let new_i = node_rewrites[ index. index ( ) ] ;
719
+ let new_i = node_rewrites[ * index] ;
729
720
if new_i >= nodes_len {
730
721
false
731
722
} else {
732
- * index = NodeIndex :: new ( new_i) ;
723
+ * index = new_i;
733
724
true
734
725
}
735
726
} ) ;
0 commit comments