Skip to content

Commit ce3c949

Browse files
committed
Intern BareFnTys to make sty slightly smaller.
This cuts the ty_bare_fn variant to 48 bytes rather than 56. There doesn't seem to be a noticable memory usage decrease from this.
1 parent a33a7d2 commit ce3c949

File tree

21 files changed

+91
-64
lines changed

21 files changed

+91
-64
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,12 @@ fn parse_ty<'a, 'tcx>(st: &mut PState<'a, 'tcx>, conv: conv_did) -> Ty<'tcx> {
454454
}
455455
'F' => {
456456
let def_id = parse_def(st, NominalType, |x,y| conv(x,y));
457-
return ty::mk_bare_fn(st.tcx, Some(def_id), parse_bare_fn_ty(st, |x,y| conv(x,y)));
457+
return ty::mk_bare_fn(st.tcx, Some(def_id),
458+
st.tcx.mk_bare_fn(parse_bare_fn_ty(st, |x,y| conv(x,y))));
458459
}
459460
'G' => {
460-
return ty::mk_bare_fn(st.tcx, None, parse_bare_fn_ty(st, |x,y| conv(x,y)));
461+
return ty::mk_bare_fn(st.tcx, None,
462+
st.tcx.mk_bare_fn(parse_bare_fn_ty(st, |x,y| conv(x,y))));
461463
}
462464
'#' => {
463465
let pos = parse_hex(st);

src/librustc/middle/infer/coercion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
174174

175175
self.unpack_actual_value(a, |a| {
176176
match a.sty {
177-
ty::ty_bare_fn(Some(a_def_id), ref a_f) => {
177+
ty::ty_bare_fn(Some(a_def_id), a_f) => {
178178
// Function items are coercible to any closure
179179
// type; function pointers are not (that would
180180
// require double indirection).
@@ -486,7 +486,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
486486
b.repr(self.tcx()));
487487

488488
match a.sty {
489-
ty::ty_bare_fn(Some(a_def_id), ref f) => {
489+
ty::ty_bare_fn(Some(a_def_id), f) => {
490490
self.coerce_from_fn_item(a, a_def_id, f, b)
491491
}
492492
_ => {

src/librustc/middle/infer/combine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C,
568568
}
569569
}
570570

571-
(&ty::ty_bare_fn(a_opt_def_id, ref a_fty), &ty::ty_bare_fn(b_opt_def_id, ref b_fty))
571+
(&ty::ty_bare_fn(a_opt_def_id, a_fty), &ty::ty_bare_fn(b_opt_def_id, b_fty))
572572
if a_opt_def_id == b_opt_def_id =>
573573
{
574574
let fty = try!(this.bare_fn_tys(a_fty, b_fty));

src/librustc/middle/traits/select.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
806806
}
807807

808808
// provide an impl, but only for suitable `fn` pointers
809-
ty::ty_bare_fn(_, ty::BareFnTy {
809+
ty::ty_bare_fn(_, &ty::BareFnTy {
810810
unsafety: ast::Unsafety::Normal,
811811
abi: abi::Rust,
812812
sig: ty::Binder(ty::FnSig {
@@ -1549,7 +1549,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15491549

15501550
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
15511551
let sig = match self_ty.sty {
1552-
ty::ty_bare_fn(_, ty::BareFnTy {
1552+
ty::ty_bare_fn(_, &ty::BareFnTy {
15531553
unsafety: ast::Unsafety::Normal,
15541554
abi: abi::Rust,
15551555
ref sig

src/librustc/middle/ty.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ pub struct ctxt<'tcx> {
611611
/// The arena that types are allocated from.
612612
type_arena: &'tcx TypedArena<TyS<'tcx>>,
613613
substs_arena: &'tcx TypedArena<Substs<'tcx>>,
614+
bare_fn_arena: &'tcx TypedArena<BareFnTy<'tcx>>,
614615

615616
/// Specifically use a speedy hash algorithm for this hash map, it's used
616617
/// quite often.
@@ -619,6 +620,7 @@ pub struct ctxt<'tcx> {
619620
interner: RefCell<FnvHashMap<InternedTy<'tcx>, Ty<'tcx>>>,
620621
// FIXME as above, use a hashset if equivalent elements can be queried.
621622
substs_interner: RefCell<FnvHashMap<&'tcx Substs<'tcx>, &'tcx Substs<'tcx>>>,
623+
bare_fn_interner: RefCell<FnvHashMap<&'tcx BareFnTy<'tcx>, &'tcx BareFnTy<'tcx>>>,
622624

623625
pub sess: Session,
624626
pub def_map: DefMap,
@@ -1327,7 +1329,7 @@ pub enum sty<'tcx> {
13271329

13281330
// If the def-id is Some(_), then this is the type of a specific
13291331
// fn item. Otherwise, if None(_), it a fn pointer type.
1330-
ty_bare_fn(Option<DefId>, BareFnTy<'tcx>),
1332+
ty_bare_fn(Option<DefId>, &'tcx BareFnTy<'tcx>),
13311333

13321334
ty_closure(Box<ClosureTy<'tcx>>),
13331335
ty_trait(Box<TyTrait<'tcx>>),
@@ -2056,6 +2058,7 @@ impl UnboxedClosureKind {
20562058
pub fn mk_ctxt<'tcx>(s: Session,
20572059
type_arena: &'tcx TypedArena<TyS<'tcx>>,
20582060
substs_arena: &'tcx TypedArena<Substs<'tcx>>,
2061+
bare_fn_arena: &'tcx TypedArena<BareFnTy<'tcx>>,
20592062
dm: DefMap,
20602063
named_region_map: resolve_lifetime::NamedRegionMap,
20612064
map: ast_map::Map<'tcx>,
@@ -2067,8 +2070,10 @@ pub fn mk_ctxt<'tcx>(s: Session,
20672070
ctxt {
20682071
type_arena: type_arena,
20692072
substs_arena: substs_arena,
2073+
bare_fn_arena: bare_fn_arena,
20702074
interner: RefCell::new(FnvHashMap::new()),
20712075
substs_interner: RefCell::new(FnvHashMap::new()),
2076+
bare_fn_interner: RefCell::new(FnvHashMap::new()),
20722077
named_region_map: named_region_map,
20732078
item_variance_map: RefCell::new(DefIdMap::new()),
20742079
variance_computed: Cell::new(false),
@@ -2138,6 +2143,16 @@ impl<'tcx> ctxt<'tcx> {
21382143
self.substs_interner.borrow_mut().insert(substs, substs);
21392144
substs
21402145
}
2146+
2147+
pub fn mk_bare_fn(&self, bare_fn: BareFnTy<'tcx>) -> &'tcx BareFnTy<'tcx> {
2148+
if let Some(bare_fn) = self.bare_fn_interner.borrow().get(&bare_fn) {
2149+
return *bare_fn;
2150+
}
2151+
2152+
let bare_fn = self.bare_fn_arena.alloc(bare_fn);
2153+
self.bare_fn_interner.borrow_mut().insert(bare_fn, bare_fn);
2154+
bare_fn
2155+
}
21412156
}
21422157

21432158
// Interns a type/name combination, stores the resulting box in cx.interner,
@@ -2444,7 +2459,7 @@ pub fn mk_closure<'tcx>(cx: &ctxt<'tcx>, fty: ClosureTy<'tcx>) -> Ty<'tcx> {
24442459

24452460
pub fn mk_bare_fn<'tcx>(cx: &ctxt<'tcx>,
24462461
opt_def_id: Option<ast::DefId>,
2447-
fty: BareFnTy<'tcx>) -> Ty<'tcx> {
2462+
fty: &'tcx BareFnTy<'tcx>) -> Ty<'tcx> {
24482463
mk_t(cx, ty_bare_fn(opt_def_id, fty))
24492464
}
24502465

@@ -2455,15 +2470,15 @@ pub fn mk_ctor_fn<'tcx>(cx: &ctxt<'tcx>,
24552470
let input_args = input_tys.iter().map(|ty| *ty).collect();
24562471
mk_bare_fn(cx,
24572472
Some(def_id),
2458-
BareFnTy {
2473+
cx.mk_bare_fn(BareFnTy {
24592474
unsafety: ast::Unsafety::Normal,
24602475
abi: abi::Rust,
24612476
sig: ty::Binder(FnSig {
24622477
inputs: input_args,
24632478
output: ty::FnConverging(output),
24642479
variadic: false
24652480
})
2466-
})
2481+
}))
24672482
}
24682483

24692484

src/librustc_driver/driver.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ pub fn compile_input(sess: Session,
8181

8282
let type_arena = TypedArena::new();
8383
let substs_arena = TypedArena::new();
84-
let analysis = phase_3_run_analysis_passes(sess, ast_map, &type_arena, &substs_arena, id);
84+
let bare_fn_arena = TypedArena::new();
85+
let analysis = phase_3_run_analysis_passes(sess, ast_map,
86+
&type_arena, &substs_arena, &bare_fn_arena,
87+
id);
8588
phase_save_analysis(&analysis.ty_cx.sess, analysis.ty_cx.map.krate(), &analysis, outdir);
8689

8790
if log_enabled!(::log::INFO) {
@@ -345,6 +348,7 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
345348
ast_map: ast_map::Map<'tcx>,
346349
type_arena: &'tcx TypedArena<ty::TyS<'tcx>>,
347350
substs_arena: &'tcx TypedArena<subst::Substs<'tcx>>,
351+
bare_fn_arena: &'tcx TypedArena<ty::BareFnTy<'tcx>>,
348352
name: String) -> ty::CrateAnalysis<'tcx> {
349353
let time_passes = sess.time_passes();
350354
let krate = ast_map.krate();
@@ -406,6 +410,7 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
406410
let ty_cx = ty::mk_ctxt(sess,
407411
type_arena,
408412
substs_arena,
413+
bare_fn_arena,
409414
def_map,
410415
named_region_map,
411416
ast_map,

src/librustc_driver/pretty.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl PpSourceMode {
114114
ast_map: Option<ast_map::Map<'tcx>>,
115115
type_arena: &'tcx TypedArena<ty::TyS<'tcx>>,
116116
substs_arena: &'tcx TypedArena<subst::Substs<'tcx>>,
117+
bare_fn_arena: &'tcx TypedArena<ty::BareFnTy<'tcx>>,
117118
id: String,
118119
payload: B,
119120
f: F) -> A where
@@ -136,7 +137,9 @@ impl PpSourceMode {
136137
PpmTyped => {
137138
let ast_map = ast_map.expect("--pretty=typed missing ast_map");
138139
let analysis = driver::phase_3_run_analysis_passes(sess, ast_map,
139-
type_arena, substs_arena, id);
140+
type_arena, substs_arena,
141+
bare_fn_arena,
142+
id);
140143
let annotation = TypedAnnotation { analysis: analysis };
141144
f(&annotation, payload)
142145
}
@@ -513,6 +516,7 @@ pub fn pretty_print_input(sess: Session,
513516
let mut forest = ast_map::Forest::new(krate);
514517
let type_arena = TypedArena::new();
515518
let substs_arena = TypedArena::new();
519+
let bare_fn_arena = TypedArena::new();
516520

517521
let (krate, ast_map) = if compute_ast_map {
518522
let map = driver::assign_node_ids_and_map(&sess, &mut forest);
@@ -541,7 +545,8 @@ pub fn pretty_print_input(sess: Session,
541545
match (ppm, opt_uii) {
542546
(PpmSource(s), None) =>
543547
s.call_with_pp_support(
544-
sess, ast_map, &type_arena, &substs_arena, id, out, |annotation, out| {
548+
sess, ast_map, &type_arena, &substs_arena, &bare_fn_arena,
549+
id, out, |annotation, out| {
545550
debug!("pretty printing source code {}", s);
546551
let sess = annotation.sess();
547552
pprust::print_crate(sess.codemap(),
@@ -556,7 +561,8 @@ pub fn pretty_print_input(sess: Session,
556561

557562
(PpmSource(s), Some(uii)) =>
558563
s.call_with_pp_support(
559-
sess, ast_map, &type_arena, &substs_arena, id, (out,uii), |annotation, (out,uii)| {
564+
sess, ast_map, &type_arena, &substs_arena, &bare_fn_arena,
565+
id, (out,uii), |annotation, (out,uii)| {
560566
debug!("pretty printing source code {}", s);
561567
let sess = annotation.sess();
562568
let ast_map = annotation.ast_map()
@@ -600,6 +606,7 @@ pub fn pretty_print_input(sess: Session,
600606
let variants = gather_flowgraph_variants(&sess);
601607
let analysis = driver::phase_3_run_analysis_passes(sess, ast_map,
602608
&type_arena, &substs_arena,
609+
&bare_fn_arena,
603610
id);
604611
print_flowgraph(variants, analysis, code, out)
605612
}

src/librustc_driver/test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ fn test_env<F>(source_string: &str,
128128
let region_map = region::resolve_crate(&sess, krate);
129129
let stability_index = stability::Index::build(krate);
130130
let type_arena = TypedArena::new();
131+
let substs_arena = TypedArena::new();
132+
let bare_fn_arena = TypedArena::new();
131133
let tcx = ty::mk_ctxt(sess,
132134
&type_arena,
135+
&substs_arena,
136+
&bare_fn_arena,
133137
def_map,
134138
named_region_map,
135139
ast_map,
@@ -816,4 +820,3 @@ fn subst_region_renumber_region() {
816820
assert_eq!(t_substituted, t_expected);
817821
})
818822
}
819-

src/librustc_trans/trans/callee.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
279279
let (opt_def_id, input_tys, output_ty) =
280280
match bare_fn_ty.sty {
281281
ty::ty_bare_fn(opt_def_id,
282-
ty::BareFnTy { unsafety: ast::Unsafety::Normal,
282+
&ty::BareFnTy { unsafety: ast::Unsafety::Normal,
283283
abi: synabi::Rust,
284284
sig: ty::Binder(ty::FnSig { inputs: ref input_tys,
285285
output: output_ty,
@@ -296,14 +296,15 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>(
296296
let tuple_input_ty = ty::mk_tup(tcx, input_tys.to_vec());
297297
let tuple_fn_ty = ty::mk_bare_fn(tcx,
298298
opt_def_id,
299-
ty::BareFnTy { unsafety: ast::Unsafety::Normal,
300-
abi: synabi::RustCall,
301-
sig: ty::Binder(ty::FnSig {
302-
inputs: vec![bare_fn_ty_ref,
303-
tuple_input_ty],
304-
output: output_ty,
305-
variadic: false
306-
})});
299+
tcx.mk_bare_fn(ty::BareFnTy {
300+
unsafety: ast::Unsafety::Normal,
301+
abi: synabi::RustCall,
302+
sig: ty::FnSig {
303+
inputs: vec![bare_fn_ty_ref,
304+
tuple_input_ty],
305+
output: output_ty,
306+
variadic: false
307+
}}));
307308
debug!("tuple_fn_ty: {}", tuple_fn_ty.repr(tcx));
308309

309310
//

src/librustc_trans/trans/debuginfo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl<'tcx> TypeMap<'tcx> {
430430
trait_data.principal.substs(),
431431
&mut unique_type_id);
432432
},
433-
ty::ty_bare_fn(_, ty::BareFnTy{ unsafety, abi, ref sig } ) => {
433+
ty::ty_bare_fn(_, &ty::BareFnTy{ unsafety, abi, ref sig } ) => {
434434
if unsafety == ast::Unsafety::Unsafe {
435435
unique_type_id.push_str("unsafe ");
436436
}
@@ -3819,7 +3819,7 @@ fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
38193819
push_item_name(cx, trait_data.principal.def_id(), false, output);
38203820
push_type_params(cx, trait_data.principal.substs(), output);
38213821
},
3822-
ty::ty_bare_fn(_, ty::BareFnTy{ unsafety, abi, ref sig } ) => {
3822+
ty::ty_bare_fn(_, &ty::BareFnTy{ unsafety, abi, ref sig } ) => {
38233823
if unsafety == ast::Unsafety::Unsafe {
38243824
output.push_str("unsafe ");
38253825
}

0 commit comments

Comments
 (0)