about summary refs log tree commit diff
path: root/src/libstd/thread
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-11-06 04:04:33 +0000
committerbors <bors@rust-lang.org>2018-11-06 04:04:33 +0000
commit24e66c28980442a48d9458f1a4f9b76cc722dc8a (patch)
treec1c32d1146a30ac22231e69f4e5cb67f2f9abe6d /src/libstd/thread
parent8aa926729e68ef03316e81c9309b670a25e37b48 (diff)
parent0c3d08e9676a7defd16b88307838f7294d28c3e5 (diff)
downloadrust-24e66c28980442a48d9458f1a4f9b76cc722dc8a.tar.gz
rust-24e66c28980442a48d9458f1a4f9b76cc722dc8a.zip
Auto merge of #55518 - alexcrichton:smaller-wasm, r=sfackler
std: Improve codegen size of accessing TLS

Some code in the TLS implementation in libstd stores `Some(val)` into an
`&mut Option<T>` (effectively) and then pulls out `&T`, but it currently
uses `.unwrap()` which can codegen into a panic even though it can never
panic. With sufficient optimizations enabled (like LTO) the compiler can
see through this but this commit helps it along in normal mode
(`--release` with Cargo by default) to avoid codegen'ing the panic path.

This ends up improving the optimized codegen on wasm by ensuring that a
call to panic pulling in more file size doesn't stick around.
Diffstat (limited to 'src/libstd/thread')
-rw-r--r--src/libstd/thread/local.rs11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/libstd/thread/local.rs b/src/libstd/thread/local.rs
index 59f100fad1b..ccbead7cc2f 100644
--- a/src/libstd/thread/local.rs
+++ b/src/libstd/thread/local.rs
@@ -14,6 +14,7 @@
 
 use cell::UnsafeCell;
 use fmt;
+use hint;
 use mem;
 
 /// A thread local storage key which owns its contents.
@@ -275,7 +276,15 @@ impl<T: 'static> LocalKey<T> {
         // operations a little differently and make this safe to call.
         mem::replace(&mut *ptr, Some(value));
 
-        (*ptr).as_ref().unwrap()
+        // After storing `Some` we want to get a reference to the contents of
+        // what we just stored. While we could use `unwrap` here and it should
+        // always work it empirically doesn't seem to always get optimized away,
+        // which means that using something like `try_with` can pull in
+        // panicking code and cause a large size bloat.
+        match *ptr {
+            Some(ref x) => x,
+            None => hint::unreachable_unchecked(),
+        }
     }
 
     /// Acquires a reference to the value in this TLS key.