Skip to content

Change interners to start preallocated with an increased capacity #137354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading