@@ -31,7 +31,7 @@ use hir::FreevarMap;
31
31
use ty:: { BareFnTy , InferTy , ParamTy , ProjectionTy , TraitTy } ;
32
32
use ty:: { TyVar , TyVid , IntVar , IntVid , FloatVar , FloatVid } ;
33
33
use ty:: TypeVariants :: * ;
34
- use ty:: layout:: TargetDataLayout ;
34
+ use ty:: layout:: { Layout , TargetDataLayout } ;
35
35
use ty:: maps;
36
36
use util:: common:: MemoizationMap ;
37
37
use util:: nodemap:: { NodeMap , NodeSet , DefIdMap , DefIdSet } ;
@@ -56,6 +56,7 @@ pub struct CtxtArenas<'tcx> {
56
56
bare_fn : TypedArena < BareFnTy < ' tcx > > ,
57
57
region : TypedArena < Region > ,
58
58
stability : TypedArena < attr:: Stability > ,
59
+ layout : TypedArena < Layout > ,
59
60
60
61
// references
61
62
trait_defs : TypedArena < ty:: TraitDef < ' tcx > > ,
@@ -70,6 +71,7 @@ impl<'tcx> CtxtArenas<'tcx> {
70
71
bare_fn : TypedArena :: new ( ) ,
71
72
region : TypedArena :: new ( ) ,
72
73
stability : TypedArena :: new ( ) ,
74
+ layout : TypedArena :: new ( ) ,
73
75
74
76
trait_defs : TypedArena :: new ( ) ,
75
77
adt_defs : TypedArena :: new ( )
@@ -230,6 +232,7 @@ pub struct TyCtxt<'tcx> {
230
232
bare_fn_interner : RefCell < FnvHashMap < & ' tcx BareFnTy < ' tcx > , & ' tcx BareFnTy < ' tcx > > > ,
231
233
region_interner : RefCell < FnvHashMap < & ' tcx Region , & ' tcx Region > > ,
232
234
stability_interner : RefCell < FnvHashMap < & ' tcx attr:: Stability , & ' tcx attr:: Stability > > ,
235
+ layout_interner : RefCell < FnvHashMap < & ' tcx Layout , & ' tcx Layout > > ,
233
236
234
237
pub dep_graph : DepGraph ,
235
238
@@ -423,6 +426,9 @@ pub struct TyCtxt<'tcx> {
423
426
424
427
/// Data layout specification for the current target.
425
428
pub data_layout : TargetDataLayout ,
429
+
430
+ /// Cache for layouts computed from types.
431
+ pub layout_cache : RefCell < FnvHashMap < Ty < ' tcx > , & ' tcx Layout > > ,
426
432
}
427
433
428
434
impl < ' tcx > TyCtxt < ' tcx > {
@@ -504,6 +510,20 @@ impl<'tcx> TyCtxt<'tcx> {
504
510
interned
505
511
}
506
512
513
+ pub fn intern_layout ( & self , layout : Layout ) -> & ' tcx Layout {
514
+ if let Some ( layout) = self . layout_interner . borrow ( ) . get ( & layout) {
515
+ return layout;
516
+ }
517
+
518
+ let interned = self . arenas . layout . alloc ( layout) ;
519
+ if let Some ( prev) = self . layout_interner
520
+ . borrow_mut ( )
521
+ . insert ( interned, interned) {
522
+ bug ! ( "Tried to overwrite interned Layout: {:?}" , prev)
523
+ }
524
+ interned
525
+ }
526
+
507
527
pub fn store_free_region_map ( & self , id : NodeId , map : FreeRegionMap ) {
508
528
if self . free_region_maps . borrow_mut ( ) . insert ( id, map) . is_some ( ) {
509
529
bug ! ( "Tried to overwrite interned FreeRegionMap for NodeId {:?}" , id)
@@ -547,6 +567,7 @@ impl<'tcx> TyCtxt<'tcx> {
547
567
bare_fn_interner : RefCell :: new ( FnvHashMap ( ) ) ,
548
568
region_interner : RefCell :: new ( FnvHashMap ( ) ) ,
549
569
stability_interner : RefCell :: new ( FnvHashMap ( ) ) ,
570
+ layout_interner : RefCell :: new ( FnvHashMap ( ) ) ,
550
571
dep_graph : dep_graph. clone ( ) ,
551
572
types : common_types,
552
573
named_region_map : named_region_map,
@@ -595,6 +616,7 @@ impl<'tcx> TyCtxt<'tcx> {
595
616
fragment_infos : RefCell :: new ( DefIdMap ( ) ) ,
596
617
crate_name : token:: intern_and_get_ident ( crate_name) ,
597
618
data_layout : data_layout,
619
+ layout_cache : RefCell :: new ( FnvHashMap ( ) ) ,
598
620
} , f)
599
621
}
600
622
}
@@ -768,6 +790,7 @@ impl<'tcx> TyCtxt<'tcx> {
768
790
println ! ( "BareFnTy interner: #{}" , self . bare_fn_interner. borrow( ) . len( ) ) ;
769
791
println ! ( "Region interner: #{}" , self . region_interner. borrow( ) . len( ) ) ;
770
792
println ! ( "Stability interner: #{}" , self . stability_interner. borrow( ) . len( ) ) ;
793
+ println ! ( "Layout interner: #{}" , self . layout_interner. borrow( ) . len( ) ) ;
771
794
}
772
795
}
773
796
0 commit comments