Skip to content

Commit

Permalink
Rollup merge of rust-lang#113872 - nnethercote:tweak-cgu-sorting, r=p…
Browse files Browse the repository at this point in the history
…nkfelix

Tweak CGU sorting in a couple of places.

In `base.rs`, tweak how the CGU size interleaving works. Since rust-lang#113777, it's much more common to have multiple CGUs with identical sizes. With the existing code these same-sized items ended up in the opposite-to-desired order due to the stable sorting. The code now starts with a reverse sort (like is done in `partitioning.rs`) which gives the behaviour we want. This doesn't matter much for perf, but makes profiles in `samply` look more like what we expect.

In `partitioning.rs`, we can use `sort_by_key` instead of `sort_by_cached_key` because `CGU::size_estimate()` is cheap. (There is an identical CGU sort earlier in that function that already uses `sort_by_key`.)

r? ```@pnkfelix```
  • Loading branch information
workingjubilee authored Jul 26, 2023
2 parents e655ecd + 8c31219 commit 05adff5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use rustc_span::symbol::sym;
use rustc_span::Symbol;
use rustc_target::abi::{Align, FIRST_VARIANT};

use std::cmp;
use std::collections::BTreeSet;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -682,10 +683,10 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
// are large size variations, this can reduce memory usage significantly.
let codegen_units: Vec<_> = {
let mut sorted_cgus = codegen_units.iter().collect::<Vec<_>>();
sorted_cgus.sort_by_cached_key(|cgu| cgu.size_estimate());
sorted_cgus.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));

let (first_half, second_half) = sorted_cgus.split_at(sorted_cgus.len() / 2);
second_half.iter().rev().interleave(first_half).copied().collect()
first_half.iter().interleave(second_half.iter().rev()).copied().collect()
};

// Calculate the CGU reuse
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_monomorphize/src/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ fn merge_codegen_units<'tcx>(
&& codegen_units.iter().any(|cgu| cgu.size_estimate() < NON_INCR_MIN_CGU_SIZE)
{
// Sort small cgus to the back.
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));

let mut smallest = codegen_units.pop().unwrap();
let second_smallest = codegen_units.last_mut().unwrap();
Expand Down

0 comments on commit 05adff5

Please sign in to comment.