@@ -4,7 +4,7 @@ use super::query::DepGraphQuery;
44use super :: { DepKind , DepNode , DepNodeIndex } ;
55use rustc_data_structures:: fingerprint:: Fingerprint ;
66use rustc_data_structures:: fx:: FxHashMap ;
7- use rustc_data_structures:: sync:: { Lock , Lrc } ;
7+ use rustc_data_structures:: sync:: Lock ;
88use rustc_index:: vec:: { Idx , IndexVec } ;
99use rustc_serialize:: opaque:: { self , FileEncodeResult , FileEncoder , IntEncodedWithFixedSize } ;
1010use rustc_serialize:: { Decodable , Decoder , Encodable } ;
@@ -125,66 +125,80 @@ struct Stat<K: DepKind> {
125125 edge_counter : u64 ,
126126}
127127
128- struct Stats < K : DepKind > {
129- stats : FxHashMap < K , Stat < K > > ,
128+ struct EncodingStatus < K : DepKind > {
129+ encoder : FileEncoder ,
130130 total_node_count : usize ,
131131 total_edge_count : usize ,
132+ result : FileEncodeResult ,
133+ stats : Option < FxHashMap < K , Stat < K > > > ,
132134}
133135
134- #[ instrument( skip( encoder, _record_graph, record_stats) ) ]
135- fn encode_node < K : DepKind > (
136- encoder : & mut FileEncoder ,
137- _index : DepNodeIndex ,
138- node : & NodeInfo < K > ,
139- _record_graph : & Option < Lrc < Lock < DepGraphQuery < K > > > > ,
140- record_stats : & Option < Lrc < Lock < Stats < K > > > > ,
141- ) -> FileEncodeResult {
142- #[ cfg( debug_assertions) ]
143- if let Some ( record_graph) = & _record_graph {
144- // Do not ICE when a query is called from within `with_query`.
145- if let Some ( record_graph) = & mut record_graph. try_lock ( ) {
146- record_graph. push ( _index, node. node , & node. edges ) ;
136+ impl < K : DepKind > EncodingStatus < K > {
137+ fn new ( encoder : FileEncoder , record_stats : bool ) -> Self {
138+ Self {
139+ encoder,
140+ total_edge_count : 0 ,
141+ total_node_count : 0 ,
142+ result : Ok ( ( ) ) ,
143+ stats : if record_stats { Some ( FxHashMap :: default ( ) ) } else { None } ,
147144 }
148145 }
149146
150- if let Some ( record_stats) = & record_stats {
151- let mut stats = record_stats. lock ( ) ;
152- let kind = node. node . kind ;
147+ #[ instrument( skip( self , _record_graph) ) ]
148+ fn encode_node (
149+ & mut self ,
150+ node : & NodeInfo < K > ,
151+ _record_graph : & Option < Lock < DepGraphQuery < K > > > ,
152+ ) -> DepNodeIndex {
153+ let index = DepNodeIndex :: new ( self . total_node_count ) ;
154+ self . total_node_count += 1 ;
155+
153156 let edge_count = node. edges . len ( ) ;
157+ self . total_edge_count += edge_count;
158+
159+ #[ cfg( debug_assertions) ]
160+ if let Some ( record_graph) = & _record_graph {
161+ // Do not ICE when a query is called from within `with_query`.
162+ if let Some ( record_graph) = & mut record_graph. try_lock ( ) {
163+ record_graph. push ( index, node. node , & node. edges ) ;
164+ }
165+ }
166+
167+ if let Some ( stats) = & mut self . stats {
168+ let kind = node. node . kind ;
154169
155- let stat =
156- stats. stats . entry ( kind) . or_insert ( Stat { kind, node_counter : 0 , edge_counter : 0 } ) ;
157- stat. node_counter += 1 ;
158- stat. edge_counter += edge_count as u64 ;
159- stats. total_node_count += 1 ;
160- stats. total_edge_count += edge_count;
170+ let stat = stats. entry ( kind) . or_insert ( Stat { kind, node_counter : 0 , edge_counter : 0 } ) ;
171+ stat. node_counter += 1 ;
172+ stat. edge_counter += edge_count as u64 ;
173+ }
174+
175+ debug ! ( ?index, ?node) ;
176+ let encoder = & mut self . encoder ;
177+ self . result =
178+ std:: mem:: replace ( & mut self . result , Ok ( ( ) ) ) . and_then ( |( ) | node. encode ( encoder) ) ;
179+ index
161180 }
162181
163- debug ! ( ?_index , ?node ) ;
164- node . encode ( encoder)
165- }
182+ fn finish ( self ) -> FileEncodeResult {
183+ let Self { mut encoder, total_node_count , total_edge_count , result , stats : _ } = self ;
184+ let ( ) = result? ;
166185
167- fn encode_counts (
168- mut encoder : FileEncoder ,
169- node_count : usize ,
170- edge_count : usize ,
171- ) -> FileEncodeResult {
172- let node_count = node_count. try_into ( ) . unwrap ( ) ;
173- let edge_count = edge_count. try_into ( ) . unwrap ( ) ;
174-
175- debug ! ( ?node_count, ?edge_count) ;
176- debug ! ( "position: {:?}" , encoder. position( ) ) ;
177- IntEncodedWithFixedSize ( node_count) . encode ( & mut encoder) ?;
178- IntEncodedWithFixedSize ( edge_count) . encode ( & mut encoder) ?;
179- debug ! ( "position: {:?}" , encoder. position( ) ) ;
180- // Drop the encoder so that nothing is written after the counts.
181- encoder. flush ( )
186+ let node_count = total_node_count. try_into ( ) . unwrap ( ) ;
187+ let edge_count = total_edge_count. try_into ( ) . unwrap ( ) ;
188+
189+ debug ! ( ?node_count, ?edge_count) ;
190+ debug ! ( "position: {:?}" , encoder. position( ) ) ;
191+ IntEncodedWithFixedSize ( node_count) . encode ( & mut encoder) ?;
192+ IntEncodedWithFixedSize ( edge_count) . encode ( & mut encoder) ?;
193+ debug ! ( "position: {:?}" , encoder. position( ) ) ;
194+ // Drop the encoder so that nothing is written after the counts.
195+ encoder. flush ( )
196+ }
182197}
183198
184199pub struct GraphEncoder < K : DepKind > {
185- status : Lock < ( FileEncoder , DepNodeIndex , usize , FileEncodeResult ) > ,
186- record_graph : Option < Lrc < Lock < DepGraphQuery < K > > > > ,
187- record_stats : Option < Lrc < Lock < Stats < K > > > > ,
200+ status : Lock < EncodingStatus < K > > ,
201+ record_graph : Option < Lock < DepGraphQuery < K > > > ,
188202}
189203
190204impl < K : DepKind + Encodable < FileEncoder > > GraphEncoder < K > {
@@ -195,21 +209,12 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
195209 record_stats : bool ,
196210 ) -> Self {
197211 let record_graph = if cfg ! ( debug_assertions) && record_graph {
198- Some ( Lrc :: new ( Lock :: new ( DepGraphQuery :: new ( prev_node_count) ) ) )
199- } else {
200- None
201- } ;
202- let record_stats = if record_stats {
203- Some ( Lrc :: new ( Lock :: new ( Stats {
204- stats : FxHashMap :: default ( ) ,
205- total_node_count : 0 ,
206- total_edge_count : 0 ,
207- } ) ) )
212+ Some ( Lock :: new ( DepGraphQuery :: new ( prev_node_count) ) )
208213 } else {
209214 None
210215 } ;
211- let status = Lock :: new ( ( encoder , DepNodeIndex :: new ( 0 ) , 0 , Ok ( ( ) ) ) ) ;
212- GraphEncoder { status, record_graph, record_stats }
216+ let status = Lock :: new ( EncodingStatus :: new ( encoder , record_stats ) ) ;
217+ GraphEncoder { status, record_graph }
213218 }
214219
215220 pub ( crate ) fn with_query ( & self , f : impl Fn ( & DepGraphQuery < K > ) ) {
@@ -223,10 +228,9 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
223228 total_read_count : u64 ,
224229 total_duplicate_read_count : u64 ,
225230 ) {
226- if let Some ( record_stats) = & self . record_stats {
227- let record_stats = record_stats. lock ( ) ;
228-
229- let mut stats: Vec < _ > = record_stats. stats . values ( ) . collect ( ) ;
231+ let status = self . status . lock ( ) ;
232+ if let Some ( record_stats) = & status. stats {
233+ let mut stats: Vec < _ > = record_stats. values ( ) . collect ( ) ;
230234 stats. sort_by_key ( |s| -( s. node_counter as i64 ) ) ;
231235
232236 const SEPARATOR : & str = "[incremental] --------------------------------\
@@ -237,8 +241,8 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
237241 eprintln ! ( "[incremental] DepGraph Statistics" ) ;
238242 eprintln ! ( "{}" , SEPARATOR ) ;
239243 eprintln ! ( "[incremental]" ) ;
240- eprintln ! ( "[incremental] Total Node Count: {}" , record_stats . total_node_count) ;
241- eprintln ! ( "[incremental] Total Edge Count: {}" , record_stats . total_edge_count) ;
244+ eprintln ! ( "[incremental] Total Node Count: {}" , status . total_node_count) ;
245+ eprintln ! ( "[incremental] Total Edge Count: {}" , status . total_edge_count) ;
242246
243247 if cfg ! ( debug_assertions) {
244248 eprintln ! ( "[incremental] Total Edge Reads: {}" , total_read_count) ;
@@ -257,7 +261,7 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
257261
258262 for stat in stats {
259263 let node_kind_ratio =
260- ( 100.0 * ( stat. node_counter as f64 ) ) / ( record_stats . total_node_count as f64 ) ;
264+ ( 100.0 * ( stat. node_counter as f64 ) ) / ( status . total_node_count as f64 ) ;
261265 let node_kind_avg_edges = ( stat. edge_counter as f64 ) / ( stat. node_counter as f64 ) ;
262266
263267 eprintln ! (
@@ -280,23 +284,11 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
280284 fingerprint : Fingerprint ,
281285 edges : SmallVec < [ DepNodeIndex ; 8 ] > ,
282286 ) -> DepNodeIndex {
283- let & mut ( ref mut encoder, ref mut next_index, ref mut edge_count, ref mut result) =
284- & mut * self . status . lock ( ) ;
285- let index = next_index. clone ( ) ;
286- next_index. increment_by ( 1 ) ;
287- * edge_count += edges. len ( ) ;
288- * result = std:: mem:: replace ( result, Ok ( ( ) ) ) . and_then ( |( ) | {
289- let node = NodeInfo { node, fingerprint, edges } ;
290- encode_node ( encoder, index, & node, & self . record_graph , & self . record_stats )
291- } ) ;
292- index
287+ let node = NodeInfo { node, fingerprint, edges } ;
288+ self . status . lock ( ) . encode_node ( & node, & self . record_graph )
293289 }
294290
295291 pub fn finish ( self ) -> FileEncodeResult {
296- let ( encoder, node_count, edge_count, result) = self . status . into_inner ( ) ;
297- let ( ) = result?;
298- let node_count = node_count. index ( ) ;
299-
300- encode_counts ( encoder, node_count, edge_count)
292+ self . status . into_inner ( ) . finish ( )
301293 }
302294}
0 commit comments