Skip to content

Commit ac91805

Browse files
committed
Auto merge of rust-lang#137354 - FractalFir:intern_with_cap, r=FractalFir
Change interners to start preallocated with an increased capacity Inspired by rust-lang#137005. Added a `with_capacity` function to `InternedSet`. Changed the `CtxtInterners` to start with `InternedSets` preallocated with a capacity. This *does* increase memory usage at very slightly(by ~1 MB at the start), altough that increase quickly disaperars for larger crates(since they require such capacity anyway). A local perf run indicates this improves compiletimes for small crates(like `ripgrep`), without a negative effect on larger ones.
2 parents 2af87ea + 7d2cfca commit ac91805

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

compiler/rustc_data_structures/src/sharded.rs

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ pub fn shards() -> usize {
143143
pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
144144

145145
impl<K: Eq, V> ShardedHashMap<K, V> {
146+
pub fn with_capacity(cap: usize) -> Self {
147+
Self::new(|| FxHashMap::with_capacity_and_hasher(cap, rustc_hash::FxBuildHasher::default()))
148+
}
146149
pub fn len(&self) -> usize {
147150
self.lock_shards().map(|shard| shard.len()).sum()
148151
}

compiler/rustc_middle/src/ty/context.rs

+30-24
Original file line numberDiff line numberDiff line change
@@ -812,32 +812,38 @@ pub struct CtxtInterners<'tcx> {
812812

813813
impl<'tcx> CtxtInterners<'tcx> {
814814
fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> {
815+
// Default interner size - this value has been chosen empirically, and may need to be adjusted
816+
// as the compiler evolves.
817+
const N: usize = 2048;
815818
CtxtInterners {
816819
arena,
817-
type_: Default::default(),
818-
const_lists: Default::default(),
819-
args: Default::default(),
820-
type_lists: Default::default(),
821-
region: Default::default(),
822-
poly_existential_predicates: Default::default(),
823-
canonical_var_infos: Default::default(),
824-
predicate: Default::default(),
825-
clauses: Default::default(),
826-
projs: Default::default(),
827-
place_elems: Default::default(),
828-
const_: Default::default(),
829-
pat: Default::default(),
830-
const_allocation: Default::default(),
831-
bound_variable_kinds: Default::default(),
832-
layout: Default::default(),
833-
adt_def: Default::default(),
834-
external_constraints: Default::default(),
835-
predefined_opaques_in_body: Default::default(),
836-
fields: Default::default(),
837-
local_def_ids: Default::default(),
838-
captures: Default::default(),
839-
offset_of: Default::default(),
840-
valtree: Default::default(),
820+
// The factors have been chosen by @FractalFir based on observed interner sizes, and local perf runs.
821+
// To get the interner sizes, insert `eprintln` printing the size of the interner in functions like `intern_ty`.
822+
// Bigger benchmarks tend to give more accurate ratios, so use something like `x perf eprintln --includes cargo`.
823+
type_: InternedSet::with_capacity(N * 16),
824+
const_lists: InternedSet::with_capacity(N * 4),
825+
args: InternedSet::with_capacity(N * 4),
826+
type_lists: InternedSet::with_capacity(N * 4),
827+
region: InternedSet::with_capacity(N * 4),
828+
poly_existential_predicates: InternedSet::with_capacity(N / 4),
829+
canonical_var_infos: InternedSet::with_capacity(N / 2),
830+
predicate: InternedSet::with_capacity(N),
831+
clauses: InternedSet::with_capacity(N),
832+
projs: InternedSet::with_capacity(N * 4),
833+
place_elems: InternedSet::with_capacity(N * 2),
834+
const_: InternedSet::with_capacity(N * 2),
835+
pat: InternedSet::with_capacity(N),
836+
const_allocation: InternedSet::with_capacity(N),
837+
bound_variable_kinds: InternedSet::with_capacity(N * 2),
838+
layout: InternedSet::with_capacity(N),
839+
adt_def: InternedSet::with_capacity(N),
840+
external_constraints: InternedSet::with_capacity(N),
841+
predefined_opaques_in_body: InternedSet::with_capacity(N),
842+
fields: InternedSet::with_capacity(N * 4),
843+
local_def_ids: InternedSet::with_capacity(N),
844+
captures: InternedSet::with_capacity(N),
845+
offset_of: InternedSet::with_capacity(N),
846+
valtree: InternedSet::with_capacity(N),
841847
}
842848
}
843849

0 commit comments

Comments
 (0)