@@ -47,7 +47,9 @@ use rustc_span::{DUMMY_SP, Ident, Span, Symbol, bug, kw, sym};
4747use rustc_structures:: { CrateType , Limit } ;
4848use rustc_type_ir:: TyKind :: * ;
4949pub 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+ } ;
5153use tracing:: { debug, instrument} ;
5254
5355use 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.
306301const 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
310306pub 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
385392pub 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
406413pub 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}
0 commit comments