mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-01 18:01:06 +00:00
Merge pull request #880 from alexcrichton/better-link-mem-intrinsics
Improve `link_mem_intrinsics` hack
This commit is contained in:
commit
2c62795a8d
@ -451,9 +451,6 @@ impl TryToTokens for ast::Export {
|
|||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
||||||
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
pub extern fn #generated_name(#(#args),*) #ret_ty {
|
||||||
// See definition of `link_mem_intrinsics` for what this is doing
|
|
||||||
::wasm_bindgen::__rt::link_mem_intrinsics();
|
|
||||||
|
|
||||||
// Scope all local variables to be destroyed after we call the
|
// Scope all local variables to be destroyed after we call the
|
||||||
// function to ensure that `#convert_ret`, if it panics, doesn't
|
// function to ensure that `#convert_ret`, if it panics, doesn't
|
||||||
// leak anything.
|
// leak anything.
|
||||||
@ -887,8 +884,6 @@ impl TryToTokens for ast::ImportFunction {
|
|||||||
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
||||||
#[doc = #doc_comment]
|
#[doc = #doc_comment]
|
||||||
#vis fn #rust_name(#me #(#arguments),*) #ret {
|
#vis fn #rust_name(#me #(#arguments),*) #ret {
|
||||||
// See definition of `link_mem_intrinsics` for what this is doing
|
|
||||||
::wasm_bindgen::__rt::link_mem_intrinsics();
|
|
||||||
#[link(wasm_import_module = "__wbindgen_placeholder__")]
|
#[link(wasm_import_module = "__wbindgen_placeholder__")]
|
||||||
extern {
|
extern {
|
||||||
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
|
fn #import_name(#(#abi_arguments),*) -> #abi_ret;
|
||||||
@ -1256,6 +1251,8 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
|
|||||||
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
||||||
pub extern fn #name() {
|
pub extern fn #name() {
|
||||||
use wasm_bindgen::describe::*;
|
use wasm_bindgen::describe::*;
|
||||||
|
// See definition of `link_mem_intrinsics` for what this is doing
|
||||||
|
::wasm_bindgen::__rt::link_mem_intrinsics();
|
||||||
#inner
|
#inner
|
||||||
}
|
}
|
||||||
}).to_tokens(tokens);
|
}).to_tokens(tokens);
|
||||||
|
@ -15,11 +15,11 @@ Currently this code...
|
|||||||
{{#include ../../../examples/add/src/lib.rs}}
|
{{#include ../../../examples/add/src/lib.rs}}
|
||||||
```
|
```
|
||||||
|
|
||||||
generates a 723 byte wasm binary:
|
generates a 710 byte wasm binary:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ ls -l add_bg.wasm
|
$ ls -l add_bg.wasm
|
||||||
-rw-rw-r-- 1 alex alex 723 Sep 19 17:32 add_bg.wasm
|
-rw-rw-r-- 1 alex alex 710 Sep 19 17:32 add_bg.wasm
|
||||||
```
|
```
|
||||||
|
|
||||||
If you run [wasm-opt], a C++ tool for optimize WebAssembly, you can make it
|
If you run [wasm-opt], a C++ tool for optimize WebAssembly, you can make it
|
||||||
@ -28,7 +28,7 @@ even smaller too!
|
|||||||
```
|
```
|
||||||
$ wasm-opt -Os add_bg.wasm -o add.wasm
|
$ wasm-opt -Os add_bg.wasm -o add.wasm
|
||||||
$ ls -l add.wasm
|
$ ls -l add.wasm
|
||||||
-rw-rw-r-- 1 alex alex 182 Sep 19 17:33 add.wasm
|
-rw-rw-r-- 1 alex alex 172 Sep 19 17:33 add.wasm
|
||||||
```
|
```
|
||||||
|
|
||||||
And sure enough, using the [wasm2wat] tool it's quite small!
|
And sure enough, using the [wasm2wat] tool it's quite small!
|
||||||
@ -38,9 +38,6 @@ $ wasm2wat add.wasm
|
|||||||
(module
|
(module
|
||||||
(type (;0;) (func (param i32 i32) (result i32)))
|
(type (;0;) (func (param i32 i32) (result i32)))
|
||||||
(func (;0;) (type 0) (param i32 i32) (result i32)
|
(func (;0;) (type 0) (param i32 i32) (result i32)
|
||||||
i32.const 1048640
|
|
||||||
i32.const 0
|
|
||||||
i32.store
|
|
||||||
get_local 1
|
get_local 1
|
||||||
get_local 0
|
get_local 0
|
||||||
i32.add)
|
i32.add)
|
||||||
|
14
src/lib.rs
14
src/lib.rs
@ -846,22 +846,8 @@ pub mod __rt {
|
|||||||
/// above. That means if this function is called and referenced we'll pull
|
/// above. That means if this function is called and referenced we'll pull
|
||||||
/// in the object file and link the intrinsics.
|
/// in the object file and link the intrinsics.
|
||||||
///
|
///
|
||||||
/// Note that this is an `#[inline]` function to remove the function call
|
|
||||||
/// overhead we inject in functions, but right now it's unclear how to do
|
|
||||||
/// this in a zero-cost fashion. The lowest cost seems to be generating a
|
|
||||||
/// store that can't be optimized away (to a global), which is listed below.
|
|
||||||
///
|
|
||||||
/// Ideas for how to improve this are most welcome!
|
/// Ideas for how to improve this are most welcome!
|
||||||
#[inline]
|
|
||||||
pub fn link_mem_intrinsics() {
|
pub fn link_mem_intrinsics() {
|
||||||
// the above symbols only exist with the `std` feature enabled.
|
|
||||||
if !cfg!(feature = "std") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
use core::sync::atomic::*;
|
|
||||||
static FOO: AtomicUsize = ATOMIC_USIZE_INIT;
|
|
||||||
FOO.store(0, Ordering::SeqCst);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user