perf(plugin/runner): Optimize write_into_memory_view (#3797)

This commit is contained in:
OJ Kwon 2022-03-01 00:20:56 -08:00 committed by GitHub
parent c8a605c8a0
commit 7fd0eeecb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 150 additions and 32 deletions

View File

@ -15,6 +15,7 @@ use wasmer_wasi::{is_wasi_module, WasiState};
pub mod cache;
#[tracing::instrument(level = "trace", skip_all)]
fn copy_bytes_into_host(memory: &Memory, bytes_ptr: i32, bytes_ptr_len: i32) -> Vec<u8> {
let ptr: WasmPtr<u8, Array> = WasmPtr::new(bytes_ptr as _);
@ -32,6 +33,7 @@ fn copy_bytes_into_host(memory: &Memory, bytes_ptr: i32, bytes_ptr_len: i32) ->
}
/// Locate a view from given memory, write serialized bytes into.
#[tracing::instrument(level = "trace", skip_all)]
fn write_into_memory_view<F>(
memory: &Memory,
serialized_bytes: &Serialized,
@ -47,17 +49,23 @@ where
let ptr_start_size = ptr_start
.try_into()
.expect("Should be able to convert to usize");
let serialized_len_size: u32 = serialized_len
.try_into()
.expect("Should be able to convert to u32");
// Note: it's important to get a view from memory _after_ alloc completes
let view = memory.view::<u8>();
// loop over the Wasm memory view's bytes, assign bytes value of alignedvec from
// serialized
for (cell, byte) in view[ptr_start_size..ptr_start_size + serialized_len + 1]
.iter()
.zip(serialized.iter())
{
cell.set(*byte)
// Get a subarray for current memoryview starting from ptr address we just
// allocated above, perform copying into specified ptr. Wasm's memory layout
// is linear and we have atomic gaurantee by not having any thread access,
// so can safely get subarray from allocated ptr address.
//
// If we want safer operation instead, refer previous implementation
// https://github.com/swc-project/swc/blob/1ef8f3749b6454eb7d40a36a5f9366137fa97928/crates/swc_plugin_runner/src/lib.rs#L56-L61
unsafe {
view.subarray(ptr_start_size, ptr_start_size + serialized_len_size)
.copy_from(serialized);
}
(

View File

@ -38,6 +38,12 @@ version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3"
[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "ast_node"
version = "0.7.6"
@ -161,6 +167,16 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "enum_kind"
version = "0.2.1"
dependencies = [
"pmutil",
"proc-macro2",
"swc_macros_common",
"syn",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -235,6 +251,16 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indexmap"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -263,6 +289,29 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lexical"
version = "5.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f404a90a744e32e8be729034fc33b90cf2a56418fbf594d69aa3c0214ad414e5"
dependencies = [
"cfg-if 1.0.0",
"lexical-core",
]
[[package]]
name = "lexical-core"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe"
dependencies = [
"arrayvec",
"bitflags",
"cfg-if 1.0.0",
"ryu",
"static_assertions",
]
[[package]]
name = "libc"
version = "0.2.112"
@ -333,15 +382,6 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
[[package]]
name = "owning_ref"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ff55baddef9e4ad00f88b6c743a2a8062d4c6ade126c2a528644b8e444d52ce"
dependencies = [
"stable_deref_trait",
]
[[package]]
name = "parking_lot"
version = "0.11.1"
@ -583,6 +623,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "scoped-tls"
version = "1.0.0"
@ -634,10 +680,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309"
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "string_cache"
@ -692,7 +738,7 @@ dependencies = [
[[package]]
name = "swc_common"
version = "0.17.7"
version = "0.17.9"
dependencies = [
"ahash",
"anyhow",
@ -704,7 +750,6 @@ dependencies = [
"from_variant",
"num-bigint",
"once_cell",
"owning_ref",
"rkyv",
"rustc-hash",
"serde",
@ -719,7 +764,7 @@ dependencies = [
[[package]]
name = "swc_ecma_ast"
version = "0.65.4"
version = "0.68.3"
dependencies = [
"is-macro",
"num-bigint",
@ -731,9 +776,67 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "swc_ecma_parser"
version = "0.91.12"
dependencies = [
"either",
"enum_kind",
"lexical",
"num-bigint",
"serde",
"smallvec",
"swc_atoms",
"swc_common",
"swc_ecma_ast",
"tracing",
"typed-arena",
"unicode-xid",
]
[[package]]
name = "swc_ecma_quote"
version = "0.1.2"
dependencies = [
"swc_atoms",
"swc_common",
"swc_ecma_ast",
"swc_ecma_quote_macros",
"swc_ecma_utils",
]
[[package]]
name = "swc_ecma_quote_macros"
version = "0.1.0"
dependencies = [
"anyhow",
"pmutil",
"proc-macro2",
"quote",
"swc_atoms",
"swc_common",
"swc_ecma_ast",
"swc_ecma_parser",
"swc_macros_common",
"syn",
]
[[package]]
name = "swc_ecma_utils"
version = "0.69.0"
dependencies = [
"indexmap",
"once_cell",
"swc_atoms",
"swc_common",
"swc_ecma_ast",
"swc_ecma_visit",
"tracing",
]
[[package]]
name = "swc_ecma_visit"
version = "0.51.1"
version = "0.54.0"
dependencies = [
"num-bigint",
"swc_atoms",
@ -773,18 +876,19 @@ dependencies = [
[[package]]
name = "swc_plugin"
version = "0.27.1"
version = "0.30.0"
dependencies = [
"swc_atoms",
"swc_common",
"swc_ecma_ast",
"swc_ecma_quote",
"swc_ecma_visit",
"swc_plugin_macro",
]
[[package]]
name = "swc_plugin_macro"
version = "0.3.0"
version = "0.3.1"
dependencies = [
"proc-macro2",
"quote",
@ -801,7 +905,7 @@ dependencies = [
[[package]]
name = "swc_visit_macros"
version = "0.3.0"
version = "0.3.1"
dependencies = [
"Inflector",
"pmutil",
@ -839,9 +943,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "tracing"
version = "0.1.29"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105"
checksum = "f6c650a8ef0cd2dd93736f033d21cbd1224c5a967aa0c258d00fcf7dafef9b9f"
dependencies = [
"cfg-if 1.0.0",
"pin-project-lite",
@ -851,9 +955,9 @@ dependencies = [
[[package]]
name = "tracing-attributes"
version = "0.1.18"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e"
checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716"
dependencies = [
"proc-macro2",
"quote",
@ -862,13 +966,19 @@ dependencies = [
[[package]]
name = "tracing-core"
version = "0.1.21"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4"
checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23"
dependencies = [
"lazy_static",
]
[[package]]
name = "typed-arena"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0685c84d5d54d1c26f7d3eb96cd41550adb97baed141a761cf335d3d33bcd0ae"
[[package]]
name = "unicode-bidi"
version = "0.3.7"