Skip to content

Commit

Permalink
Auto merge of rust-lang#137354 - FractalFir:intern_with_cap, r=Fracta…
Browse files Browse the repository at this point in the history
…lFir

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.
  • Loading branch information
bors committed Feb 26, 2025
2 parents 2af87ea + 7d2cfca commit ac91805
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_data_structures/src/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ pub fn shards() -> usize {
pub type ShardedHashMap<K, V> = Sharded<FxHashMap<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 {
self.lock_shards().map(|shard| shard.len()).sum()
}
Expand Down
54 changes: 30 additions & 24 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,32 +812,38 @@ pub struct CtxtInterners<'tcx> {

impl<'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 {
arena,
type_: Default::default(),
const_lists: Default::default(),
args: Default::default(),
type_lists: Default::default(),
region: Default::default(),
poly_existential_predicates: Default::default(),
canonical_var_infos: Default::default(),
predicate: Default::default(),
clauses: Default::default(),
projs: Default::default(),
place_elems: Default::default(),
const_: Default::default(),
pat: Default::default(),
const_allocation: Default::default(),
bound_variable_kinds: Default::default(),
layout: Default::default(),
adt_def: Default::default(),
external_constraints: Default::default(),
predefined_opaques_in_body: Default::default(),
fields: Default::default(),
local_def_ids: Default::default(),
captures: Default::default(),
offset_of: Default::default(),
valtree: Default::default(),
// The factors have been chosen by @FractalFir based on observed interner sizes, and local perf runs.
// To get the interner sizes, insert `eprintln` printing the size of the interner in functions like `intern_ty`.
// Bigger benchmarks tend to give more accurate ratios, so use something like `x perf eprintln --includes cargo`.
type_: InternedSet::with_capacity(N * 16),
const_lists: InternedSet::with_capacity(N * 4),
args: InternedSet::with_capacity(N * 4),
type_lists: InternedSet::with_capacity(N * 4),
region: InternedSet::with_capacity(N * 4),
poly_existential_predicates: InternedSet::with_capacity(N / 4),
canonical_var_infos: InternedSet::with_capacity(N / 2),
predicate: InternedSet::with_capacity(N),
clauses: InternedSet::with_capacity(N),
projs: InternedSet::with_capacity(N * 4),
place_elems: InternedSet::with_capacity(N * 2),
const_: InternedSet::with_capacity(N * 2),
pat: InternedSet::with_capacity(N),
const_allocation: InternedSet::with_capacity(N),
bound_variable_kinds: InternedSet::with_capacity(N * 2),
layout: InternedSet::with_capacity(N),
adt_def: InternedSet::with_capacity(N),
external_constraints: InternedSet::with_capacity(N),
predefined_opaques_in_body: InternedSet::with_capacity(N),
fields: InternedSet::with_capacity(N * 4),
local_def_ids: InternedSet::with_capacity(N),
captures: InternedSet::with_capacity(N),
offset_of: InternedSet::with_capacity(N),
valtree: InternedSet::with_capacity(N),
}
}

Expand Down

0 comments on commit ac91805

Please sign in to comment.