Skip to content

Commit 0a223d1

Browse files
committed
Auto merge of rust-lang#50395 - Zoxc:small-tys, r=michaelwoerister
Optimize layout of TypeVariants This makes references to `Slice` use thin pointers by storing the slice length in the slice itself. `GeneratorInterior` is replaced by storing the movability of generators in `TyGenerator` and the interior witness is stored in `GeneratorSubsts` (which is just a wrapper around `&'tcx Substs`, like `ClosureSubsts`). Finally the fields of `TypeAndMut` is stored inline in `TyRef`. These changes combine to reduce `TypeVariants` from 48 bytes to 24 bytes on x86_64. r? @michaelwoerister
2 parents 57dc984 + c9d9c24 commit 0a223d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+615
-537
lines changed

src/librustc/ich/impls_mir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,10 @@ for mir::AggregateKind<'gcx> {
483483
def_id.hash_stable(hcx, hasher);
484484
substs.hash_stable(hcx, hasher);
485485
}
486-
mir::AggregateKind::Generator(def_id, ref substs, ref interior) => {
486+
mir::AggregateKind::Generator(def_id, ref substs, movability) => {
487487
def_id.hash_stable(hcx, hasher);
488488
substs.hash_stable(hcx, hasher);
489-
interior.hash_stable(hcx, hasher);
489+
movability.hash_stable(hcx, hasher);
490490
}
491491
}
492492
}

src/librustc/ich/impls_ty.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,7 @@ for ::middle::const_val::ErrKind<'gcx> {
517517
}
518518

519519
impl_stable_hash_for!(struct ty::ClosureSubsts<'tcx> { substs });
520-
521-
impl_stable_hash_for!(struct ty::GeneratorInterior<'tcx> { witness, movable });
520+
impl_stable_hash_for!(struct ty::GeneratorSubsts<'tcx> { substs });
522521

523522
impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> {
524523
parent,
@@ -889,9 +888,10 @@ for ty::TypeVariants<'gcx>
889888
TyRawPtr(pointee_ty) => {
890889
pointee_ty.hash_stable(hcx, hasher);
891890
}
892-
TyRef(region, pointee_ty) => {
891+
TyRef(region, pointee_ty, mutbl) => {
893892
region.hash_stable(hcx, hasher);
894893
pointee_ty.hash_stable(hcx, hasher);
894+
mutbl.hash_stable(hcx, hasher);
895895
}
896896
TyFnDef(def_id, substs) => {
897897
def_id.hash_stable(hcx, hasher);
@@ -908,10 +908,10 @@ for ty::TypeVariants<'gcx>
908908
def_id.hash_stable(hcx, hasher);
909909
closure_substs.hash_stable(hcx, hasher);
910910
}
911-
TyGenerator(def_id, closure_substs, interior) => {
911+
TyGenerator(def_id, generator_substs, movability) => {
912912
def_id.hash_stable(hcx, hasher);
913-
closure_substs.hash_stable(hcx, hasher);
914-
interior.hash_stable(hcx, hasher);
913+
generator_substs.hash_stable(hcx, hasher);
914+
movability.hash_stable(hcx, hasher);
915915
}
916916
TyGeneratorWitness(types) => {
917917
types.hash_stable(hcx, hasher)
@@ -1315,11 +1315,11 @@ for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContex
13151315
hcx: &mut StableHashingContext<'a>,
13161316
hasher: &mut StableHasher<W>) {
13171317
let traits::VtableGeneratorData {
1318-
closure_def_id,
1318+
generator_def_id,
13191319
substs,
13201320
ref nested,
13211321
} = *self;
1322-
closure_def_id.hash_stable(hcx, hasher);
1322+
generator_def_id.hash_stable(hcx, hasher);
13231323
substs.hash_stable(hcx, hasher);
13241324
nested.hash_stable(hcx, hasher);
13251325
}

src/librustc/infer/error_reporting/mod.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -665,21 +665,22 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
665665

666666
fn push_ty_ref<'tcx>(
667667
r: &ty::Region<'tcx>,
668-
tnm: &ty::TypeAndMut<'tcx>,
668+
ty: Ty<'tcx>,
669+
mutbl: hir::Mutability,
669670
s: &mut DiagnosticStyledString,
670671
) {
671672
let r = &format!("{}", r);
672673
s.push_highlighted(format!(
673674
"&{}{}{}",
674675
r,
675676
if r == "" { "" } else { " " },
676-
if tnm.mutbl == hir::MutMutable {
677+
if mutbl == hir::MutMutable {
677678
"mut "
678679
} else {
679680
""
680681
}
681682
));
682-
s.push_normal(format!("{}", tnm.ty));
683+
s.push_normal(format!("{}", ty));
683684
}
684685

685686
match (&t1.sty, &t2.sty) {
@@ -803,24 +804,25 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
803804
}
804805

805806
// When finding T != &T, highlight only the borrow
806-
(&ty::TyRef(r1, ref tnm1), _) if equals(&tnm1.ty, &t2) => {
807+
(&ty::TyRef(r1, ref_ty1, mutbl1), _) if equals(&ref_ty1, &t2) => {
807808
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
808-
push_ty_ref(&r1, tnm1, &mut values.0);
809+
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
809810
values.1.push_normal(format!("{}", t2));
810811
values
811812
}
812-
(_, &ty::TyRef(r2, ref tnm2)) if equals(&t1, &tnm2.ty) => {
813+
(_, &ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&t1, &ref_ty2) => {
813814
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
814815
values.0.push_normal(format!("{}", t1));
815-
push_ty_ref(&r2, tnm2, &mut values.1);
816+
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
816817
values
817818
}
818819

819820
// When encountering &T != &mut T, highlight only the borrow
820-
(&ty::TyRef(r1, ref tnm1), &ty::TyRef(r2, ref tnm2)) if equals(&tnm1.ty, &tnm2.ty) => {
821+
(&ty::TyRef(r1, ref_ty1, mutbl1),
822+
&ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&ref_ty1, &ref_ty2) => {
821823
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
822-
push_ty_ref(&r1, tnm1, &mut values.0);
823-
push_ty_ref(&r2, tnm2, &mut values.1);
824+
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
825+
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
824826
values
825827
}
826828

src/librustc/middle/expr_use_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
456456
// make sure that the thing we are pointing out stays valid
457457
// for the lifetime `scope_r` of the resulting ptr:
458458
let expr_ty = return_if_err!(self.mc.expr_ty(expr));
459-
if let ty::TyRef(r, _) = expr_ty.sty {
459+
if let ty::TyRef(r, _, _) = expr_ty.sty {
460460
let bk = ty::BorrowKind::from_mutbl(m);
461461
self.borrow_expr(&base, r, bk, AddrOf);
462462
}
@@ -859,7 +859,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
859859
// It is also a borrow or copy/move of the value being matched.
860860
match bm {
861861
ty::BindByReference(m) => {
862-
if let ty::TyRef(r, _) = pat_ty.sty {
862+
if let ty::TyRef(r, _, _) = pat_ty.sty {
863863
let bk = ty::BorrowKind::from_mutbl(m);
864864
delegate.borrow(pat.id, pat.span, &cmt_pat, r, bk, RefBinding);
865865
}

src/librustc/middle/mem_categorization.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10121012
let base_ty = self.expr_ty_adjusted(base)?;
10131013

10141014
let (region, mutbl) = match base_ty.sty {
1015-
ty::TyRef(region, mt) => (region, mt.mutbl),
1015+
ty::TyRef(region, _, mutbl) => (region, mutbl),
10161016
_ => {
10171017
span_bug!(expr.span, "cat_overloaded_place: base is not a reference")
10181018
}
@@ -1046,8 +1046,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10461046
let ptr = match base_cmt.ty.sty {
10471047
ty::TyAdt(def, ..) if def.is_box() => Unique,
10481048
ty::TyRawPtr(ref mt) => UnsafePtr(mt.mutbl),
1049-
ty::TyRef(r, mt) => {
1050-
let bk = ty::BorrowKind::from_mutbl(mt.mutbl);
1049+
ty::TyRef(r, _, mutbl) => {
1050+
let bk = ty::BorrowKind::from_mutbl(mutbl);
10511051
if implicit { Implicit(bk, r) } else { BorrowedPtr(bk, r) }
10521052
}
10531053
ref ty => bug!("unexpected type in cat_deref: {:?}", ty)

src/librustc/mir/mod.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ use hir::def_id::DefId;
2727
use mir::visit::MirVisitable;
2828
use mir::interpret::{Value, PrimVal, EvalErrorKind};
2929
use ty::subst::{Subst, Substs};
30-
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, Region, Ty, TyCtxt, GeneratorInterior};
30+
use ty::{self, AdtDef, CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt};
3131
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
32-
use ty::TypeAndMut;
3332
use util::ppaux;
3433
use std::slice;
3534
use hir::{self, InlineAsm};
@@ -1641,7 +1640,7 @@ pub enum AggregateKind<'tcx> {
16411640
Adt(&'tcx AdtDef, usize, &'tcx Substs<'tcx>, Option<usize>),
16421641

16431642
Closure(DefId, ClosureSubsts<'tcx>),
1644-
Generator(DefId, ClosureSubsts<'tcx>, GeneratorInterior<'tcx>),
1643+
Generator(DefId, GeneratorSubsts<'tcx>, hir::GeneratorMovability),
16451644
}
16461645

16471646
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
@@ -1905,9 +1904,8 @@ pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Resul
19051904
write!(f, "{:?}", ::std::char::from_u32(n as u32).unwrap()),
19061905
(Value::ByVal(PrimVal::Undef), &TyFnDef(did, _)) =>
19071906
write!(f, "{}", item_path_str(did)),
1908-
(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)), &TyRef(_, TypeAndMut {
1909-
ty: &ty::TyS { sty: TyStr, .. }, ..
1910-
})) => {
1907+
(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)),
1908+
&TyRef(_, &ty::TyS { sty: TyStr, .. }, _)) => {
19111909
ty::tls::with(|tcx| {
19121910
let alloc = tcx
19131911
.interpret_interner
@@ -2375,10 +2373,8 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
23752373
AggregateKind::Adt(def, v, substs.fold_with(folder), n),
23762374
AggregateKind::Closure(id, substs) =>
23772375
AggregateKind::Closure(id, substs.fold_with(folder)),
2378-
AggregateKind::Generator(id, substs, interior) =>
2379-
AggregateKind::Generator(id,
2380-
substs.fold_with(folder),
2381-
interior.fold_with(folder)),
2376+
AggregateKind::Generator(id, substs, movablity) =>
2377+
AggregateKind::Generator(id, substs.fold_with(folder), movablity),
23822378
};
23832379
Aggregate(kind, fields.fold_with(folder))
23842380
}
@@ -2405,8 +2401,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
24052401
AggregateKind::Tuple => false,
24062402
AggregateKind::Adt(_, _, substs, _) => substs.visit_with(visitor),
24072403
AggregateKind::Closure(_, substs) => substs.visit_with(visitor),
2408-
AggregateKind::Generator(_, substs, interior) => substs.visit_with(visitor) ||
2409-
interior.visit_with(visitor),
2404+
AggregateKind::Generator(_, substs, _) => substs.visit_with(visitor),
24102405
}) || fields.visit_with(visitor)
24112406
}
24122407
}

src/librustc/mir/tcx.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ impl<'tcx> Rvalue<'tcx> {
184184
tcx.type_of(def.did).subst(tcx, substs)
185185
}
186186
AggregateKind::Closure(did, substs) => {
187-
tcx.mk_closure_from_closure_substs(did, substs)
187+
tcx.mk_closure(did, substs)
188188
}
189-
AggregateKind::Generator(did, substs, interior) => {
190-
tcx.mk_generator(did, substs, interior)
189+
AggregateKind::Generator(did, substs, movability) => {
190+
tcx.mk_generator(did, substs, movability)
191191
}
192192
}
193193
}

src/librustc/mir/visit.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use hir::def_id::DefId;
1212
use ty::subst::Substs;
13-
use ty::{CanonicalTy, ClosureSubsts, Region, Ty, GeneratorInterior};
13+
use ty::{CanonicalTy, ClosureSubsts, GeneratorSubsts, Region, Ty};
1414
use mir::*;
1515
use syntax_pos::Span;
1616

@@ -243,10 +243,10 @@ macro_rules! make_mir_visitor {
243243
self.super_closure_substs(substs);
244244
}
245245

246-
fn visit_generator_interior(&mut self,
247-
interior: & $($mutability)* GeneratorInterior<'tcx>,
246+
fn visit_generator_substs(&mut self,
247+
substs: & $($mutability)* GeneratorSubsts<'tcx>,
248248
_: Location) {
249-
self.super_generator_interior(interior);
249+
self.super_generator_substs(substs);
250250
}
251251

252252
fn visit_local_decl(&mut self,
@@ -595,11 +595,10 @@ macro_rules! make_mir_visitor {
595595
self.visit_closure_substs(closure_substs, location);
596596
}
597597
AggregateKind::Generator(ref $($mutability)* def_id,
598-
ref $($mutability)* closure_substs,
599-
ref $($mutability)* interior) => {
598+
ref $($mutability)* generator_substs,
599+
_movability) => {
600600
self.visit_def_id(def_id, location);
601-
self.visit_closure_substs(closure_substs, location);
602-
self.visit_generator_interior(interior, location);
601+
self.visit_generator_substs(generator_substs, location);
603602
}
604603
}
605604

@@ -786,8 +785,8 @@ macro_rules! make_mir_visitor {
786785
fn super_substs(&mut self, _substs: & $($mutability)* &'tcx Substs<'tcx>) {
787786
}
788787

789-
fn super_generator_interior(&mut self,
790-
_interior: & $($mutability)* GeneratorInterior<'tcx>) {
788+
fn super_generator_substs(&mut self,
789+
_substs: & $($mutability)* GeneratorSubsts<'tcx>) {
791790
}
792791

793792
fn super_closure_substs(&mut self,

src/librustc/traits/error_reporting.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
878878
let mut trait_type = trait_ref.self_ty();
879879

880880
for refs_remaining in 0..refs_number {
881-
if let ty::TypeVariants::TyRef(_, ty::TypeAndMut{ ty: t_type, mutbl: _ }) =
882-
trait_type.sty {
883-
881+
if let ty::TypeVariants::TyRef(_, t_type, _) = trait_type.sty {
884882
trait_type = t_type;
885883

886884
let substs = self.tcx.mk_substs_trait(trait_type, &[]);

src/librustc/traits/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ pub struct VtableImplData<'tcx, N> {
482482

483483
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)]
484484
pub struct VtableGeneratorData<'tcx, N> {
485-
pub closure_def_id: DefId,
486-
pub substs: ty::ClosureSubsts<'tcx>,
485+
pub generator_def_id: DefId,
486+
pub substs: ty::GeneratorSubsts<'tcx>,
487487
/// Nested obligations. This can be non-empty if the generator
488488
/// signature contains associated types.
489489
pub nested: Vec<N>
@@ -991,7 +991,7 @@ impl<'tcx, N> Vtable<'tcx, N> {
991991
nested: p.nested.into_iter().map(f).collect(),
992992
}),
993993
VtableGenerator(c) => VtableGenerator(VtableGeneratorData {
994-
closure_def_id: c.closure_def_id,
994+
generator_def_id: c.generator_def_id,
995995
substs: c.substs,
996996
nested: c.nested.into_iter().map(f).collect(),
997997
}),

src/librustc/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ fn confirm_generator_candidate<'cx, 'gcx, 'tcx>(
12881288
vtable: VtableGeneratorData<'tcx, PredicateObligation<'tcx>>)
12891289
-> Progress<'tcx>
12901290
{
1291-
let gen_sig = vtable.substs.generator_poly_sig(vtable.closure_def_id, selcx.tcx());
1291+
let gen_sig = vtable.substs.poly_sig(vtable.generator_def_id, selcx.tcx());
12921292
let Normalized {
12931293
value: gen_sig,
12941294
obligations

0 commit comments

Comments
 (0)