@@ -4,7 +4,7 @@ use super::query::DepGraphQuery;
4
4
use super :: { DepKind , DepNode , DepNodeIndex } ;
5
5
use rustc_data_structures:: fingerprint:: Fingerprint ;
6
6
use rustc_data_structures:: fx:: FxHashMap ;
7
- use rustc_data_structures:: sync:: { Lock , Lrc } ;
7
+ use rustc_data_structures:: sync:: Lock ;
8
8
use rustc_index:: vec:: { Idx , IndexVec } ;
9
9
use rustc_serialize:: opaque:: { self , FileEncodeResult , FileEncoder , IntEncodedWithFixedSize } ;
10
10
use rustc_serialize:: { Decodable , Decoder , Encodable } ;
@@ -125,66 +125,80 @@ struct Stat<K: DepKind> {
125
125
edge_counter : u64 ,
126
126
}
127
127
128
- struct Stats < K : DepKind > {
129
- stats : FxHashMap < K , Stat < K > > ,
128
+ struct EncodingStatus < K : DepKind > {
129
+ encoder : FileEncoder ,
130
130
total_node_count : usize ,
131
131
total_edge_count : usize ,
132
+ result : FileEncodeResult ,
133
+ stats : Option < FxHashMap < K , Stat < K > > > ,
132
134
}
133
135
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 } ,
147
144
}
148
145
}
149
146
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
+
153
156
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 ;
154
169
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
161
180
}
162
181
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? ;
166
185
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
+ }
182
197
}
183
198
184
199
pub 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 > > > ,
188
202
}
189
203
190
204
impl < K : DepKind + Encodable < FileEncoder > > GraphEncoder < K > {
@@ -195,21 +209,12 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
195
209
record_stats : bool ,
196
210
) -> Self {
197
211
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) ) )
208
213
} else {
209
214
None
210
215
} ;
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 }
213
218
}
214
219
215
220
pub ( crate ) fn with_query ( & self , f : impl Fn ( & DepGraphQuery < K > ) ) {
@@ -223,10 +228,9 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
223
228
total_read_count : u64 ,
224
229
total_duplicate_read_count : u64 ,
225
230
) {
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 ( ) ;
230
234
stats. sort_by_key ( |s| -( s. node_counter as i64 ) ) ;
231
235
232
236
const SEPARATOR : & str = "[incremental] --------------------------------\
@@ -237,8 +241,8 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
237
241
eprintln ! ( "[incremental] DepGraph Statistics" ) ;
238
242
eprintln ! ( "{}" , SEPARATOR ) ;
239
243
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) ;
242
246
243
247
if cfg ! ( debug_assertions) {
244
248
eprintln ! ( "[incremental] Total Edge Reads: {}" , total_read_count) ;
@@ -257,7 +261,7 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
257
261
258
262
for stat in stats {
259
263
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 ) ;
261
265
let node_kind_avg_edges = ( stat. edge_counter as f64 ) / ( stat. node_counter as f64 ) ;
262
266
263
267
eprintln ! (
@@ -280,23 +284,11 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
280
284
fingerprint : Fingerprint ,
281
285
edges : SmallVec < [ DepNodeIndex ; 8 ] > ,
282
286
) -> 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 )
293
289
}
294
290
295
291
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 ( )
301
293
}
302
294
}
0 commit comments