Skip to content

Commit f5637ed

Browse files
committed
Auto merge of rust-lang#137354 - FractalFir:intern_with_cap, r=<try>
[perf experiment] Changed interners to start preallocated with an increased capacity Inspired by rust-lang#137005. *Not meant to be merged in its current form* 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: ![image](https://github.com/user-attachments/assets/4a7f3317-7e61-4b28-a651-cc79ee990689) The current default capacities are choosen somewhat arbitrarily, and are relatively low. Depending on what kind of memory usage is acceptable, it may be beneficial to increase that capacity for some interners. From a second local perf run(with capacity of `_type` increased to `131072`), it looks like increasing the size of the preallocated type interner has the biggest impact: ![image](https://github.com/user-attachments/assets/08ac324a-b03c-4fe9-b779-4dd35e7970d9) What would be the maximum acceptable memory usage increase? I think most people would not mind sacrificing 1-2MB for an improvement in compile speed, but I am curious what is the general opinion here.
2 parents a18bd8a + e914e37 commit f5637ed

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

compiler/rustc_data_structures/src/sharded.rs

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ pub fn shards() -> usize {
143143
pub type ShardedHashMap<K, V> = Sharded<FxHashMap<K, V>>;
144144

145145
impl<K: Eq, V> ShardedHashMap<K, V> {
146+
pub fn with_capacity(cap:usize)->Self{
147+
Self::new(||{FxHashMap::with_capacity_and_hasher(cap, rustc_hash::FxBuildHasher::default())})
148+
}
146149
pub fn len(&self) -> usize {
147150
self.lock_shards().map(|shard| shard.len()).sum()
148151
}

compiler/rustc_middle/src/ty/context.rs

+26-25
Original file line numberDiff line numberDiff line change
@@ -811,33 +811,34 @@ pub struct CtxtInterners<'tcx> {
811811
}
812812

813813
impl<'tcx> CtxtInterners<'tcx> {
814-
fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> {
814+
fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> {
815815
CtxtInterners {
816816
arena,
817-
type_: Default::default(),
818-
const_lists: Default::default(),
819-
args: Default::default(),
820-
type_lists: Default::default(),
821-
region: Default::default(),
822-
poly_existential_predicates: Default::default(),
823-
canonical_var_infos: Default::default(),
824-
predicate: Default::default(),
825-
clauses: Default::default(),
826-
projs: Default::default(),
827-
place_elems: Default::default(),
828-
const_: Default::default(),
829-
pat: Default::default(),
830-
const_allocation: Default::default(),
831-
bound_variable_kinds: Default::default(),
832-
layout: Default::default(),
833-
adt_def: Default::default(),
834-
external_constraints: Default::default(),
835-
predefined_opaques_in_body: Default::default(),
836-
fields: Default::default(),
837-
local_def_ids: Default::default(),
838-
captures: Default::default(),
839-
offset_of: Default::default(),
840-
valtree: Default::default(),
817+
// There are likely to be a lot of types. This should occupy 0.4 MB.
818+
type_: InternedSet::with_capacity(16384),
819+
const_lists: InternedSet::with_capacity(4096),
820+
args: InternedSet::with_capacity(4096),
821+
type_lists: InternedSet::with_capacity(4096),
822+
region: InternedSet::with_capacity(4096),
823+
poly_existential_predicates: InternedSet::with_capacity(1024),
824+
canonical_var_infos: InternedSet::with_capacity(1024),
825+
predicate:InternedSet::with_capacity(1024),
826+
clauses: InternedSet::with_capacity(1024),
827+
projs: InternedSet::with_capacity(4096),
828+
place_elems: InternedSet::with_capacity(4096),
829+
const_: InternedSet::with_capacity(4096),
830+
pat: InternedSet::with_capacity(1024),
831+
const_allocation: InternedSet::with_capacity(1024),
832+
bound_variable_kinds: InternedSet::with_capacity(1024),
833+
layout: InternedSet::with_capacity(1024),
834+
adt_def: InternedSet::with_capacity(1024),
835+
external_constraints: InternedSet::with_capacity(1024),
836+
predefined_opaques_in_body: InternedSet::with_capacity(1024),
837+
fields: InternedSet::with_capacity(4096),
838+
local_def_ids: InternedSet::with_capacity(1024),
839+
captures: InternedSet::with_capacity(1024),
840+
offset_of: InternedSet::with_capacity(1024),
841+
valtree: InternedSet::with_capacity(1024),
841842
}
842843
}
843844

0 commit comments

Comments
 (0)