]> git.lizzy.rs Git - rust.git/commit
core: Remove panics from some `Layout` methods
authorAlex Crichton <alex@alexcrichton.com>
Wed, 11 Apr 2018 17:47:16 +0000 (10:47 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 13 Apr 2018 14:02:10 +0000 (07:02 -0700)
commit68e555b7d03eefe4a226e6a0ae3fd13a118cb27e
treec88c9dd1a7fcbb3beca4b33bd402916d157f419a
parent99d4886ead646da864cff7963f540a44acd4af05
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.
src/libcore/alloc.rs