@@ -7,6 +7,7 @@ use rustc_data_structures::fx::FxHashMap;
77use rustc_index:: vec:: { Idx , IndexVec } ;
88use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
99use std:: convert:: TryInto ;
10+ use std:: ops:: Range ;
1011
1112#[ derive( Debug , PartialEq , Eq ) ]
1213pub enum DepNodeColor {
@@ -67,48 +68,75 @@ static_assert_size!(Option<DepNodeIndex>, 4);
6768static_assert_size ! ( Option <SerializedDepNodeIndex >, 4 ) ;
6869
6970#[ derive( Copy , Clone , Encodable , Decodable ) ]
70- struct ColorAndOffset ( u32 ) ;
71+ struct ColorAndOffset ( u32 , u32 ) ;
7172
7273impl std:: fmt:: Debug for ColorAndOffset {
7374 fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
7475 fmt. debug_struct ( "ColorAndOffset" )
7576 . field ( "color" , & self . color ( ) )
76- . field ( "offset" , & self . offset ( ) )
77+ . field ( "lifted" , & self . lifted ( ) )
78+ . field ( "range" , & self . range ( ) )
7779 . finish ( )
7880 }
7981}
8082
8183impl ColorAndOffset {
82- fn unknown ( offset : u32 ) -> ColorAndOffset {
83- debug_assert_eq ! ( offset & TAG_MASK , 0 ) ;
84- ColorAndOffset ( offset | TAG_UNKNOWN )
84+ fn unknown ( start : u32 , end : u32 ) -> ColorAndOffset {
85+ debug_assert_eq ! ( start & TAG_MASK , 0 ) ;
86+ debug_assert_eq ! ( end & TAG_MASK , 0 ) ;
87+ ColorAndOffset ( start | TAG_UNKNOWN , end | TAG_UNKNOWN )
8588 }
8689
87- fn new ( color : DepNodeColor , offset : u32 ) -> ColorAndOffset {
88- let offset: u32 = offset. try_into ( ) . unwrap ( ) ;
89- debug_assert_eq ! ( offset & TAG_MASK , 0 ) ;
90- ColorAndOffset ( offset | color. tag ( ) )
90+ #[ allow( dead_code) ]
91+ fn new ( color : DepNodeColor , range : Range < usize > ) -> ColorAndOffset {
92+ let start: u32 = range. start . try_into ( ) . unwrap ( ) ;
93+ let end: u32 = range. end . try_into ( ) . unwrap ( ) ;
94+ debug_assert_eq ! ( start & TAG_MASK , 0 ) ;
95+ debug_assert_eq ! ( end & TAG_MASK , 0 ) ;
96+ ColorAndOffset ( start | color. tag ( ) , end | TAG_UNKNOWN )
97+ }
98+
99+ fn new_lifted ( color : DepNodeColor , range : Range < usize > ) -> ColorAndOffset {
100+ let start: u32 = range. start . try_into ( ) . unwrap ( ) ;
101+ let end: u32 = range. end . try_into ( ) . unwrap ( ) ;
102+ debug_assert_eq ! ( start & TAG_MASK , 0 ) ;
103+ debug_assert_eq ! ( end & TAG_MASK , 0 ) ;
104+ ColorAndOffset ( start | color. tag ( ) , end | TAG_GREEN )
91105 }
92106
93107 fn set_color ( & mut self , color : DepNodeColor ) {
94108 let offset = self . 0 & OFFSET_MASK ;
95109 self . 0 = color. tag ( ) | offset;
96110 }
97111
98- fn offset ( self ) -> u32 {
99- self . 0 & OFFSET_MASK
100- }
101-
102112 fn color ( self ) -> Option < DepNodeColor > {
103113 let tag = self . 0 & TAG_MASK ;
104114 match tag {
105115 TAG_NEW => Some ( DepNodeColor :: New ) ,
106116 TAG_RED => Some ( DepNodeColor :: Red ) ,
107117 TAG_GREEN => Some ( DepNodeColor :: Green ) ,
108118 TAG_UNKNOWN => None ,
109- _ => panic ! ( ) ,
119+ _ => unsafe { std:: hint:: unreachable_unchecked ( ) } ,
120+ }
121+ }
122+
123+ fn lifted ( self ) -> bool {
124+ let tag = self . 1 & TAG_MASK ;
125+ match tag {
126+ TAG_UNKNOWN => false ,
127+ _ => true ,
110128 }
111129 }
130+
131+ fn range ( self ) -> Range < usize > {
132+ let start = ( self . 0 & OFFSET_MASK ) as usize ;
133+ let end = ( self . 1 & OFFSET_MASK ) as usize ;
134+ start..end
135+ }
136+ }
137+
138+ fn shrink_range ( range : Range < usize > ) -> Range < u32 > {
139+ range. start . try_into ( ) . unwrap ( ) ..range. end . try_into ( ) . unwrap ( )
112140}
113141
114142/// Data for use when recompiling the **previous crate**.
@@ -124,7 +152,7 @@ pub struct SerializedDepGraph<K: DepKind> {
124152 /// For each DepNode, stores the list of edges originating from that
125153 /// DepNode. Encoded as a [start, end) pair indexing into edge_list_data,
126154 /// which holds the actual DepNodeIndices of the target nodes.
127- edge_list_indices : IndexVec < SerializedDepNodeIndex , ( ColorAndOffset , u32 ) > ,
155+ edge_list_indices : IndexVec < SerializedDepNodeIndex , ColorAndOffset > ,
128156 /// A flattened list of all edge targets in the graph. Edge sources are
129157 /// implicit in edge_list_indices.
130158 edge_list_data : Vec < DepNodeIndex > ,
@@ -143,7 +171,7 @@ pub struct CurrentDepGraph<K: DepKind> {
143171 /// For each DepNode, stores the list of edges originating from that
144172 /// DepNode. Encoded as a [start, end) pair indexing into edge_list_data,
145173 /// which holds the actual DepNodeIndices of the target nodes.
146- edge_list_indices : IndexVec < SplitIndex , ( u32 , u32 ) > ,
174+ edge_list_indices : IndexVec < SplitIndex , Range < u32 > > ,
147175 /// A flattened list of all edge targets in the graph. Edge sources are
148176 /// implicit in edge_list_indices.
149177 edge_list_data : Vec < DepNodeIndex > ,
@@ -194,20 +222,20 @@ impl<K: DepKind> CurrentDepGraph<K> {
194222 debug ! ( "intern_new: {:?} {:?}" , self . from_split( index) , node) ;
195223 let _index = self . fingerprints . push ( fingerprint) ;
196224 debug_assert_eq ! ( index, _index) ;
197- let ( start , end ) = self . insert_deps ( deps) ;
198- let _index = self . edge_list_indices . push ( ( start , end ) ) ;
225+ let range = self . insert_deps ( deps) ;
226+ let _index = self . edge_list_indices . push ( shrink_range ( range ) ) ;
199227 debug_assert_eq ! ( index, _index) ;
200228 let index = self . from_split ( index) ;
201229 let _o = self . index . insert ( node, index) ;
202230 debug_assert_eq ! ( _o, None ) ;
203231 index
204232 }
205233
206- fn insert_deps ( & mut self , deps : & [ DepNodeIndex ] ) -> ( u32 , u32 ) {
234+ fn insert_deps ( & mut self , deps : & [ DepNodeIndex ] ) -> Range < usize > {
207235 let start = self . edge_list_data . len ( ) ;
208236 self . edge_list_data . extend ( deps. iter ( ) . copied ( ) ) ;
209237 let end = self . edge_list_data . len ( ) ;
210- ( start. try_into ( ) . unwrap ( ) , end. try_into ( ) . unwrap ( ) )
238+ start.. end
211239 }
212240
213241 fn update_deps (
@@ -216,19 +244,18 @@ impl<K: DepKind> CurrentDepGraph<K> {
216244 color : DepNodeColor ,
217245 deps : & [ DepNodeIndex ] ,
218246 ) {
219- let ( start, _) = self . serialized . edge_list_indices [ index] ;
220- debug_assert_eq ! ( start. color( ) , None ) ;
221- let ( start, end) = self . insert_deps ( deps) ;
222- let len = self . serialized . edge_list_data . len ( ) as u32 ;
223247 debug ! ( "intern_color: {:?} => {:?}" , index, color) ;
224- let start = ColorAndOffset :: new ( color, start + len) ;
225- self . serialized . edge_list_indices [ index] = ( start, end + len) ;
248+ let range = self . serialized . edge_list_indices [ index] ;
249+ debug_assert_eq ! ( range. color( ) , None ) ;
250+ let range = self . insert_deps ( deps) ;
251+ let range = ColorAndOffset :: new_lifted ( color, range) ;
252+ self . serialized . edge_list_indices [ index] = range;
226253 }
227254
228255 pub ( crate ) fn intern_dark_green_node ( & mut self , index : SerializedDepNodeIndex ) -> DepNodeIndex {
229256 debug ! ( "intern_drak_green: {:?}" , index) ;
230- debug_assert_eq ! ( self . serialized. edge_list_indices[ index] . 0 . color( ) , None ) ;
231- self . serialized . edge_list_indices [ index] . 0 . set_color ( DepNodeColor :: Green ) ;
257+ debug_assert_eq ! ( self . serialized. edge_list_indices[ index] . color( ) , None ) ;
258+ self . serialized . edge_list_indices [ index] . set_color ( DepNodeColor :: Green ) ;
232259 debug ! ( "intern_color: {:?} => Green" , index) ;
233260 index. rejuvenate ( )
234261 }
@@ -329,22 +356,19 @@ impl<K: DepKind> CurrentDepGraph<K> {
329356
330357 #[ inline]
331358 fn serialized_edges ( & self , source : SerializedDepNodeIndex ) -> & [ DepNodeIndex ] {
332- let ( start, end) = self . serialized . edge_list_indices [ source] ;
333- let start = start. offset ( ) as usize ;
334- let end = end as usize ;
335- let len = self . serialized . edge_list_data . len ( ) ;
336- if start < len {
337- & self . serialized . edge_list_data [ start..end]
359+ let range = self . serialized . edge_list_indices [ source] ;
360+ if range. lifted ( ) {
361+ & self . edge_list_data [ range. range ( ) ]
338362 } else {
339- & self . edge_list_data [ start - len..end - len ]
363+ & self . serialized . edge_list_data [ range . range ( ) ]
340364 }
341365 }
342366
343367 #[ inline]
344368 fn new_edges ( & self , source : SplitIndex ) -> & [ DepNodeIndex ] {
345- let ( start , end ) = self . edge_list_indices [ source] ;
346- let start = start as usize ;
347- let end = end as usize ;
369+ let range = & self . edge_list_indices [ source] ;
370+ let start = range . start as usize ;
371+ let end = range . end as usize ;
348372 & self . edge_list_data [ start..end]
349373 }
350374
@@ -353,15 +377,14 @@ impl<K: DepKind> CurrentDepGraph<K> {
353377 & self ,
354378 source : SerializedDepNodeIndex ,
355379 ) -> Result < DepNodeColor , & ' static [ SerializedDepNodeIndex ] > {
356- let ( start , end ) = self . serialized . edge_list_indices [ source] ;
357- if let Some ( color) = start . color ( ) {
380+ let range = self . serialized . edge_list_indices [ source] ;
381+ if let Some ( color) = range . color ( ) {
358382 return Ok ( color) ;
359383 }
360- let start = start. offset ( ) as usize ;
361- let end = end as usize ;
362384 // The node has not been colored, so the dependencies have not been lifted to point to the
363385 // new nodes vector.
364- let edges = & self . serialized . edge_list_data [ start..end] ;
386+ debug_assert ! ( !range. lifted( ) ) ;
387+ let edges = & self . serialized . edge_list_data [ range. range ( ) ] ;
365388 debug_assert_eq ! (
366389 std:: mem:: size_of:: <DepNodeIndex >( ) ,
367390 std:: mem:: size_of:: <SerializedDepNodeIndex >( )
@@ -407,14 +430,14 @@ impl<K: DepKind> CurrentDepGraph<K> {
407430 let index = * self . index . get ( dep_node) ?;
408431 if let Ok ( prev) = self . as_serialized ( index) {
409432 // Return none if the node has not been coloured yet.
410- self . serialized . edge_list_indices [ prev] . 0 . color ( ) ?;
433+ self . serialized . edge_list_indices [ prev] . color ( ) ?;
411434 }
412435 Some ( index)
413436 }
414437
415438 #[ inline]
416439 pub ( crate ) fn color ( & self , index : SerializedDepNodeIndex ) -> Option < DepNodeColor > {
417- self . serialized . edge_list_indices [ index] . 0 . color ( )
440+ self . serialized . edge_list_indices [ index] . color ( )
418441 }
419442
420443 #[ inline]
@@ -432,9 +455,9 @@ impl<K: DepKind> CurrentDepGraph<K> {
432455
433456 #[ inline]
434457 fn live_serialized_indices ( & self ) -> impl Iterator < Item = SerializedDepNodeIndex > + ' _ {
435- self . serialized . edge_list_indices . iter_enumerated ( ) . filter_map ( |( i, ( s , _ ) ) | {
458+ self . serialized . edge_list_indices . iter_enumerated ( ) . filter_map ( |( i, range ) | {
436459 // Return none if the node has not been coloured yet.
437- let _ = s . color ( ) ?;
460+ let _ = range . color ( ) ?;
438461 Some ( i)
439462 } )
440463 }
@@ -461,9 +484,9 @@ impl<K: DepKind> CurrentDepGraph<K> {
461484 fn edge_map ( & self ) -> impl Iterator < Item = & [ DepNodeIndex ] > + ' _ {
462485 let serialized_edges =
463486 self . live_serialized_indices ( ) . map ( move |index| self . serialized_edges ( index) ) ;
464- let new_edges = self . edge_list_indices . iter ( ) . map ( move |& ( start , end ) | {
465- let start = start as usize ;
466- let end = end as usize ;
487+ let new_edges = self . edge_list_indices . iter ( ) . map ( move |range | {
488+ let start = range . start as usize ;
489+ let end = range . end as usize ;
467490 & self . edge_list_data [ start..end]
468491 } ) ;
469492 serialized_edges. chain ( new_edges)
@@ -620,7 +643,7 @@ impl<D: Decoder, K: DepKind + Decodable<D>> Decodable<D> for SerializedDepGraph<
620643 let mut start: u32 = 0 ;
621644 for i in 0 ..len {
622645 let end: u32 = d. read_seq_elt ( i, Decodable :: decode) ?;
623- indices. push ( ( ColorAndOffset :: unknown ( start) , end) ) ;
646+ indices. push ( ColorAndOffset :: unknown ( start, end) ) ;
624647 start = end;
625648 }
626649 Ok ( indices)
0 commit comments