From 4436c0eae6bacac66e456ebac45b150c080df1cf Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 21 Apr 2018 13:14:33 -0700 Subject: [PATCH] Avoid invoking a function pointer with JsStatic The previous codegen wasn't enough to convince LLVM that the function pointer was a constant value and could be aggressively inlined, so this updates the `JsStatic` internals slightly to guarantee to LLVM that the function pointer is constant and no dynamic dispatch is needed after all --- crates/backend/src/codegen.rs | 4 +++- src/lib.rs | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index a91c44df9..0377db161 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -779,8 +779,10 @@ impl ToTokens for ast::ImportStatic { } } + static mut _VAL: ::wasm_bindgen::__rt::core::cell::UnsafeCell> = + ::wasm_bindgen::__rt::core::cell::UnsafeCell::new(None); ::wasm_bindgen::JsStatic { - __inner: ::wasm_bindgen::__rt::core::cell::UnsafeCell::new(None), + __inner: unsafe { &_VAL }, __init: init, } }; diff --git a/src/lib.rs b/src/lib.rs index 85667269e..cb3321cc6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -303,9 +303,9 @@ impl Drop for JsValue { /// /// This type implements `Deref` to the inner type so it's typically used as if /// it were `&T`. -pub struct JsStatic { +pub struct JsStatic { #[doc(hidden)] - pub __inner: UnsafeCell>, + pub __inner: &'static UnsafeCell>, #[doc(hidden)] pub __init: fn() -> T, }