mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-17 06:26:55 +00:00
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:
commit
ac91805f31
@ -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()
|
||||
}
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user