Skip to content

Commit

Permalink
refactor(es/plugins): Simplify read_result_* (#6426)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Nov 13, 2022
1 parent e25b738 commit 7a1c2a1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 124 deletions.
2 changes: 1 addition & 1 deletion crates/swc_plugin_proxy/src/memory_interop/mod.rs
@@ -1,6 +1,6 @@
mod read_returned_result_from_host;
pub use read_returned_result_from_host::AllocatedBytesPtr;
#[cfg_attr(not(target_arch = "wasm32"), allow(unused))]
#[cfg(all(feature = "__rkyv", feature = "__plugin_mode", target_arch = "wasm32"))]
pub(crate) use read_returned_result_from_host::{
read_returned_result_from_host, read_returned_result_from_host_fallible,
};
Expand Up @@ -26,8 +26,7 @@ fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr> {
///
/// Returns a struct AllocatedBytesPtr to the ptr for actual return value if
/// host fn allocated return value, None otherwise.
#[cfg(feature = "__rkyv")]
#[cfg_attr(not(target_arch = "wasm32"), allow(unused))]
#[cfg(all(feature = "__rkyv", feature = "__plugin_mode", target_arch = "wasm32"))]
#[tracing::instrument(level = "info", skip_all)]
fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr>
where
Expand All @@ -40,17 +39,14 @@ where
let (serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr_size) =
serialized_allocated_bytes_ptr.as_ptr();

#[cfg(target_arch = "wasm32")]
{
let ret = f(serialized_allocated_bytes_raw_ptr as _);
let ret = f(serialized_allocated_bytes_raw_ptr as _);

// Host fn call completes: by contract in host proxy, if return value is 0
// we know there's no value to read. Otherwise, we know host filled in
// AllocatedBytesPtr to the pointer for the actual value for the
// results.
if ret == 0 {
return None;
}
// Host fn call completes: by contract in host proxy, if return value is 0
// we know there's no value to read. Otherwise, we know host filled in
// AllocatedBytesPtr to the pointer for the actual value for the
// results.
if ret == 0 {
return None;
}

// Return reconstructted AllocatedBytesPtr to reveal ptr to the allocated bytes
Expand All @@ -73,7 +69,7 @@ pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R> {
/// Performs deserialization to the actual return value type from returned ptr.
///
/// This fn is for the Infallible types works for most of the cases.
#[cfg(feature = "__rkyv")]
#[cfg(all(feature = "__rkyv", feature = "__plugin_mode", target_arch = "wasm32"))]
#[cfg_attr(not(target_arch = "wasm32"), allow(unused))]
#[tracing::instrument(level = "info", skip_all)]
pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R>
Expand Down Expand Up @@ -105,8 +101,7 @@ pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R> {
/// this is for the `Fallible` struct to deserialize. If a struct contains
/// shared pointers like Arc, Rc rkyv requires trait bounds to the
/// SharedSerializeRegistry which cannot be infallible.
#[cfg(feature = "__rkyv")]
#[cfg_attr(not(target_arch = "wasm32"), allow(unused))]
#[cfg(all(feature = "__rkyv", target_arch = "wasm32"))]
#[tracing::instrument(level = "info", skip_all)]
pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R>
where
Expand All @@ -121,17 +116,14 @@ where
let (serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr_size) =
serialized_allocated_bytes_ptr.as_ptr();

#[cfg(target_arch = "wasm32")]
{
let ret = f(serialized_allocated_bytes_raw_ptr as _);
let ret = f(serialized_allocated_bytes_raw_ptr as _);

// Host fn call completes: by contract in host proxy, if return value is 0
// we know there's no value to read. Otherwise, we know host filled in
// AllocatedBytesPtr to the pointer for the actual value for the
// results.
if ret == 0 {
return None;
}
// Host fn call completes: by contract in host proxy, if return value is 0
// we know there's no value to read. Otherwise, we know host filled in
// AllocatedBytesPtr to the pointer for the actual value for the
// results.
if ret == 0 {
return None;
}

// Now reconstruct AllocatedBytesPtr to reveal ptr to the allocated bytes
Expand Down
Expand Up @@ -13,8 +13,7 @@ use swc_common::{sync::OnceCell, CharPos, FileLines, SourceFile};
use swc_ecma_ast::SourceMapperExt;
use swc_trace_macro::swc_trace;

#[cfg(feature = "__plugin_mode")]
#[cfg_attr(not(target_arch = "wasm32"), allow(unused))]
#[cfg(all(feature = "__rkyv", feature = "__plugin_mode", target_arch = "wasm32"))]
use crate::memory_interop::read_returned_result_from_host_fallible;

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -65,7 +64,7 @@ pub struct PluginSourceMapProxy {
pub source_file: OnceCell<swc_common::sync::Lrc<SourceFile>>,
}

#[cfg(feature = "__plugin_mode")]
#[cfg(all(feature = "__rkyv", feature = "__plugin_mode", target_arch = "wasm32"))]
#[swc_trace]
impl PluginSourceMapProxy {
/*
Expand Down

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

1 comment on commit 7a1c2a1

@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: 7a1c2a1 Previous: 68ffc5b Ratio
es/full/bugs-1 331740 ns/iter (± 27508) 416072 ns/iter (± 13709) 0.80
es/full/minify/libraries/antd 1849928006 ns/iter (± 22932446) 2175117314 ns/iter (± 41089339) 0.85
es/full/minify/libraries/d3 370049033 ns/iter (± 16917712) 412997615 ns/iter (± 11118822) 0.90
es/full/minify/libraries/echarts 1560028342 ns/iter (± 48132412) 1867459909 ns/iter (± 83826989) 0.84
es/full/minify/libraries/jquery 101871750 ns/iter (± 2568297) 128282448 ns/iter (± 20926942) 0.79
es/full/minify/libraries/lodash 117314005 ns/iter (± 5404659) 142531725 ns/iter (± 4481749) 0.82
es/full/minify/libraries/moment 58823483 ns/iter (± 2871460) 72499023 ns/iter (± 2869740) 0.81
es/full/minify/libraries/react 21288136 ns/iter (± 1661783) 24821955 ns/iter (± 1466232) 0.86
es/full/minify/libraries/terser 310018263 ns/iter (± 18548098) 329193589 ns/iter (± 12125074) 0.94
es/full/minify/libraries/three 584480462 ns/iter (± 34434334) 588669796 ns/iter (± 12025641) 0.99
es/full/minify/libraries/typescript 3277302496 ns/iter (± 73091238) 3947735327 ns/iter (± 80285748) 0.83
es/full/minify/libraries/victory 803494342 ns/iter (± 25895480) 860168590 ns/iter (± 57322809) 0.93
es/full/minify/libraries/vue 145461710 ns/iter (± 1752431) 183816045 ns/iter (± 1870092) 0.79
es/full/codegen/es3 33711 ns/iter (± 1029) 39967 ns/iter (± 1000) 0.84
es/full/codegen/es5 33696 ns/iter (± 1239) 40062 ns/iter (± 1192) 0.84
es/full/codegen/es2015 33619 ns/iter (± 1746) 40032 ns/iter (± 2547) 0.84
es/full/codegen/es2016 33769 ns/iter (± 339) 40498 ns/iter (± 5407) 0.83
es/full/codegen/es2017 33691 ns/iter (± 373) 40082 ns/iter (± 1192) 0.84
es/full/codegen/es2018 33795 ns/iter (± 415) 39957 ns/iter (± 937) 0.85
es/full/codegen/es2019 33630 ns/iter (± 1003) 40016 ns/iter (± 1704) 0.84
es/full/codegen/es2020 33727 ns/iter (± 883) 40171 ns/iter (± 7694) 0.84
es/full/all/es3 190737316 ns/iter (± 12758128) 233857943 ns/iter (± 11516942) 0.82
es/full/all/es5 179069335 ns/iter (± 7101289) 221575574 ns/iter (± 7729686) 0.81
es/full/all/es2015 147282439 ns/iter (± 7399404) 178887830 ns/iter (± 8416022) 0.82
es/full/all/es2016 139400862 ns/iter (± 4018966) 178881242 ns/iter (± 10811478) 0.78
es/full/all/es2017 146013879 ns/iter (± 13918127) 177588587 ns/iter (± 8919335) 0.82
es/full/all/es2018 141916501 ns/iter (± 4554118) 175201414 ns/iter (± 7805884) 0.81
es/full/all/es2019 141032613 ns/iter (± 10883016) 175118681 ns/iter (± 9395327) 0.81
es/full/all/es2020 139976513 ns/iter (± 9890605) 167393657 ns/iter (± 7259341) 0.84
es/full/parser 722357 ns/iter (± 24014) 878217 ns/iter (± 22555) 0.82
es/full/base/fixer 27083 ns/iter (± 2145) 32145 ns/iter (± 1622) 0.84
es/full/base/resolver_and_hygiene 90715 ns/iter (± 5926) 114468 ns/iter (± 12492) 0.79
serialization of ast node 213 ns/iter (± 8) 270 ns/iter (± 5) 0.79
serialization of serde 222 ns/iter (± 11) 280 ns/iter (± 8) 0.79

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

Please sign in to comment.