Skip to content

Rollup of 9 pull requests #35639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1403df7
Implement From for Cell, RefCell and UnsafeCell
malbarbo Aug 5, 2016
6ca9094
Add --test-threads option to test binaries
jupp0r Aug 6, 2016
9d6fa40
Add regression test for #22894.
frewsxcv Aug 8, 2016
6bc494b
Proc_macro is alive
cgswords Aug 4, 2016
16cc8a7
Implemented a smarter concatenation system that will hopefully produc…
cgswords Aug 2, 2016
045c8c8
std: Optimize panic::catch_unwind slightly
alexcrichton Jul 9, 2016
d77a136
add SetDiscriminant StatementKind to enable deaggregation of enums
scottcarr Aug 4, 2016
d099e30
Introduce `as_slice` method on `std::vec::IntoIter` struct.
frewsxcv Aug 7, 2016
01a766e
Introduce `as_mut_slice` method on `std::vec::IntoIter` struct.
frewsxcv Aug 7, 2016
f76a737
Correct span for pub_restricted field
sanxiyn Aug 8, 2016
ad247ce
Rollup merge of #35348 - scottcarr:discriminant2, r=nikomatsakis
Manishearth Aug 13, 2016
7e2fa43
Rollup merge of #35392 - malbarbo:cell-from, r=brson
Manishearth Aug 13, 2016
c105a05
Rollup merge of #35414 - jupp0r:feature/test-threads-flag, r=alexcric…
Manishearth Aug 13, 2016
78cbbd4
Rollup merge of #35444 - alexcrichton:optimize-catch-unwind, r=brson
Manishearth Aug 13, 2016
d1e286a
Rollup merge of #35447 - frewsxcv:vec-into-iter-as-slice, r=alexcrichton
Manishearth Aug 13, 2016
6e59f49
Rollup merge of #35491 - sanxiyn:pub-restricted-span, r=nikomatsakis
Manishearth Aug 13, 2016
b61adfb
Rollup merge of #35533 - frewsxcv:22984, r=brson
Manishearth Aug 13, 2016
3a86773
Rollup merge of #35538 - cgswords:libproc_macro, r=nrc
Manishearth Aug 13, 2016
cbed977
Rollup merge of #35539 - cgswords:ts_concat, r=nrc
Manishearth Aug 13, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
std: Optimize panic::catch_unwind slightly
The previous implementation of this function was overly conservative with
liberal usage of `Option` and `.unwrap()` which in theory never triggers. This
commit essentially removes the `Option`s in favor of unsafe implementations,
improving the code generation of the fast path for LLVM to see through what's
happening more clearly.

cc #34727
  • Loading branch information
alexcrichton committed Aug 11, 2016
commit 045c8c86244aa69843c7f55ec91f2330a3aaec4e
104 changes: 68 additions & 36 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ use cell::RefCell;
use fmt;
use intrinsics;
use mem;
use ptr;
use raw;
use sys_common::rwlock::RWLock;
use sys::stdio::Stderr;
use sys_common::rwlock::RWLock;
use sys_common::thread_info;
use sys_common::util;
use thread;
Expand Down Expand Up @@ -255,45 +256,76 @@ pub use realstd::rt::update_panic_count;

/// Invoke a closure, capturing the cause of an unwinding panic if one occurs.
pub unsafe fn try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<Any + Send>> {
let mut slot = None;
let mut f = Some(f);
let ret;

{
let mut to_run = || {
slot = Some(f.take().unwrap()());
};
let fnptr = get_call(&mut to_run);
let dataptr = &mut to_run as *mut _ as *mut u8;
let mut any_data = 0;
let mut any_vtable = 0;
let fnptr = mem::transmute::<fn(&mut _), fn(*mut u8)>(fnptr);
let r = __rust_maybe_catch_panic(fnptr,
dataptr,
&mut any_data,
&mut any_vtable);
if r == 0 {
ret = Ok(());
} else {
update_panic_count(-1);
ret = Err(mem::transmute(raw::TraitObject {
data: any_data as *mut _,
vtable: any_vtable as *mut _,
}));
}
struct Data<F, R> {
f: F,
r: R,
}

debug_assert!(update_panic_count(0) == 0);
return ret.map(|()| {
slot.take().unwrap()
});
// We do some sketchy operations with ownership here for the sake of
// performance. The `Data` structure is never actually fully valid, but
// instead it always contains at least one uninitialized field. We can only
// pass pointers down to `__rust_maybe_catch_panic` (can't pass objects by
// value), so we do all the ownership tracking here manully.
//
// Note that this is all invalid if any of these functions unwind, but the
// whole point of this function is to prevent that! As a result we go
// through a transition where:
//
// * First, only the closure we're going to call is initialized. The return
// value is uninitialized.
// * When we make the function call, the `do_call` function below, we take
// ownership of the function pointer, replacing it with uninitialized
// data. At this point the `Data` structure is entirely uninitialized, but
// it won't drop due to an unwind because it's owned on the other side of
// the catch panic.
// * If the closure successfully returns, we write the return value into the
// data's return slot. Note that `ptr::write` is used as it's overwriting
// uninitialized data.
// * Finally, when we come back out of the `__rust_maybe_catch_panic` we're
// in one of two states:
//
// 1. The closure didn't panic, in which case the return value was
// filled in. We have to be careful to `forget` the closure,
// however, as ownership was passed to the `do_call` function.
// 2. The closure panicked, in which case the return value wasn't
// filled in. In this case the entire `data` structure is invalid,
// so we forget the entire thing.
//
// Once we stack all that together we should have the "most efficient'
// method of calling a catch panic whilst juggling ownership.
let mut any_data = 0;
let mut any_vtable = 0;
let mut data = Data {
f: f,
r: mem::uninitialized(),
};

fn get_call<F: FnMut()>(_: &mut F) -> fn(&mut F) {
call
}
let r = __rust_maybe_catch_panic(do_call::<F, R>,
&mut data as *mut _ as *mut u8,
&mut any_data,
&mut any_vtable);

return if r == 0 {
let Data { f, r } = data;
mem::forget(f);
debug_assert!(update_panic_count(0) == 0);
Ok(r)
} else {
mem::forget(data);
update_panic_count(-1);
debug_assert!(update_panic_count(0) == 0);
Err(mem::transmute(raw::TraitObject {
data: any_data as *mut _,
vtable: any_vtable as *mut _,
}))
};

fn call<F: FnMut()>(f: &mut F) {
f()
fn do_call<F: FnOnce() -> R, R>(data: *mut u8) {
unsafe {
let data = data as *mut Data<F, R>;
let f = ptr::read(&mut (*data).f);
ptr::write(&mut (*data).r, f());
}
}
}

Expand Down