Skip to content

Commit 252d3da

Browse files
committed
Auto merge of #41469 - arielb1:rustc-spring-cleaning, r=eddyb
Performance audit, Spring 2017 Fix up some quite important performance "surprises" I've found running callgrind on rustc.
2 parents cb4065b + a660ad8 commit 252d3da

Some content is hidden

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

47 files changed

+322
-185
lines changed

src/liballoc/heap.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
issue = "27700")]
1717

1818
use core::{isize, usize};
19-
#[cfg(not(test))]
2019
use core::intrinsics::{min_align_of_val, size_of_val};
2120

2221
#[allow(improper_ctypes)]
@@ -158,10 +157,9 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
158157
}
159158
}
160159

161-
#[cfg(not(test))]
162-
#[lang = "box_free"]
160+
#[cfg_attr(not(test), lang = "box_free")]
163161
#[inline]
164-
unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
162+
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
165163
let size = size_of_val(&*ptr);
166164
let align = min_align_of_val(&*ptr);
167165
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.

src/liballoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#![feature(needs_allocator)]
8888
#![feature(optin_builtin_traits)]
8989
#![feature(placement_in_syntax)]
90+
#![cfg_attr(stage0, feature(pub_restricted))]
9091
#![feature(shared)]
9192
#![feature(staged_api)]
9293
#![feature(unboxed_closures)]

src/liballoc/rc.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ use core::ops::CoerceUnsized;
239239
use core::ptr::{self, Shared};
240240
use core::convert::From;
241241

242-
use heap::deallocate;
242+
use heap::{allocate, deallocate, box_free};
243243
use raw_vec::RawVec;
244244

245245
struct RcBox<T: ?Sized> {
@@ -248,7 +248,6 @@ struct RcBox<T: ?Sized> {
248248
value: T,
249249
}
250250

251-
252251
/// A single-threaded reference-counting pointer.
253252
///
254253
/// See the [module-level documentation](./index.html) for more details.
@@ -438,6 +437,38 @@ impl Rc<str> {
438437
}
439438
}
440439

440+
impl<T> Rc<[T]> {
441+
/// Constructs a new `Rc<[T]>` from a `Box<[T]>`.
442+
#[doc(hidden)]
443+
#[unstable(feature = "rustc_private",
444+
reason = "for internal use in rustc",
445+
issue = "0")]
446+
pub fn __from_array(value: Box<[T]>) -> Rc<[T]> {
447+
unsafe {
448+
let ptr: *mut RcBox<[T]> =
449+
mem::transmute([mem::align_of::<RcBox<[T; 1]>>(), value.len()]);
450+
// FIXME(custom-DST): creating this invalid &[T] is dubiously defined,
451+
// we should have a better way of getting the size/align
452+
// of a DST from its unsized part.
453+
let ptr = allocate(size_of_val(&*ptr), align_of_val(&*ptr));
454+
let ptr: *mut RcBox<[T]> = mem::transmute([ptr as usize, value.len()]);
455+
456+
// Initialize the new RcBox.
457+
ptr::write(&mut (*ptr).strong, Cell::new(1));
458+
ptr::write(&mut (*ptr).weak, Cell::new(1));
459+
ptr::copy_nonoverlapping(
460+
value.as_ptr(),
461+
&mut (*ptr).value as *mut [T] as *mut T,
462+
value.len());
463+
464+
// Free the original allocation without freeing its (moved) contents.
465+
box_free(Box::into_raw(value));
466+
467+
Rc { ptr: Shared::new(ptr as *const _) }
468+
}
469+
}
470+
}
471+
441472
impl<T: ?Sized> Rc<T> {
442473
/// Creates a new [`Weak`][weak] pointer to this value.
443474
///

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
9999
ty::ReEmpty |
100100
ty::ReErased => {
101101
// replace all free regions with 'erased
102-
self.tcx().mk_region(ty::ReErased)
102+
self.tcx().types.re_erased
103103
}
104104
}
105105
}

src/librustc/infer/region_inference/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
948948
} else {
949949
// otherwise, we don't know what the free region is,
950950
// so we must conservatively say the LUB is static:
951-
self.tcx.mk_region(ReStatic)
951+
self.tcx.types.re_static
952952
}
953953
}
954954

@@ -971,7 +971,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
971971
if a == b {
972972
a
973973
} else {
974-
self.tcx.mk_region(ReStatic)
974+
self.tcx.types.re_static
975975
}
976976
}
977977
}
@@ -1018,7 +1018,7 @@ impl<'a, 'gcx, 'tcx> RegionVarBindings<'a, 'gcx, 'tcx> {
10181018

10191019
fn construct_var_data(&self) -> Vec<VarValue<'tcx>> {
10201020
(0..self.num_vars() as usize)
1021-
.map(|_| Value(self.tcx.mk_region(ty::ReEmpty)))
1021+
.map(|_| Value(self.tcx.types.re_empty))
10221022
.collect()
10231023
}
10241024

@@ -1493,7 +1493,7 @@ fn lookup<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
14931493
-> &'tcx ty::Region {
14941494
match values[rid.index as usize] {
14951495
Value(r) => r,
1496-
ErrorValue => tcx.mk_region(ReStatic), // Previously reported error.
1496+
ErrorValue => tcx.types.re_static, // Previously reported error.
14971497
}
14981498
}
14991499

src/librustc/middle/cstore.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,13 @@ pub trait CrateStore {
188188
fn visibility(&self, def: DefId) -> ty::Visibility;
189189
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
190190
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
191-
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
191+
fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]>;
192192
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name>;
193193

194194
// trait info
195195
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>;
196196

197197
// impl info
198-
fn impl_polarity(&self, def: DefId) -> hir::ImplPolarity;
199198
fn impl_parent(&self, impl_def_id: DefId) -> Option<DefId>;
200199

201200
// trait/impl-item info
@@ -323,14 +322,13 @@ impl CrateStore for DummyCrateStore {
323322
}
324323
fn item_generics_cloned(&self, def: DefId) -> ty::Generics
325324
{ bug!("item_generics_cloned") }
326-
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
325+
fn item_attrs(&self, def_id: DefId) -> Rc<[ast::Attribute]> { bug!("item_attrs") }
327326
fn fn_arg_names(&self, did: DefId) -> Vec<ast::Name> { bug!("fn_arg_names") }
328327

329328
// trait info
330329
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
331330

332331
// impl info
333-
fn impl_polarity(&self, def: DefId) -> hir::ImplPolarity { bug!("impl_polarity") }
334332
fn impl_parent(&self, def: DefId) -> Option<DefId> { bug!("impl_parent") }
335333

336334
// trait/impl-item info

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
426426

427427
hir::ExprMatch(ref discr, ref arms, _) => {
428428
let discr_cmt = return_if_err!(self.mc.cat_expr(&discr));
429-
let r = self.tcx().mk_region(ty::ReEmpty);
429+
let r = self.tcx().types.re_empty;
430430
self.borrow_expr(&discr, r, ty::ImmBorrow, MatchDiscriminant);
431431

432432
// treatment of the discriminant is handled while walking the arms.

src/librustc/middle/lang_items.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
223223

224224
pub fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
225225
for attribute in attrs {
226-
match attribute.value_str() {
227-
Some(value) if attribute.check_name("lang") => return Some(value),
228-
_ => {}
226+
if attribute.check_name("lang") {
227+
if let Some(value) = attribute.value_str() {
228+
return Some(value)
229+
}
229230
}
230231
}
231232

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
871871
// we can promote to a constant, otherwise equal to enclosing temp
872872
// lifetime.
873873
let (re, old_re) = if promotable {
874-
(self.tcx().mk_region(ty::ReStatic),
875-
self.tcx().mk_region(ty::ReStatic))
874+
(self.tcx().types.re_static,
875+
self.tcx().types.re_static)
876876
} else {
877877
self.temporary_scope(id)
878878
};

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ fn process_predicate<'a, 'gcx, 'tcx>(
443443
// Otherwise, we have something of the form
444444
// `for<'a> T: 'a where 'a not in T`, which we can treat as `T: 'static`.
445445
Some(t_a) => {
446-
let r_static = selcx.tcx().mk_region(ty::ReStatic);
446+
let r_static = selcx.tcx().types.re_static;
447447
register_region_obligation(t_a, r_static,
448448
obligation.cause.clone(),
449449
region_obligations);

0 commit comments

Comments
 (0)