Skip to content

Commit 405b5fc

Browse files
committed
rustc: put ty_trait behind some indirection.
This reduces ty::sty from 160 bytes to just 112, and some measurements eddyb made suggest that the ty_trait variant occurs very rarely (e.g. ~1% of all sty instances) hence this will result in a large memory saving, and the cost of the indirection is unlikely to be an issue.
1 parent a39c294 commit 405b5fc

File tree

18 files changed

+94
-61
lines changed

18 files changed

+94
-61
lines changed

src/librustc/metadata/tyencode.rs

Lines changed: 3 additions & 3 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);

src/librustc/middle/kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ 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) |
176176
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
@@ -183,7 +183,7 @@ 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
}

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: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ pub enum sty {
747747
ty_rptr(Region, mt),
748748
ty_bare_fn(BareFnTy),
749749
ty_closure(ClosureTy),
750-
ty_trait(DefId, substs, TraitStore, ast::Mutability, BuiltinBounds),
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
_ => {}
@@ -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)); } }
@@ -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

@@ -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: 8 additions & 6 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()))

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
542542
// explaining how it goes about doing that.
543543
let target_ty = rcx.resolve_node_type(expr.id);
544544
match ty::get(target_ty).sty {
545-
ty::ty_trait(_, _, ty::RegionTraitStore(trait_region), _, _) => {
545+
ty::ty_trait(~ty::TyTrait { store: ty::RegionTraitStore(trait_region), .. }) => {
546546
let source_ty = rcx.resolve_expr_type_adjusted(source);
547547
constrain_regions_in_type(
548548
rcx,

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ fn fixup_substs(vcx: &VtableContext,
478478
ty::EmptyBuiltinBounds());
479479
fixup_ty(vcx, span, t, is_early).map(|t_f| {
480480
match ty::get(t_f).sty {
481-
ty::ty_trait(_, ref substs_f, _, _, _) => (*substs_f).clone(),
481+
ty::ty_trait(ref inner) => inner.substs.clone(),
482482
_ => fail!("t_f should be a trait")
483483
}
484484
})
@@ -536,8 +536,10 @@ pub fn early_resolve_expr(ex: &ast::Expr, fcx: &FnCtxt, is_early: bool) {
536536
let resolve_object_cast = |src: &ast::Expr, target_ty: ty::t| {
537537
match ty::get(target_ty).sty {
538538
// Bounds of type's contents are not checked here, but in kind.rs.
539-
ty::ty_trait(target_def_id, ref target_substs, store,
540-
target_mutbl, _bounds) => {
539+
ty::ty_trait(~ty::TyTrait {
540+
def_id: target_def_id, substs: ref target_substs, store: store,
541+
mutability: target_mutbl, bounds: _bounds
542+
}) => {
541543
fn mutability_allowed(a_mutbl: ast::Mutability,
542544
b_mutbl: ast::Mutability) -> bool {
543545
a_mutbl == b_mutbl ||

src/librustc/middle/typeck/coherence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn type_is_defined_in_local_crate(original_type: t) -> bool {
106106
ty::walk_ty(original_type, |t| {
107107
match get(t).sty {
108108
ty_enum(def_id, _) |
109-
ty_trait(def_id, _, _, _, _) |
109+
ty_trait(~ty::TyTrait { def_id, .. }) |
110110
ty_struct(def_id, _) => {
111111
if def_id.krate == ast::LOCAL_CRATE {
112112
found_nominal = true;
@@ -132,7 +132,7 @@ fn get_base_type_def_id(inference_context: &InferCtxt,
132132
match get(base_type).sty {
133133
ty_enum(def_id, _) |
134134
ty_struct(def_id, _) |
135-
ty_trait(def_id, _, _, _, _) => {
135+
ty_trait(~ty::TyTrait { def_id, .. }) => {
136136
return Some(def_id);
137137
}
138138
_ => {

src/librustc/middle/typeck/infer/coercion.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ impl<'f> Coerce<'f> {
131131
});
132132
}
133133

134-
ty::ty_trait(def_id, ref substs, ty::UniqTraitStore, m, bounds) => {
134+
ty::ty_trait(~ty::TyTrait {
135+
def_id, ref substs, store: ty::UniqTraitStore, mutability: m, bounds
136+
}) => {
135137
let result = self.unpack_actual_value(a, |sty_a| {
136138
match *sty_a {
137139
ty::ty_uniq(..) => {
@@ -148,7 +150,9 @@ impl<'f> Coerce<'f> {
148150
}
149151
}
150152

151-
ty::ty_trait(def_id, ref substs, ty::RegionTraitStore(region), m, bounds) => {
153+
ty::ty_trait(~ty::TyTrait {
154+
def_id, ref substs, store: ty::RegionTraitStore(region), mutability: m, bounds
155+
}) => {
152156
let result = self.unpack_actual_value(a, |sty_a| {
153157
match *sty_a {
154158
ty::ty_rptr(..) => {
@@ -313,9 +317,9 @@ impl<'f> Coerce<'f> {
313317
let r_a = self.get_ref().infcx.next_region_var(Coercion(self.get_ref().trace));
314318

315319
let a_borrowed = match *sty_a {
316-
ty::ty_trait(did, ref substs, _, _, b) => {
317-
ty::mk_trait(tcx, did, substs.clone(),
318-
ty::RegionTraitStore(r_a), b_mutbl, b)
320+
ty::ty_trait(~ty::TyTrait { def_id, ref substs, bounds, .. }) => {
321+
ty::mk_trait(tcx, def_id, substs.clone(),
322+
ty::RegionTraitStore(r_a), b_mutbl, bounds)
319323
}
320324
_ => {
321325
return self.subtype(a, b);

src/librustc/middle/typeck/infer/combine.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -500,18 +500,18 @@ pub fn super_tys<C:Combine>(this: &C, a: ty::t, b: ty::t) -> cres<ty::t> {
500500
Ok(ty::mk_enum(tcx, a_id, substs))
501501
}
502502

503-
(&ty::ty_trait(a_id, ref a_substs, a_store, a_mutbl, a_bounds),
504-
&ty::ty_trait(b_id, ref b_substs, b_store, b_mutbl, b_bounds))
505-
if a_id == b_id && a_mutbl == b_mutbl => {
503+
(&ty::ty_trait(ref a_),
504+
&ty::ty_trait(ref b_))
505+
if a_.def_id == b_.def_id && a_.mutability == b_.mutability => {
506506
debug!("Trying to match traits {:?} and {:?}", a, b);
507-
let substs = if_ok!(this.substs(a_id, a_substs, b_substs));
508-
let s = if_ok!(this.trait_stores(ty::terr_trait, a_store, b_store));
509-
let bounds = if_ok!(this.bounds(a_bounds, b_bounds));
507+
let substs = if_ok!(this.substs(a_.def_id, &a_.substs, &b_.substs));
508+
let s = if_ok!(this.trait_stores(ty::terr_trait, a_.store, b_.store));
509+
let bounds = if_ok!(this.bounds(a_.bounds, b_.bounds));
510510
Ok(ty::mk_trait(tcx,
511-
a_id,
511+
a_.def_id,
512512
substs.clone(),
513513
s,
514-
a_mutbl,
514+
a_.mutability,
515515
bounds))
516516
}
517517

0 commit comments

Comments
 (0)