Auto merge of #137354 - FractalFir:intern_with_cap, r=FractalFir

Change interners to start preallocated with an increased capacity

Inspired by https://github.com/rust-lang/rust/issues/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.
This commit is contained in:
bors 2025-02-26 13:01:45 +00:00
commit ac91805f31
2 changed files with 33 additions and 24 deletions

View file

@ -143,6 +143,9 @@ pub fn shards() -> usize {
pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>; pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
impl<K: Eq, V> ShardedHashMap<K, V> { impl<K: Eq, V> ShardedHashMap<K, V> {
pub fn with_capacity(cap: usize) -> Self {
Self::new(|| FxHashMap::with_capacity_and_hasher(cap, rustc_hash::FxBuildHasher::default()))
}
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.lock_shards().map(|shard| shard.len()).sum() self.lock_shards().map(|shard| shard.len()).sum()
} }

View file

@ -812,32 +812,38 @@ pub struct CtxtInterners<'tcx> {
impl<'tcx> CtxtInterners<'tcx> { impl<'tcx> CtxtInterners<'tcx> {
fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> { fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> {
// Default interner size - this value has been chosen empirically, and may need to be adjusted
// as the compiler evolves.
const N: usize = 2048;
CtxtInterners { CtxtInterners {
arena, arena,
type_: Default::default(), // The factors have been chosen by @FractalFir based on observed interner sizes, and local perf runs.
const_lists: Default::default(), // To get the interner sizes, insert `eprintln` printing the size of the interner in functions like `intern_ty`.
args: Default::default(), // Bigger benchmarks tend to give more accurate ratios, so use something like `x perf eprintln --includes cargo`.
type_lists: Default::default(), type_: InternedSet::with_capacity(N * 16),
region: Default::default(), const_lists: InternedSet::with_capacity(N * 4),
poly_existential_predicates: Default::default(), args: InternedSet::with_capacity(N * 4),
canonical_var_infos: Default::default(), type_lists: InternedSet::with_capacity(N * 4),
predicate: Default::default(), region: InternedSet::with_capacity(N * 4),
clauses: Default::default(), poly_existential_predicates: InternedSet::with_capacity(N / 4),
projs: Default::default(), canonical_var_infos: InternedSet::with_capacity(N / 2),
place_elems: Default::default(), predicate: InternedSet::with_capacity(N),
const_: Default::default(), clauses: InternedSet::with_capacity(N),
pat: Default::default(), projs: InternedSet::with_capacity(N * 4),
const_allocation: Default::default(), place_elems: InternedSet::with_capacity(N * 2),
bound_variable_kinds: Default::default(), const_: InternedSet::with_capacity(N * 2),
layout: Default::default(), pat: InternedSet::with_capacity(N),
adt_def: Default::default(), const_allocation: InternedSet::with_capacity(N),
external_constraints: Default::default(), bound_variable_kinds: InternedSet::with_capacity(N * 2),
predefined_opaques_in_body: Default::default(), layout: InternedSet::with_capacity(N),
fields: Default::default(), adt_def: InternedSet::with_capacity(N),
local_def_ids: Default::default(), external_constraints: InternedSet::with_capacity(N),
captures: Default::default(), predefined_opaques_in_body: InternedSet::with_capacity(N),
offset_of: Default::default(), fields: InternedSet::with_capacity(N * 4),
valtree: Default::default(), local_def_ids: InternedSet::with_capacity(N),
captures: InternedSet::with_capacity(N),
offset_of: InternedSet::with_capacity(N),
valtree: InternedSet::with_capacity(N),
} }
} }