@@ -20,8 +20,8 @@ pub fn merge_codegen_units<'tcx>(
20
20
// We want this merging to produce a deterministic ordering of codegen units
21
21
// from the input.
22
22
//
23
- // Due to basically how we've implemented the merging below (merge the two
24
- // smallest into each other ) we're sure to start off with a deterministic
23
+ // Due to basically how we've implemented the merging below (repeatedly
24
+ // merging adjacent pairs of CGUs ) we're sure to start off with a deterministic
25
25
// order (sorted by name). This'll mean that if two cgus have the same size
26
26
// the stable sort below will keep everything nice and deterministic.
27
27
codegen_units. sort_by ( |a, b| a. name ( ) . as_str ( ) . cmp ( b. name ( ) . as_str ( ) ) ) ;
@@ -30,29 +30,26 @@ pub fn merge_codegen_units<'tcx>(
30
30
let mut cgu_contents: FxHashMap < Symbol , Vec < Symbol > > =
31
31
codegen_units. iter ( ) . map ( |cgu| ( cgu. name ( ) , vec ! [ cgu. name( ) ] ) ) . collect ( ) ;
32
32
33
- // Merge the two smallest codegen units until the target size is reached .
33
+ // Repeatedly merge cgu[n] into cgu[n-1] .
34
34
while codegen_units. len ( ) > cx. target_cgu_count {
35
- // Sort small cgus to the back
35
+ // njn: more comments about this.
36
+ // Sort small cgus to the back. At this point
36
37
codegen_units. sort_by_cached_key ( |cgu| cmp:: Reverse ( cgu. size_estimate ( ) ) ) ;
37
- let mut smallest = codegen_units. pop ( ) . unwrap ( ) ;
38
- let second_smallest = codegen_units. last_mut ( ) . unwrap ( ) ;
38
+ let mut cgu_n = codegen_units. swap_remove ( cx . target_cgu_count ) ;
39
+ let cgu_n_minus_1 = & mut codegen_units[ cx . target_cgu_count - 1 ] ;
39
40
40
- // Move the mono-items from `smallest ` to `second_smallest `
41
- second_smallest . modify_size_estimate ( smallest . size_estimate ( ) ) ;
42
- for ( k, v) in smallest . items_mut ( ) . drain ( ) {
43
- second_smallest . items_mut ( ) . insert ( k, v) ;
41
+ // Move the mono-items from `cgu_n ` to `cgu_n_minus_1 `
42
+ cgu_n_minus_1 . modify_size_estimate ( cgu_n . size_estimate ( ) ) ;
43
+ for ( k, v) in cgu_n . items_mut ( ) . drain ( ) {
44
+ cgu_n_minus_1 . items_mut ( ) . insert ( k, v) ;
44
45
}
45
46
46
- // Record that `second_smallest ` now contains all the stuff that was in
47
- // `smallest ` before.
48
- let mut consumed_cgu_names = cgu_contents. remove ( & smallest . name ( ) ) . unwrap ( ) ;
49
- cgu_contents. get_mut ( & second_smallest . name ( ) ) . unwrap ( ) . append ( & mut consumed_cgu_names) ;
47
+ // Record that `cgu_n_minus_1 ` now contains all the stuff that was in
48
+ // `cgu_n ` before.
49
+ let mut consumed_cgu_names = cgu_contents. remove ( & cgu_n . name ( ) ) . unwrap ( ) ;
50
+ cgu_contents. get_mut ( & cgu_n_minus_1 . name ( ) ) . unwrap ( ) . append ( & mut consumed_cgu_names) ;
50
51
51
- debug ! (
52
- "CodegenUnit {} merged into CodegenUnit {}" ,
53
- smallest. name( ) ,
54
- second_smallest. name( )
55
- ) ;
52
+ debug ! ( "CodegenUnit {} merged into CodegenUnit {}" , cgu_n. name( ) , cgu_n_minus_1. name( ) ) ;
56
53
}
57
54
58
55
let cgu_name_builder = & mut CodegenUnitNameBuilder :: new ( cx. tcx ) ;
0 commit comments