Skip to content

Commit ebe0c47

Browse files
authored
Rollup merge of #163981 - nnethercote:diesel-opts-1, r=jdonszelmann
New solver opts for `diesel` A collection of small optimizations. Details in individual commits. Local measurements: ``` solver icounts (B) ------ ----------- old 37.9 new (orig) 56.4 (1.49x) new (this PR) 49.5 (1.31x) ``` Also some small improvements on a range of other benchmarks. r? @jdonszelmann
2 parents b542750 + d1998c0 commit ebe0c47

10 files changed

Lines changed: 129 additions & 121 deletions

File tree

‎compiler/rustc_middle/src/ty/context.rs‎

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ use rustc_span::{DUMMY_SP, Ident, Span, Symbol, bug, kw, sym};
4747
use rustc_structures::{CrateType, Limit};
4848
use rustc_type_ir::TyKind::*;
4949
pub use rustc_type_ir::lift::Lift;
50-
use rustc_type_ir::{CollectAndApply, WithCachedTypeInfo, elaborate, search_graph};
50+
use rustc_type_ir::{
51+
CollectAndApply, PlaceholderType, WithCachedTypeInfo, elaborate, search_graph,
52+
};
5153
use tracing::{debug, instrument};
5254

5355
use crate::arena::Arena;
@@ -284,28 +286,22 @@ impl<'tcx> CtxtInterners<'tcx> {
284286
// For these preinterned values, an alternative would be to have
285287
// variable-length vectors that grow as needed. But that turned out to be
286288
// slightly more complex and no faster.
289+
//
290+
// All numbers have been chosen based on profiling data.
291+
292+
const NUM_PREINTERNED_INFER_TY_VARS: u32 = 512;
293+
const NUM_PREINTERNED_INFER_FRESH_TYS: u32 = 20;
294+
const NUM_PREINTERNED_INFER_FRESH_INT_TYS: u32 = 3;
295+
const NUM_PREINTERNED_INFER_FRESH_FLOAT_TYS: u32 = 3;
296+
const NUM_PREINTERNED_BOUND_BOUND_ANONS_I: u32 = 3;
297+
const NUM_PREINTERNED_BOUND_BOUND_ANONS_V: u32 = 20;
298+
const NUM_PREINTERNED_BOUND_CANONICAL_ANONS: u32 = 128;
299+
const NUM_PREINTERNED_PLACEHOLDER_ROOT_ANONS: u32 = 128;
287300

288-
const NUM_PREINTERNED_TY_VARS: u32 = 100;
289-
const NUM_PREINTERNED_FRESH_TYS: u32 = 20;
290-
const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3;
291-
const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3;
292-
const NUM_PREINTERNED_ANON_BOUND_TYS_I: u32 = 3;
293-
294-
// From general profiling of the *max vars during canonicalization* of a value:
295-
// - about 90% of the time, there are no canonical vars
296-
// - about 9% of the time, there is only one canonical var
297-
// - there are rarely more than 3-5 canonical vars (with exceptions in particularly pathological
298-
// cases)
299-
// This may not match the number of bound vars found in `for`s.
300-
// Given that this is all heap interned, it seems likely that interning fewer
301-
// vars here won't make an appreciable difference. Though, if we were to inline the data (in an
302-
// array), we may want to consider reducing the number for canonicalized vars down to 4 or so.
303-
const NUM_PREINTERNED_ANON_BOUND_TYS_V: u32 = 20;
304-
305-
// This number may seem high, but it is reached in all but the smallest crates.
306301
const NUM_PREINTERNED_RE_VARS: u32 = 500;
307-
const NUM_PREINTERNED_ANON_RE_BOUNDS_I: u32 = 3;
308-
const NUM_PREINTERNED_ANON_RE_BOUNDS_V: u32 = 20;
302+
const NUM_PREINTERNED_RE_BOUND_BOUND_ANONS_I: u32 = 3;
303+
const NUM_PREINTERNED_RE_BOUND_BOUND_ANONS_V: u32 = 20;
304+
const NUM_PREINTERNED_RE_BOUND_CANONICAL_ANONS: u32 = 20;
309305

310306
pub struct CommonTypes<'tcx> {
311307
pub unit: Ty<'tcx>,
@@ -360,26 +356,37 @@ pub struct CommonTypes<'tcx> {
360356
pub trait_object_dummy_self: Ty<'tcx>,
361357

362358
/// Pre-interned `Infer(ty::TyVar(n))` for small values of `n`.
363-
pub ty_vars: Vec<Ty<'tcx>>,
359+
pub infer_ty_vars: Vec<Ty<'tcx>>,
364360

365361
/// Pre-interned `Infer(ty::FreshTy(n))` for small values of `n`.
366-
pub fresh_tys: Vec<Ty<'tcx>>,
362+
pub infer_fresh_tys: Vec<Ty<'tcx>>,
367363

368364
/// Pre-interned `Infer(ty::FreshIntTy(n))` for small values of `n`.
369-
pub fresh_int_tys: Vec<Ty<'tcx>>,
365+
pub infer_fresh_int_tys: Vec<Ty<'tcx>>,
370366

371367
/// Pre-interned `Infer(ty::FreshFloatTy(n))` for small values of `n`.
372-
pub fresh_float_tys: Vec<Ty<'tcx>>,
368+
pub infer_fresh_float_tys: Vec<Ty<'tcx>>,
369+
370+
/// Pre-interned values of the form:
371+
/// `Bound(
372+
/// BoundVarIndexKind::Bound(DebruijnIndex(i)),
373+
/// BoundTy { var: v, kind: BoundTyKind::Anon }
374+
/// )`
375+
/// for small values of `i` and `v`.
376+
pub bound_bound_anons: Vec<Vec<Ty<'tcx>>>,
373377

374378
/// Pre-interned values of the form:
375-
/// `Bound(BoundVarIndexKind::Bound(DebruijnIndex(i)), BoundTy { var: v, kind:
376-
/// BoundTyKind::Anon})` for small values of `i` and `v`.
377-
pub anon_bound_tys: Vec<Vec<Ty<'tcx>>>,
378-
379-
// Pre-interned values of the form:
380-
// `Bound(BoundVarIndexKind::Canonical, BoundTy { var: v, kind: BoundTyKind::Anon })`
381-
// for small values of `v`.
382-
pub anon_canonical_bound_tys: Vec<Ty<'tcx>>,
379+
/// `Bound(BoundVarIndexKind::Canonical, BoundTy { var: v, kind: BoundTyKind::Anon })`
380+
/// for small values of `v`.
381+
pub bound_canonical_anons: Vec<Ty<'tcx>>,
382+
383+
/// Pre-interned values of the form:
384+
/// `Placeholder(Placeholder {
385+
/// universe: UniverseIndex::ROOT,
386+
/// bound: BoundTy { var: v, kind: BoundTyKind::Anon }
387+
/// })`
388+
/// for small values of `v`.
389+
pub placeholder_root_anons: Vec<Ty<'tcx>>,
383390
}
384391

385392
pub struct CommonLifetimes<'tcx> {
@@ -389,18 +396,18 @@ pub struct CommonLifetimes<'tcx> {
389396
/// Erased region, used outside of type inference.
390397
pub re_erased: Region<'tcx>,
391398

392-
/// Pre-interned `ReVar(ty::RegionVar(n))` for small values of `n`.
399+
/// Pre-interned `ReVar(ty::RegionVid(n))` for small values of `n`.
393400
pub re_vars: Vec<Region<'tcx>>,
394401

395402
/// Pre-interned values of the form:
396403
/// `ReBound(BoundVarIndexKind::Bound(DebruijnIndex(i)), BoundRegion { var: v, kind: BoundRegionKind::Anon })`
397404
/// for small values of `i` and `v`.
398-
pub anon_re_bounds: Vec<Vec<Region<'tcx>>>,
405+
pub re_bound_bound_anons: Vec<Vec<Region<'tcx>>>,
399406

400-
// Pre-interned values of the form:
401-
// `ReBound(BoundVarIndexKind::Canonical, BoundRegion { var: v, kind: BoundRegionKind::Anon })`
402-
// for small values of `v`.
403-
pub anon_re_canonical_bounds: Vec<Region<'tcx>>,
407+
/// Pre-interned values of the form:
408+
/// `ReBound(BoundVarIndexKind::Canonical, BoundRegion { var: v, kind: BoundRegionKind::Anon })`
409+
/// for small values of `v`.
410+
pub re_bound_canonical_anons: Vec<Region<'tcx>>,
404411
}
405412

406413
pub struct CommonConsts<'tcx> {
@@ -415,18 +422,21 @@ impl<'tcx> CommonTypes<'tcx> {
415422
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
416423
let mk = |ty| interners.intern_ty(ty);
417424

418-
let ty_vars =
419-
(0..NUM_PREINTERNED_TY_VARS).map(|n| mk(Infer(ty::TyVar(TyVid::from(n))))).collect();
420-
let fresh_tys: Vec<_> =
421-
(0..NUM_PREINTERNED_FRESH_TYS).map(|n| mk(Infer(ty::FreshTy(n)))).collect();
422-
let fresh_int_tys: Vec<_> =
423-
(0..NUM_PREINTERNED_FRESH_INT_TYS).map(|n| mk(Infer(ty::FreshIntTy(n)))).collect();
424-
let fresh_float_tys: Vec<_> =
425-
(0..NUM_PREINTERNED_FRESH_FLOAT_TYS).map(|n| mk(Infer(ty::FreshFloatTy(n)))).collect();
425+
let infer_ty_vars = (0..NUM_PREINTERNED_INFER_TY_VARS)
426+
.map(|n| mk(Infer(ty::TyVar(TyVid::from(n)))))
427+
.collect();
428+
let infer_fresh_tys: Vec<_> =
429+
(0..NUM_PREINTERNED_INFER_FRESH_TYS).map(|n| mk(Infer(ty::FreshTy(n)))).collect();
430+
let infer_fresh_int_tys: Vec<_> = (0..NUM_PREINTERNED_INFER_FRESH_INT_TYS)
431+
.map(|n| mk(Infer(ty::FreshIntTy(n))))
432+
.collect();
433+
let infer_fresh_float_tys: Vec<_> = (0..NUM_PREINTERNED_INFER_FRESH_FLOAT_TYS)
434+
.map(|n| mk(Infer(ty::FreshFloatTy(n))))
435+
.collect();
426436

427-
let anon_bound_tys = (0..NUM_PREINTERNED_ANON_BOUND_TYS_I)
437+
let bound_bound_anons = (0..NUM_PREINTERNED_BOUND_BOUND_ANONS_I)
428438
.map(|i| {
429-
(0..NUM_PREINTERNED_ANON_BOUND_TYS_V)
439+
(0..NUM_PREINTERNED_BOUND_BOUND_ANONS_V)
430440
.map(|v| {
431441
mk(ty::Bound(
432442
ty::BoundVarIndexKind::Bound(ty::DebruijnIndex::from(i)),
@@ -437,7 +447,7 @@ impl<'tcx> CommonTypes<'tcx> {
437447
})
438448
.collect();
439449

440-
let anon_canonical_bound_tys = (0..NUM_PREINTERNED_ANON_BOUND_TYS_V)
450+
let bound_canonical_anons = (0..NUM_PREINTERNED_BOUND_CANONICAL_ANONS)
441451
.map(|v| {
442452
mk(ty::Bound(
443453
ty::BoundVarIndexKind::Canonical,
@@ -446,6 +456,15 @@ impl<'tcx> CommonTypes<'tcx> {
446456
})
447457
.collect();
448458

459+
let placeholder_root_anons = (0..NUM_PREINTERNED_PLACEHOLDER_ROOT_ANONS)
460+
.map(|v| {
461+
mk(ty::Placeholder(PlaceholderType::new_anon(
462+
ty::UniverseIndex::ROOT,
463+
ty::BoundVar::from(v),
464+
)))
465+
})
466+
.collect();
467+
449468
CommonTypes {
450469
unit: mk(Tuple(List::empty())),
451470
bool: mk(Bool),
@@ -470,14 +489,15 @@ impl<'tcx> CommonTypes<'tcx> {
470489
str_: mk(Str),
471490
self_param: mk(ty::Param(ty::ParamTy { index: 0, name: kw::SelfUpper })),
472491

473-
trait_object_dummy_self: fresh_tys[0],
492+
trait_object_dummy_self: infer_fresh_tys[0],
474493

475-
ty_vars,
476-
fresh_tys,
477-
fresh_int_tys,
478-
fresh_float_tys,
479-
anon_bound_tys,
480-
anon_canonical_bound_tys,
494+
infer_ty_vars,
495+
infer_fresh_tys,
496+
infer_fresh_int_tys,
497+
infer_fresh_float_tys,
498+
bound_bound_anons,
499+
bound_canonical_anons,
500+
placeholder_root_anons,
481501
}
482502
}
483503
}
@@ -493,9 +513,9 @@ impl<'tcx> CommonLifetimes<'tcx> {
493513
let re_vars =
494514
(0..NUM_PREINTERNED_RE_VARS).map(|n| mk(ty::ReVar(ty::RegionVid::from(n)))).collect();
495515

496-
let anon_re_bounds = (0..NUM_PREINTERNED_ANON_RE_BOUNDS_I)
516+
let re_bound_bound_anons = (0..NUM_PREINTERNED_RE_BOUND_BOUND_ANONS_I)
497517
.map(|i| {
498-
(0..NUM_PREINTERNED_ANON_RE_BOUNDS_V)
518+
(0..NUM_PREINTERNED_RE_BOUND_BOUND_ANONS_V)
499519
.map(|v| {
500520
mk(ty::ReBound(
501521
ty::BoundVarIndexKind::Bound(ty::DebruijnIndex::from(i)),
@@ -509,7 +529,7 @@ impl<'tcx> CommonLifetimes<'tcx> {
509529
})
510530
.collect();
511531

512-
let anon_re_canonical_bounds = (0..NUM_PREINTERNED_ANON_RE_BOUNDS_V)
532+
let re_bound_canonical_anons = (0..NUM_PREINTERNED_RE_BOUND_CANONICAL_ANONS)
513533
.map(|v| {
514534
mk(ty::ReBound(
515535
ty::BoundVarIndexKind::Canonical,
@@ -522,8 +542,8 @@ impl<'tcx> CommonLifetimes<'tcx> {
522542
re_static: mk(ty::ReStatic),
523543
re_erased: mk(ty::ReErased),
524544
re_vars,
525-
anon_re_bounds,
526-
anon_re_canonical_bounds,
545+
re_bound_bound_anons,
546+
re_bound_canonical_anons,
527547
}
528548
}
529549
}

‎compiler/rustc_middle/src/ty/context/impl_interner.rs‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
207207
ty::AliasConstKind::Anon { .. } => None,
208208
}
209209
}
210-
fn anon_const_kind(self, def_id: DefId) -> ty::AnonConstKind {
211-
self.anon_const_kind(def_id)
212-
}
213210

214211
fn def_span(self, def_id: DefId) -> Span {
215212
self.def_span(def_id)
@@ -494,10 +491,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
494491
self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
495492
}
496493

497-
fn is_adt_lang_item(self, def_id: DefId, lang_item: SolverAdtLangItem) -> bool {
498-
self.is_lang_item(def_id, solver_adt_lang_item_to_lang_item(lang_item))
499-
}
500-
501494
fn is_default_trait(self, def_id: DefId) -> bool {
502495
self.is_default_trait(def_id)
503496
}
@@ -604,10 +597,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
604597
self.trait_def(trait_def_id).safety.is_unsafe()
605598
}
606599

607-
fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
608-
self.is_impl_trait_in_trait(def_id)
609-
}
610-
611600
fn delay_bug(self, msg: impl ToString) -> ErrorGuaranteed {
612601
self.dcx().span_delayed_bug(DUMMY_SP, msg.to_string())
613602
}
@@ -683,18 +672,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
683672
})
684673
}
685674

686-
fn get_anon_re_bounds_lifetime(self, idx: usize, var_idx: usize) -> Option<Region<'tcx>> {
687-
if let Some(inner) = self.lifetimes.anon_re_bounds.get(idx) {
688-
inner.get(var_idx).copied()
689-
} else {
690-
None
691-
}
692-
}
693-
694-
fn get_anon_re_canonical_bounds_lifetime(self, idx: usize) -> Option<Region<'tcx>> {
695-
self.lifetimes.anon_re_canonical_bounds.get(idx).copied()
696-
}
697-
698675
fn get_re_static_lifetime(self) -> Region<'tcx> {
699676
self.lifetimes.re_static
700677
}
@@ -719,7 +696,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
719696
) -> Region<'tcx> {
720697
// Use a pre-interned one when possible.
721698
if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region
722-
&& let Some(inner) = self.lifetimes.anon_re_bounds.get(debruijn.as_usize())
699+
&& let Some(inner) = self.lifetimes.re_bound_bound_anons.get(debruijn.as_usize())
723700
&& let Some(re) = inner.get(var.as_usize()).copied()
724701
{
725702
re
@@ -730,7 +707,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
730707

731708
fn intern_canonical_bound(self, var: BoundVar) -> Region<'tcx> {
732709
// Use a pre-interned one when possible.
733-
if let Some(re) = self.lifetimes.anon_re_canonical_bounds.get(var.as_usize()).copied() {
710+
if let Some(re) = self.lifetimes.re_bound_canonical_anons.get(var.as_usize()).copied() {
734711
re
735712
} else {
736713
self.intern_region(ty::ReBound(

‎compiler/rustc_middle/src/ty/sty.rs‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ impl<'tcx> Ty<'tcx> {
378378
pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::TyVid) -> Ty<'tcx> {
379379
// Use a pre-interned one when possible.
380380
tcx.types
381-
.ty_vars
381+
.infer_ty_vars
382382
.get(v.as_usize())
383383
.copied()
384384
.unwrap_or_else(|| Ty::new(tcx, Infer(TyVar(v))))
@@ -398,7 +398,7 @@ impl<'tcx> Ty<'tcx> {
398398
pub fn new_fresh(tcx: TyCtxt<'tcx>, n: u32) -> Ty<'tcx> {
399399
// Use a pre-interned one when possible.
400400
tcx.types
401-
.fresh_tys
401+
.infer_fresh_tys
402402
.get(n as usize)
403403
.copied()
404404
.unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshTy(n)))
@@ -408,7 +408,7 @@ impl<'tcx> Ty<'tcx> {
408408
pub fn new_fresh_int(tcx: TyCtxt<'tcx>, n: u32) -> Ty<'tcx> {
409409
// Use a pre-interned one when possible.
410410
tcx.types
411-
.fresh_int_tys
411+
.infer_fresh_int_tys
412412
.get(n as usize)
413413
.copied()
414414
.unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshIntTy(n)))
@@ -418,7 +418,7 @@ impl<'tcx> Ty<'tcx> {
418418
pub fn new_fresh_float(tcx: TyCtxt<'tcx>, n: u32) -> Ty<'tcx> {
419419
// Use a pre-interned one when possible.
420420
tcx.types
421-
.fresh_float_tys
421+
.infer_fresh_float_tys
422422
.get(n as usize)
423423
.copied()
424424
.unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshFloatTy(n)))
@@ -437,7 +437,7 @@ impl<'tcx> Ty<'tcx> {
437437
) -> Ty<'tcx> {
438438
// Use a pre-interned one when possible.
439439
if let ty::BoundTy { var, kind: ty::BoundTyKind::Anon } = bound_ty
440-
&& let Some(inner) = tcx.types.anon_bound_tys.get(index.as_usize())
440+
&& let Some(inner) = tcx.types.bound_bound_anons.get(index.as_usize())
441441
&& let Some(ty) = inner.get(var.as_usize()).copied()
442442
{
443443
ty
@@ -449,7 +449,7 @@ impl<'tcx> Ty<'tcx> {
449449
#[inline]
450450
pub fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: BoundVar) -> Ty<'tcx> {
451451
// Use a pre-interned one when possible.
452-
if let Some(ty) = tcx.types.anon_canonical_bound_tys.get(var.as_usize()).copied() {
452+
if let Some(ty) = tcx.types.bound_canonical_anons.get(var.as_usize()).copied() {
453453
ty
454454
} else {
455455
Ty::new(
@@ -464,7 +464,15 @@ impl<'tcx> Ty<'tcx> {
464464

465465
#[inline]
466466
pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType<'tcx>) -> Ty<'tcx> {
467-
Ty::new(tcx, Placeholder(placeholder))
467+
if placeholder.universe.is_root()
468+
&& placeholder.bound.kind == ty::BoundTyKind::Anon
469+
&& let Some(ty) =
470+
tcx.types.placeholder_root_anons.get(placeholder.bound.var.as_usize()).copied()
471+
{
472+
ty
473+
} else {
474+
Ty::new(tcx, Placeholder(placeholder))
475+
}
468476
}
469477

470478
#[inline]

‎compiler/rustc_next_trait_solver/src/canonical/canonicalizer.rs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,12 @@ impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
178178
delegate,
179179
CanonicalizeMode::Input(CanonicalizeInputKind::Predicate),
180180
);
181-
rest_canonicalizer.state.variables.extend(e.variables.iter().copied());
182-
rest_canonicalizer.state.var_kinds.extend(e.var_kinds.iter().copied());
183-
// SAFETY: The iterated elements go straight back into a hashmap.
184-
#[allow(rustc::potential_query_instability)]
181+
rest_canonicalizer.state.variables.clone_from(&e.variables);
182+
rest_canonicalizer.state.var_kinds.clone_from(&e.var_kinds);
185183
rest_canonicalizer
186184
.state
187185
.variable_lookup_table
188-
.extend(e.variable_lookup_table.iter().map(|(&arg, &idx)| (arg, idx)));
186+
.clone_from(&e.variable_lookup_table);
189187
(e.param_env, rest_canonicalizer)
190188
}
191189
})

0 commit comments

Comments
 (0)