Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2a5f3ee
Make --emit=metadata output metadata regardless of link
varkor Mar 22, 2018
8056506
Special-case OutputType::Metadata
varkor Mar 31, 2018
7575d96
Reformat trans skip condition
varkor Apr 10, 2018
7d93920
Implement Chalk lowering rule Normalize-From-Impl
fanzier Apr 3, 2018
0bf3eb6
Improve function name.
fanzier Apr 7, 2018
7e50039
Rebase and update code.
fanzier Apr 11, 2018
34956c8
Don't inject clippy into rls on stable/beta
oli-obk Apr 11, 2018
0b393e0
Ignore copyright year when generating deriving span tests
varkor Apr 11, 2018
2ef8493
Stop duplicating where clauses from impl's.
fanzier Apr 11, 2018
ec3bccb
core: Remove panics from some `Layout` methods
alexcrichton Apr 11, 2018
d554f4c
Add #[no_debug] to trans_fn_attrs() query.
michaelwoerister Apr 12, 2018
f78b8c0
Support #[no_debug] for global variables.
michaelwoerister Apr 12, 2018
f711c05
Clean up attribute handling in create_function_debug_context().
michaelwoerister Apr 12, 2018
f1610ae
Use #[no_debug] to work around LLVM problem with rustc_driver::get_tr…
michaelwoerister Apr 12, 2018
6f10146
Fix test failure in src/tools/rustdoc-themes when rust.rpath = false
chrisccoulson Apr 12, 2018
de34533
Add check builder for Windows to Travis
Mark-Simulacrum Apr 11, 2018
807c1a0
Make OnDiskCache thread-safer
Zoxc Feb 15, 2018
2f60341
improve Atomic*::fetch_update docs
llogiq Apr 12, 2018
35087fc
Remove -Z miri debugging option
f-bro Apr 13, 2018
976a17b
Rollup merge of #49396 - Zoxc:sync-on-disk-cache, r=michaelwoerister
kennytm Apr 13, 2018
509ccd6
Rollup merge of #49626 - fanzier:chalk-lowering, r=scalexm
kennytm Apr 13, 2018
f4ac852
Rollup merge of #49866 - Mark-Simulacrum:pr-travis-windows, r=alexcri…
kennytm Apr 13, 2018
9dbfcb5
Rollup merge of #49876 - oli-obk:no_secret_clippy_on_stable_☹, r=nrc
kennytm Apr 13, 2018
09c321c
Rollup merge of #49884 - alexcrichton:less-unwrap, r=Mark-Simulacrum
kennytm Apr 13, 2018
28bf5a3
Rollup merge of #49886 - varkor:generate-deriving-span-tests-usabilit…
kennytm Apr 13, 2018
451cf7c
Rollup merge of #49904 - michaelwoerister:no-debug-attr, r=alexcrichton
kennytm Apr 13, 2018
0bf5af0
Rollup merge of #49908 - chrisccoulson:fix-rustdoc-themes-test-withou…
kennytm Apr 13, 2018
5d59264
Rollup merge of #49922 - f-bro:zmiri, r=oli-obk
kennytm Apr 13, 2018
ba8be1d
Rollup merge of #49916 - llogiq:doc-atomic-fetch-update, r=kennytm
kennytm Apr 13, 2018
f1da9f4
Rollup merge of #49289 - varkor:emit-metadata-without-link, r=michael…
kennytm Apr 13, 2018
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
core: Remove panics from some Layout methods
`Layout` is often used at the core of allocation APIs and is as a result pretty
sensitive to codegen in various circumstances. I was profiling `-C opt-level=z`
with a wasm project recently and noticed that the `unwrap()` wasn't removed
inside of `Layout`, causing the program to be much larger than it otherwise
would be. If inlining were more aggressive LLVM would have figured out that the
panic could be eliminated, but in general the methods here can't panic in the
first place!

As a result this commit makes the following tweaks:

* Removes `unwrap()` and replaces it with `unsafe` in `Layout::new` and
  `Layout::for_value`. For posterity though a debug assertion was left behind.
* Removes an `unwrap()` in favor of `?` in the `repeat` method. The comment
  indicating that the function call couldn't panic wasn't quite right in that if
  `alloc_size` becomes too large and if `align` is high enough it could indeed
  cause a panic.

This'll hopefully mean that panics never get introduced into code in the first
place, ensuring that `opt-level=z` is closer to `opt-level=s` in this regard.
  • Loading branch information
alexcrichton committed Apr 12, 2018
commit ec3bccb69f1d9725a8bd83ce69739d3bad0a76cf
22 changes: 14 additions & 8 deletions src/libcore/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,26 @@ impl Layout {
/// Constructs a `Layout` suitable for holding a value of type `T`.
pub fn new<T>() -> Self {
let (size, align) = size_align::<T>();
Layout::from_size_align(size, align).unwrap()
// Note that the align is guaranteed by rustc to be a power of two and
// the size+align combo is guaranteed to fit in our address space. As a
// result use the unchecked constructor here to avoid inserting code
// that panics if it isn't optimized well enough.
debug_assert!(Layout::from_size_align(size, align).is_some());
unsafe {
Layout::from_size_align_unchecked(size, align)
}
}

/// Produces layout describing a record that could be used to
/// allocate backing structure for `T` (which could be a trait
/// or other unsized type like a slice).
pub fn for_value<T: ?Sized>(t: &T) -> Self {
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
Layout::from_size_align(size, align).unwrap()
// See rationale in `new` for why this us using an unsafe variant below
debug_assert!(Layout::from_size_align(size, align).is_some());
unsafe {
Layout::from_size_align_unchecked(size, align)
}
}

/// Creates a layout describing the record that can hold a value
Expand Down Expand Up @@ -212,12 +223,7 @@ impl Layout {
pub fn repeat(&self, n: usize) -> Option<(Self, usize)> {
let padded_size = self.size.checked_add(self.padding_needed_for(self.align))?;
let alloc_size = padded_size.checked_mul(n)?;

// We can assume that `self.align` is a power-of-two.
// Furthermore, `alloc_size` has already been rounded up
// to a multiple of `self.align`; therefore, the call to
// `Layout::from_size_align` below should never panic.
Some((Layout::from_size_align(alloc_size, self.align).unwrap(), padded_size))
Some((Layout::from_size_align(alloc_size, self.align)?, padded_size))
}

/// Creates a layout describing the record for `self` followed by
Expand Down