diff options
| author | bors <bors@rust-lang.org> | 2025-02-26 13:01:45 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-02-26 13:01:45 +0000 |
| commit | ac91805f3179fc2225c60e8ccf5a1daa09d43f3d (patch) | |
| tree | 3c84a9c84cebe59e0608228516706aaee9720f8c /compiler | |
| parent | 2af87eab3b4890d62ad998cf9c55e076d91bf06a (diff) | |
| parent | 7d2cfcab9d8291e412f41e43cdadf587d700d577 (diff) | |
| download | rust-ac91805f3179fc2225c60e8ccf5a1daa09d43f3d.tar.gz rust-ac91805f3179fc2225c60e8ccf5a1daa09d43f3d.zip | |
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.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_data_structures/src/sharded.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 54 |
2 files changed, 33 insertions, 24 deletions
diff --git a/compiler/rustc_data_structures/src/sharded.rs b/compiler/rustc_data_structures/src/sharded.rs index 65488c73d3c..5a53f8af5f8 100644 --- a/compiler/rustc_data_structures/src/sharded.rs +++ b/compiler/rustc_data_structures/src/sharded.rs @@ -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() } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 62a384af12f..00993c40dea 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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), } } |
