Skip to content

Commit d8590e2

Browse files
committed
auto merge of #13013 : huonw/rust/tytrait, r=cmr
These variants occur rarely but inflate the whole enum for the other variants, leaving a lot of wasted space. In total this reduces `ty::sty` from 160 bytes to 96 (on a 64-bit platform). After this, `ty_struct` and `ty_enum` are the largest variants, with the 80-byte `substs` being the major contributor.
2 parents 4ca51ae + ddc7960 commit d8590e2

20 files changed

+117
-84
lines changed

src/librustc/metadata/tyencode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
280280
enc_substs(w, cx, substs);
281281
mywrite!(w, "]");
282282
}
283-
ty::ty_trait(def, ref substs, store, mt, bounds) => {
284-
mywrite!(w, "x[{}|", (cx.ds)(def));
283+
ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, mutability, bounds }) => {
284+
mywrite!(w, "x[{}|", (cx.ds)(def_id));
285285
enc_substs(w, cx, substs);
286286
enc_trait_store(w, cx, store);
287-
enc_mutability(w, mt);
287+
enc_mutability(w, mutability);
288288
let bounds = ty::ParamBounds {builtin_bounds: bounds,
289289
trait_bounds: Vec::new()};
290290
enc_bounds(w, cx, &bounds);
@@ -315,7 +315,7 @@ fn enc_sty(w: &mut MemWriter, cx: &ctxt, st: &ty::sty) {
315315
ty::ty_unboxed_vec(mt) => { mywrite!(w, "U"); enc_mt(w, cx, mt); }
316316
ty::ty_closure(ref f) => {
317317
mywrite!(w, "f");
318-
enc_closure_ty(w, cx, f);
318+
enc_closure_ty(w, cx, *f);
319319
}
320320
ty::ty_bare_fn(ref f) => {
321321
mywrite!(w, "F");

src/librustc/middle/kind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,21 @@ fn with_appropriate_checker(cx: &Context,
208208

209209
let fty = ty::node_id_to_type(cx.tcx, id);
210210
match ty::get(fty).sty {
211-
ty::ty_closure(ty::ClosureTy {
211+
ty::ty_closure(~ty::ClosureTy {
212212
sigil: OwnedSigil,
213213
bounds: bounds,
214214
..
215215
}) => {
216216
b(|cx, fv| check_for_uniq(cx, fv, bounds))
217217
}
218-
ty::ty_closure(ty::ClosureTy {
218+
ty::ty_closure(~ty::ClosureTy {
219219
sigil: ManagedSigil,
220220
..
221221
}) => {
222222
// can't happen
223223
fail!("internal error: saw closure with managed sigil (@fn)");
224224
}
225-
ty::ty_closure(ty::ClosureTy {
225+
ty::ty_closure(~ty::ClosureTy {
226226
sigil: BorrowedSigil,
227227
bounds: bounds,
228228
region: region,
@@ -358,7 +358,7 @@ pub fn check_expr(cx: &mut Context, e: &Expr) {
358358
fn check_trait_cast(cx: &mut Context, source_ty: ty::t, target_ty: ty::t, span: Span) {
359359
check_cast_for_escaping_regions(cx, source_ty, target_ty, span);
360360
match ty::get(target_ty).sty {
361-
ty::ty_trait(_, _, _, _, bounds) => {
361+
ty::ty_trait(~ty::TyTrait { bounds, .. }) => {
362362
check_trait_cast_bounds(cx, span, source_ty, bounds);
363363
}
364364
_ => {}

src/librustc/middle/lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ fn check_heap_type(cx: &Context, span: Span, ty: ty::t) {
911911
}
912912
ty::ty_uniq(_) | ty::ty_str(ty::vstore_uniq) |
913913
ty::ty_vec(_, ty::vstore_uniq) |
914-
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
914+
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) => {
915915
n_uniq += 1;
916916
}
917917
ty::ty_closure(ref c) if c.sigil == ast::OwnedSigil => {

src/librustc/middle/mem_categorization.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ pub enum deref_kind {
170170
pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
171171
match ty::get(t).sty {
172172
ty::ty_uniq(_) |
173-
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
173+
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) |
174174
ty::ty_vec(_, ty::vstore_uniq) |
175175
ty::ty_str(ty::vstore_uniq) |
176-
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
176+
ty::ty_closure(~ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
177177
Some(deref_ptr(OwnedPtr))
178178
}
179179

@@ -183,13 +183,13 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
183183
Some(deref_ptr(BorrowedPtr(kind, r)))
184184
}
185185

186-
ty::ty_trait(_, _, ty::RegionTraitStore(r), m, _) => {
186+
ty::ty_trait(~ty::TyTrait { store: ty::RegionTraitStore(r), mutability: m, .. }) => {
187187
let kind = ty::BorrowKind::from_mutbl(m);
188188
Some(deref_ptr(BorrowedPtr(kind, r)))
189189
}
190190

191191
ty::ty_str(ty::vstore_slice(r)) |
192-
ty::ty_closure(ty::ClosureTy {sigil: ast::BorrowedSigil,
192+
ty::ty_closure(~ty::ClosureTy {sigil: ast::BorrowedSigil,
193193
region: r, ..}) => {
194194
Some(deref_ptr(BorrowedPtr(ty::ImmBorrow, r)))
195195
}

src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub fn decl_rust_fn(ccx: &CrateContext, has_env: bool,
269269
// noalias because the actual object pointer is nested.
270270
ty::ty_uniq(..) | // ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
271271
ty::ty_vec(_, ty::vstore_uniq) | ty::ty_str(ty::vstore_uniq) |
272-
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
272+
ty::ty_closure(~ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
273273
unsafe {
274274
llvm::LLVMAddAttribute(llarg, lib::llvm::NoAliasAttribute as c_uint);
275275
}

src/librustc/middle/trans/debuginfo.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,9 @@ fn type_metadata(cx: &CrateContext,
21262126
ty::ty_closure(ref closurety) => {
21272127
subroutine_type_metadata(cx, &closurety.sig, usage_site_span)
21282128
},
2129-
ty::ty_trait(def_id, ref substs, trait_store, mutability, ref bounds) => {
2129+
ty::ty_trait(~ty::TyTrait { def_id, ref substs,
2130+
store: trait_store, mutability,
2131+
ref bounds }) => {
21302132
trait_metadata(cx, def_id, t, substs, trait_store, mutability, bounds)
21312133
},
21322134
ty::ty_struct(def_id, ref substs) => {

src/librustc/middle/trans/glue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
310310
}
311311
}
312312
}
313-
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
313+
ty::ty_trait(~ty::TyTrait { store: ty::UniqTraitStore, .. }) => {
314314
let lluniquevalue = GEPi(bcx, v0, [0, abi::trt_field_box]);
315315
// Only drop the value when it is non-null
316316
with_cond(bcx, IsNotNull(bcx, Load(bcx, lluniquevalue)), |bcx| {

src/librustc/middle/trans/reflect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl<'a> Reflector<'a> {
351351
})
352352
}
353353

354-
ty::ty_trait(_, _, _, _, _) => {
354+
ty::ty_trait(..) => {
355355
let extra = [
356356
self.c_slice(token::intern_and_get_ident(ty_to_str(tcx, t)))
357357
];

src/librustc/middle/ty.rs

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,8 @@ pub enum sty {
746746
ty_ptr(mt),
747747
ty_rptr(Region, mt),
748748
ty_bare_fn(BareFnTy),
749-
ty_closure(ClosureTy),
750-
ty_trait(DefId, substs, TraitStore, ast::Mutability, BuiltinBounds),
749+
ty_closure(~ClosureTy),
750+
ty_trait(~TyTrait),
751751
ty_struct(DefId, substs),
752752
ty_tup(Vec<t>),
753753

@@ -764,6 +764,15 @@ pub enum sty {
764764
ty_unboxed_vec(mt),
765765
}
766766

767+
#[deriving(Clone, Eq, Hash)]
768+
pub struct TyTrait {
769+
def_id: DefId,
770+
substs: substs,
771+
store: TraitStore,
772+
mutability: ast::Mutability,
773+
bounds: BuiltinBounds
774+
}
775+
767776
#[deriving(Eq, Hash)]
768777
pub struct TraitRef {
769778
def_id: DefId,
@@ -1205,10 +1214,10 @@ pub fn mk_t(cx: &ctxt, st: sty) -> t {
12051214
&ty_infer(_) => flags |= needs_infer as uint,
12061215
&ty_self(_) => flags |= has_self as uint,
12071216
&ty_enum(_, ref substs) | &ty_struct(_, ref substs) |
1208-
&ty_trait(_, ref substs, _, _, _) => {
1217+
&ty_trait(~ty::TyTrait { ref substs, .. }) => {
12091218
flags |= sflags(substs);
12101219
match st {
1211-
ty_trait(_, _, RegionTraitStore(r), _, _) => {
1220+
ty_trait(~ty::TyTrait { store: RegionTraitStore(r), .. }) => {
12121221
flags |= rflags(r);
12131222
}
12141223
_ => {}
@@ -1398,7 +1407,7 @@ pub fn mk_mut_unboxed_vec(cx: &ctxt, ty: t) -> t {
13981407
pub fn mk_tup(cx: &ctxt, ts: Vec<t>) -> t { mk_t(cx, ty_tup(ts)) }
13991408

14001409
pub fn mk_closure(cx: &ctxt, fty: ClosureTy) -> t {
1401-
mk_t(cx, ty_closure(fty))
1410+
mk_t(cx, ty_closure(~fty))
14021411
}
14031412

14041413
pub fn mk_bare_fn(cx: &ctxt, fty: BareFnTy) -> t {
@@ -1432,7 +1441,14 @@ pub fn mk_trait(cx: &ctxt,
14321441
bounds: BuiltinBounds)
14331442
-> t {
14341443
// take a copy of substs so that we own the vectors inside
1435-
mk_t(cx, ty_trait(did, substs, store, mutability, bounds))
1444+
let inner = ~TyTrait {
1445+
def_id: did,
1446+
substs: substs,
1447+
store: store,
1448+
mutability: mutability,
1449+
bounds: bounds
1450+
};
1451+
mk_t(cx, ty_trait(inner))
14361452
}
14371453

14381454
pub fn mk_struct(cx: &ctxt, struct_id: ast::DefId, substs: substs) -> t {
@@ -1472,7 +1488,7 @@ pub fn maybe_walk_ty(ty: t, f: |t| -> bool) {
14721488
maybe_walk_ty(tm.ty, f);
14731489
}
14741490
ty_enum(_, ref substs) | ty_struct(_, ref substs) |
1475-
ty_trait(_, ref substs, _, _, _) => {
1491+
ty_trait(~TyTrait { ref substs, .. }) => {
14761492
for subty in (*substs).tps.iter() { maybe_walk_ty(*subty, |x| f(x)); }
14771493
}
14781494
ty_tup(ref ts) => { for tt in ts.iter() { maybe_walk_ty(*tt, |x| f(x)); } }
@@ -2133,7 +2149,7 @@ pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
21332149
}
21342150

21352151
ty_closure(ref c) => {
2136-
closure_contents(cx, c)
2152+
closure_contents(cx, *c)
21372153
}
21382154

21392155
ty_box(typ) => {
@@ -2144,8 +2160,8 @@ pub fn type_contents(cx: &ctxt, ty: t) -> TypeContents {
21442160
tc_ty(cx, typ, cache).owned_pointer()
21452161
}
21462162

2147-
ty_trait(_, _, store, mutbl, bounds) => {
2148-
object_contents(cx, store, mutbl, bounds)
2163+
ty_trait(~ty::TyTrait { store, mutability, bounds, .. }) => {
2164+
object_contents(cx, store, mutability, bounds)
21492165
}
21502166

21512167
ty_ptr(ref mt) => {
@@ -2437,7 +2453,7 @@ pub fn is_instantiable(cx: &ctxt, r_ty: t) -> bool {
24372453
false // unsafe ptrs can always be NULL
24382454
}
24392455

2440-
ty_trait(_, _, _, _, _) => {
2456+
ty_trait(..) => {
24412457
false
24422458
}
24432459

@@ -2854,7 +2870,7 @@ pub fn ty_region(tcx: &ctxt,
28542870
pub fn replace_fn_sig(cx: &ctxt, fsty: &sty, new_sig: FnSig) -> t {
28552871
match *fsty {
28562872
ty_bare_fn(ref f) => mk_bare_fn(cx, BareFnTy {sig: new_sig, ..*f}),
2857-
ty_closure(ref f) => mk_closure(cx, ClosureTy {sig: new_sig, ..*f}),
2873+
ty_closure(ref f) => mk_closure(cx, ClosureTy {sig: new_sig, ..**f}),
28582874
ref s => {
28592875
cx.sess.bug(
28602876
format!("ty_fn_sig() called on non-fn type: {:?}", s));
@@ -2872,7 +2888,7 @@ pub fn replace_closure_return_type(tcx: &ctxt, fn_type: t, ret_type: t) -> t {
28722888
ty::ty_closure(ref fty) => {
28732889
ty::mk_closure(tcx, ClosureTy {
28742890
sig: FnSig {output: ret_type, ..fty.sig.clone()},
2875-
..(*fty).clone()
2891+
..(**fty).clone()
28762892
})
28772893
}
28782894
_ => {
@@ -3124,7 +3140,7 @@ pub fn adjust_ty(cx: &ctxt,
31243140
ty::mk_closure(cx, ClosureTy {
31253141
sigil: BorrowedSigil,
31263142
region: r,
3127-
..(*fty).clone()
3143+
..(**fty).clone()
31283144
})
31293145
}
31303146

@@ -3140,9 +3156,9 @@ pub fn adjust_ty(cx: &ctxt,
31403156
fn borrow_obj(cx: &ctxt, span: Span, r: Region,
31413157
m: ast::Mutability, ty: ty::t) -> ty::t {
31423158
match get(ty).sty {
3143-
ty_trait(trt_did, ref trt_substs, _, _, b) => {
3144-
ty::mk_trait(cx, trt_did, trt_substs.clone(),
3145-
RegionTraitStore(r), m, b)
3159+
ty_trait(~ty::TyTrait {def_id, ref substs, bounds, .. }) => {
3160+
ty::mk_trait(cx, def_id, substs.clone(),
3161+
RegionTraitStore(r), m, bounds)
31463162
}
31473163
ref s => {
31483164
cx.sess.span_bug(
@@ -3479,7 +3495,7 @@ pub fn ty_sort_str(cx: &ctxt, t: t) -> ~str {
34793495
ty_rptr(_, _) => ~"&-ptr",
34803496
ty_bare_fn(_) => ~"extern fn",
34813497
ty_closure(_) => ~"fn",
3482-
ty_trait(id, _, _, _, _) => format!("trait {}", item_path_str(cx, id)),
3498+
ty_trait(ref inner) => format!("trait {}", item_path_str(cx, inner.def_id)),
34833499
ty_struct(id, _) => format!("struct {}", item_path_str(cx, id)),
34843500
ty_tup(_) => ~"tuple",
34853501
ty_infer(TyVar(_)) => ~"inferred type",
@@ -3865,7 +3881,7 @@ pub fn try_add_builtin_trait(tcx: &ctxt,
38653881

38663882
pub fn ty_to_def_id(ty: t) -> Option<ast::DefId> {
38673883
match get(ty).sty {
3868-
ty_trait(id, _, _, _, _) | ty_struct(id, _) | ty_enum(id, _) => Some(id),
3884+
ty_trait(~TyTrait { def_id: id, .. }) | ty_struct(id, _) | ty_enum(id, _) => Some(id),
38693885
_ => None
38703886
}
38713887
}
@@ -4951,7 +4967,7 @@ pub fn hash_crate_independent(tcx: &ctxt, t: t, svh: &Svh) -> u64 {
49514967
hash!(c.bounds);
49524968
region(&mut state, c.region);
49534969
}
4954-
ty_trait(d, _, store, m, bounds) => {
4970+
ty_trait(~ty::TyTrait { def_id: d, store, mutability: m, bounds, .. }) => {
49554971
byte!(17);
49564972
did(&mut state, d);
49574973
match store {

src/librustc/middle/ty_fold.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
159159
ty::ty_enum(tid, ref substs) => {
160160
ty::ty_enum(tid, this.fold_substs(substs))
161161
}
162-
ty::ty_trait(did, ref substs, st, mutbl, bounds) => {
163-
ty::ty_trait(did,
164-
this.fold_substs(substs),
165-
this.fold_trait_store(st),
166-
mutbl,
167-
bounds)
162+
ty::ty_trait(~ty::TyTrait { def_id, ref substs, store, mutability, bounds }) => {
163+
ty::ty_trait(~ty::TyTrait{
164+
def_id: def_id,
165+
substs: this.fold_substs(substs),
166+
store: this.fold_trait_store(store),
167+
mutability: mutability,
168+
bounds: bounds
169+
})
168170
}
169171
ty::ty_tup(ref ts) => {
170172
ty::ty_tup(fold_ty_vec(this, ts.as_slice()))
@@ -173,7 +175,7 @@ pub fn super_fold_sty<T:TypeFolder>(this: &mut T,
173175
ty::ty_bare_fn(this.fold_bare_fn_ty(f))
174176
}
175177
ty::ty_closure(ref f) => {
176-
ty::ty_closure(this.fold_closure_ty(f))
178+
ty::ty_closure(~this.fold_closure_ty(*f))
177179
}
178180
ty::ty_rptr(r, ref tm) => {
179181
ty::ty_rptr(this.fold_region(r),

src/librustc/middle/typeck/check/method.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ impl<'a> LookupContext<'a> {
417417
let span = self.self_expr.map_or(self.span, |e| e.span);
418418
check::autoderef(self.fcx, span, self_ty, None, PreferMutLvalue, |self_ty, _| {
419419
match get(self_ty).sty {
420-
ty_trait(did, ref substs, _, _, _) => {
421-
self.push_inherent_candidates_from_object(did, substs);
422-
self.push_inherent_impl_candidates_for_type(did);
420+
ty_trait(~TyTrait { def_id, ref substs, .. }) => {
421+
self.push_inherent_candidates_from_object(def_id, substs);
422+
self.push_inherent_impl_candidates_for_type(def_id);
423423
}
424424
ty_enum(did, _) | ty_struct(did, _) => {
425425
if self.check_traits == CheckTraitsAndInherentMethods {
@@ -775,10 +775,12 @@ impl<'a> LookupContext<'a> {
775775
autoderefs: autoderefs,
776776
autoref: Some(ty::AutoBorrowVec(region, self_mt.mutbl))})
777777
}
778-
ty::ty_trait(did, ref substs, ty::RegionTraitStore(_), mutbl, bounds) => {
778+
ty::ty_trait(~ty::TyTrait {
779+
def_id, ref substs, store: ty::RegionTraitStore(_), mutability: mutbl, bounds
780+
}) => {
779781
let region =
780782
self.infcx().next_region_var(infer::Autoref(self.span));
781-
(ty::mk_trait(tcx, did, substs.clone(),
783+
(ty::mk_trait(tcx, def_id, substs.clone(),
782784
ty::RegionTraitStore(region),
783785
mutbl, bounds),
784786
ty::AutoDerefRef {
@@ -860,7 +862,7 @@ impl<'a> LookupContext<'a> {
860862
})
861863
}
862864

863-
ty_trait(trt_did, trt_substs, _, _, b) => {
865+
ty_trait(~ty::TyTrait { def_id: trt_did, substs: trt_substs, bounds: b, .. }) => {
864866
// Coerce ~/@/&Trait instances to &Trait.
865867

866868
self.search_for_some_kind_of_autorefd_method(
@@ -1301,7 +1303,9 @@ impl<'a> LookupContext<'a> {
13011303
rcvr_matches_ty(self.fcx, mt.ty, candidate)
13021304
}
13031305

1304-
ty::ty_trait(self_did, _, RegionTraitStore(_), self_m, _) => {
1306+
ty::ty_trait(~ty::TyTrait {
1307+
def_id: self_did, store: RegionTraitStore(_), mutability: self_m, ..
1308+
}) => {
13051309
mutability_matches(self_m, m) &&
13061310
rcvr_matches_object(self_did, candidate)
13071311
}
@@ -1317,7 +1321,9 @@ impl<'a> LookupContext<'a> {
13171321
rcvr_matches_ty(self.fcx, typ, candidate)
13181322
}
13191323

1320-
ty::ty_trait(self_did, _, UniqTraitStore, _, _) => {
1324+
ty::ty_trait(~ty::TyTrait {
1325+
def_id: self_did, store: UniqTraitStore, ..
1326+
}) => {
13211327
rcvr_matches_object(self_did, candidate)
13221328
}
13231329

0 commit comments

Comments
 (0)