@@ -104,16 +104,16 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
104
104
// same set). We want this merging to produce a deterministic ordering
105
105
// of codegen units from the input.
106
106
//
107
- // Due to basically how we've implemented the merging below (merge the
108
- // two smallest into each other ) we're sure to start off with a
109
- // deterministic order (sorted by name). This'll mean that if two cgus
110
- // have the same size the stable sort below will keep everything nice
111
- // and deterministic.
107
+ // Due to basically how we've implemented the merging below (repeatedly
108
+ // merging adjacent pairs of CGUs ) we're sure to start off with a
109
+ // deterministic deterministic order (sorted by name). This'll mean that
110
+ // if two cgus have the same size the stable sort below will keep
111
+ // everything nice and deterministic.
112
112
codegen_units. sort_by ( |a, b| a. name ( ) . as_str ( ) . cmp ( b. name ( ) . as_str ( ) ) ) ;
113
113
114
114
//---------------------------------------------------------------------------
115
115
// njn: split big CGUs if necessary
116
- if false && codegen_units. len ( ) > cx. target_cgu_count {
116
+ if codegen_units. len ( ) > cx. target_cgu_count {
117
117
// njn: type ann?
118
118
let total_size: usize = codegen_units. iter ( ) . map ( |cgu| cgu. size_estimate ( ) ) . sum ( ) ;
119
119
let target_size = total_size / cx. target_cgu_count ;
@@ -123,6 +123,7 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
123
123
// njn: make it a for loop?
124
124
// njn: explain all this
125
125
let mut i = 0 ;
126
+ let mut j = 0 ; // njn: explain
126
127
let n = codegen_units. len ( ) ;
127
128
while i < n {
128
129
let old_cgu = & mut codegen_units[ i] ;
@@ -134,7 +135,7 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
134
135
// times
135
136
136
137
let mut new_name = old_cgu. name ( ) . to_string ( ) ;
137
- new_name += "-split" ;
138
+ new_name += & format ! ( "-split{}" , j ) ;
138
139
let mut new_cgu = CodegenUnit :: new ( Symbol :: intern ( & new_name) ) ;
139
140
new_cgu. create_size_estimate ( cx. tcx ) ; // initially zero
140
141
@@ -164,9 +165,11 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
164
165
165
166
codegen_units. push ( new_cgu) ;
166
167
// njn: explain lack of `i += 1`;
168
+ j += 1 ;
167
169
} else {
168
170
// njn: explain this
169
171
i += 1 ;
172
+ j = 0 ;
170
173
}
171
174
}
172
175
}
@@ -176,30 +179,26 @@ impl<'tcx> Partition<'tcx> for DefaultPartitioning {
176
179
let mut cgu_contents: FxHashMap < Symbol , Vec < Symbol > > =
177
180
codegen_units. iter ( ) . map ( |cgu| ( cgu. name ( ) , vec ! [ cgu. name( ) ] ) ) . collect ( ) ;
178
181
179
- // Merge the two smallest codegen units until the target size is
180
- // reached.
182
+ // Repeatedly merge cgu[n] into cgu[n-1].
181
183
while codegen_units. len ( ) > cx. target_cgu_count {
182
- // Sort small cgus to the back
184
+ // njn: more comments about this.
185
+ // Sort small cgus to the back. At this point... njn: more
183
186
codegen_units. sort_by_cached_key ( |cgu| cmp:: Reverse ( cgu. size_estimate ( ) ) ) ;
184
- let mut smallest = codegen_units. pop ( ) . unwrap ( ) ;
185
- let second_smallest = codegen_units. last_mut ( ) . unwrap ( ) ;
187
+ let mut cgu_n = codegen_units. swap_remove ( cx . target_cgu_count ) ;
188
+ let cgu_n_minus_1 = & mut codegen_units[ cx . target_cgu_count - 1 ] ;
186
189
187
- // Move the mono-items from `smallest ` to `second_smallest `
188
- second_smallest . increase_size_estimate ( smallest . size_estimate ( ) ) ;
189
- for ( k, v) in smallest . items_mut ( ) . drain ( ) {
190
- second_smallest . items_mut ( ) . insert ( k, v) ;
190
+ // Move the mono-items from `cgu_n ` to `cgu_n_minus_1 `
191
+ cgu_n_minus_1 . increase_size_estimate ( cgu_n . size_estimate ( ) ) ;
192
+ for ( k, v) in cgu_n . items_mut ( ) . drain ( ) {
193
+ cgu_n_minus_1 . items_mut ( ) . insert ( k, v) ;
191
194
}
192
195
193
- // Record that `second_smallest ` now contains all the stuff that was
194
- // in `smallest ` before.
195
- let mut consumed_cgu_names = cgu_contents. remove ( & smallest . name ( ) ) . unwrap ( ) ;
196
- cgu_contents. get_mut ( & second_smallest . name ( ) ) . unwrap ( ) . append ( & mut consumed_cgu_names) ;
196
+ // Record that `cgu_n_minus_1 ` now contains all the stuff that was in
197
+ // `cgu_n ` before.
198
+ let mut consumed_cgu_names = cgu_contents. remove ( & cgu_n . name ( ) ) . unwrap ( ) ;
199
+ cgu_contents. get_mut ( & cgu_n_minus_1 . name ( ) ) . unwrap ( ) . append ( & mut consumed_cgu_names) ;
197
200
198
- debug ! (
199
- "CodegenUnit {} merged into CodegenUnit {}" ,
200
- smallest. name( ) ,
201
- second_smallest. name( )
202
- ) ;
201
+ debug ! ( "CodegenUnit {} merged into CodegenUnit {}" , cgu_n. name( ) , cgu_n_minus_1. name( ) ) ;
203
202
}
204
203
205
204
let cgu_name_builder = & mut CodegenUnitNameBuilder :: new ( cx. tcx ) ;
0 commit comments