Skip to content

Commit

Permalink
fix(es/plugin): Disable SIMD (#6163)
Browse files Browse the repository at this point in the history
**Description:**

This PR disables SIMD for all hosts because wasmer cache is not portable.
  • Loading branch information
kdy1 committed Oct 15, 2022
1 parent 608aa94 commit 899021e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Expand Up @@ -4,6 +4,9 @@
rustdocflags = ["--cfg", "docsrs"]
rustflags = []

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-feature=+sse2"]

[target.aarch64-apple-darwin]
rustflags = []

Expand Down
55 changes: 46 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions crates/swc_plugin_runner/Cargo.toml
Expand Up @@ -38,6 +38,7 @@ rkyv-impl = [

[dependencies]
anyhow = "1.0.42"
enumset = "1.0.12"
once_cell = "1.10.0"
parking_lot = "0.12.0"
serde = { version = "1.0.126", features = ["derive"] }
Expand All @@ -48,9 +49,11 @@ swc_common = { version = "0.29.8", path = "../swc_common", features = [
swc_ecma_ast = { version = "0.94.11", path = "../swc_ecma_ast" }
swc_plugin_proxy = { version = "0.22.11", path = "../swc_plugin_proxy" }

tracing = "0.1.32"
wasmer = { version = "2.3.0", default-features = false }
wasmer-wasi = { version = "2.3.0", default-features = false }
tracing = "0.1.32"
wasmer = { version = "2.3.0", default-features = false }
wasmer-compiler-cranelift = "2.3.0"
wasmer-engine-universal = "2.3.0"
wasmer-wasi = { version = "2.3.0", default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wasmer-cache = { version = "2.3.0", optional = true }
Expand Down
26 changes: 22 additions & 4 deletions crates/swc_plugin_runner/src/cache.rs
Expand Up @@ -4,12 +4,13 @@ use std::{
};

use anyhow::{Context, Error};
use enumset::EnumSet;
use parking_lot::Mutex;
use swc_common::{
collections::AHashMap,
sync::{Lazy, OnceCell},
};
use wasmer::{Module, Store};
use wasmer::{BaseTunables, CpuFeature, Engine, Module, Store, Target, Triple};
#[cfg(all(not(target_arch = "wasm32"), feature = "filesystem_cache"))]
use wasmer_cache::{Cache as WasmerCache, FileSystemCache, Hash};

Expand All @@ -30,7 +31,7 @@ compile_error!(
/// however it is not gauranteed to be compatible across wasmer's
/// internal changes.
/// https://github.com/wasmerio/wasmer/issues/2781
const MODULE_SERIALIZATION_VERSION: &str = "v3";
const MODULE_SERIALIZATION_VERSION: &str = "v4";

/// A shared instance to plugin's module bytecode cache.
pub static PLUGIN_MODULE_CACHE: Lazy<PluginModuleCache> = Lazy::new(Default::default);
Expand Down Expand Up @@ -149,7 +150,7 @@ impl PluginModuleCache {
std::fs::read(&binary_path).context("Cannot read plugin from specified path")?;
let module_bytes_hash = Hash::generate(&module_bytes);

let wasmer_store = Store::default();
let wasmer_store = new_store();

let load_cold_wasm_bytes = || {
let span = tracing::span!(
Expand Down Expand Up @@ -205,7 +206,7 @@ impl PluginModuleCache {
//TODO: In native runtime we have to reconstruct module using raw bytes in
// memory cache. requires https://github.com/wasmerio/wasmer/pull/2821

let wasmer_store = Store::default();
let wasmer_store = new_store();
let module = Module::new(&wasmer_store, in_memory_module_bytes)?;

Ok(module)
Expand Down Expand Up @@ -234,3 +235,20 @@ impl PluginModuleCache {
}
}
}

/// Creates an instnace of [Store].
///
/// This function exists because we need to disable simd.
fn new_store() -> Store {
// Use empty enumset to disable simd.
let mut set = EnumSet::new();
set.insert(CpuFeature::SSE2);
let target = Target::new(Triple::host(), set);

let config = wasmer_compiler_cranelift::Cranelift::default();
let engine = wasmer_engine_universal::Universal::new(config)
.target(target)
.engine();
let tunables = BaseTunables::for_target(engine.target());
Store::new_with_tunables(&engine, tunables)
}

1 comment on commit 899021e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 899021e Previous: 6d0ca05 Ratio
es/full/minify/libraries/antd 1834331506 ns/iter (± 39885588) 1824713527 ns/iter (± 33930458) 1.01
es/full/minify/libraries/d3 418094059 ns/iter (± 14690679) 375450355 ns/iter (± 23970141) 1.11
es/full/minify/libraries/echarts 1530692132 ns/iter (± 29775002) 1516119864 ns/iter (± 36719876) 1.01
es/full/minify/libraries/jquery 103102409 ns/iter (± 4695676) 100387846 ns/iter (± 6271039) 1.03
es/full/minify/libraries/lodash 117076105 ns/iter (± 6573008) 112000053 ns/iter (± 9631144) 1.05
es/full/minify/libraries/moment 61189968 ns/iter (± 3739323) 56862192 ns/iter (± 4177587) 1.08
es/full/minify/libraries/react 20542165 ns/iter (± 2542483) 20054124 ns/iter (± 371796) 1.02
es/full/minify/libraries/terser 347706361 ns/iter (± 24249666) 290622064 ns/iter (± 8349644) 1.20
es/full/minify/libraries/three 609781058 ns/iter (± 80759084) 547069130 ns/iter (± 10271462) 1.11
es/full/minify/libraries/typescript 3542738000 ns/iter (± 116026114) 3421326158 ns/iter (± 138630235) 1.04
es/full/minify/libraries/victory 842689386 ns/iter (± 58118087) 799316171 ns/iter (± 15666128) 1.05
es/full/minify/libraries/vue 162035246 ns/iter (± 32361176) 137618887 ns/iter (± 4171876) 1.18
es/full/codegen/es3 34212 ns/iter (± 655) 32514 ns/iter (± 1684) 1.05
es/full/codegen/es5 33959 ns/iter (± 1318) 32256 ns/iter (± 1748) 1.05
es/full/codegen/es2015 34331 ns/iter (± 1718) 33969 ns/iter (± 3145) 1.01
es/full/codegen/es2016 34011 ns/iter (± 800) 34535 ns/iter (± 5596) 0.98
es/full/codegen/es2017 34160 ns/iter (± 958) 34098 ns/iter (± 3453) 1.00
es/full/codegen/es2018 34482 ns/iter (± 1860) 34282 ns/iter (± 5135) 1.01
es/full/codegen/es2019 34339 ns/iter (± 2857) 34493 ns/iter (± 5428) 1.00
es/full/codegen/es2020 34066 ns/iter (± 1591) 33362 ns/iter (± 1990) 1.02
es/full/all/es3 203693619 ns/iter (± 15604482) 196293778 ns/iter (± 14848229) 1.04
es/full/all/es5 198572596 ns/iter (± 16426858) 185118336 ns/iter (± 11718494) 1.07
es/full/all/es2015 158778254 ns/iter (± 10797416) 150730756 ns/iter (± 9697185) 1.05
es/full/all/es2016 158913360 ns/iter (± 15748840) 147010672 ns/iter (± 15527691) 1.08
es/full/all/es2017 160350772 ns/iter (± 15680348) 149776392 ns/iter (± 11107354) 1.07
es/full/all/es2018 155373213 ns/iter (± 11634451) 147581527 ns/iter (± 10105399) 1.05
es/full/all/es2019 154751170 ns/iter (± 11488696) 149861358 ns/iter (± 12082701) 1.03
es/full/all/es2020 149262786 ns/iter (± 10724500) 141100566 ns/iter (± 10257657) 1.06
es/full/parser 733323 ns/iter (± 44394) 722510 ns/iter (± 44273) 1.01
es/full/base/fixer 27213 ns/iter (± 1544) 26915 ns/iter (± 1569) 1.01
es/full/base/resolver_and_hygiene 94711 ns/iter (± 3773) 97704 ns/iter (± 11847) 0.97
serialization of ast node 209 ns/iter (± 15) 213 ns/iter (± 42) 0.98
serialization of serde 210 ns/iter (± 3) 214 ns/iter (± 30) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.