mirror of
https://github.com/denoland/deno.git
synced 2024-12-21 23:04:45 -05:00
0910be4d64
Join two independent ops into one. A fast impl of one + a slow callback of another. Here's an example showing optimized paths for latin-1 via fast call and the next-best fallback using V8 apis. ```rust #[op(v8)] fn op_encoding_encode_into_fallback( scope: &mut v8::HandleScope, input: serde_v8::Value, // ... #[op(fast, slow = op_encoding_encode_into_fallback)] fn op_encoding_encode_into( input: Cow<'_, str>, // ... ``` Benchmark results of the fallback path: ``` time target/release/deno run -A --unstable ./cli/tests/testdata/benches/text_encoder_into_perf.js ________________________________________________________ Executed in 70.90 millis fish external usr time 57.76 millis 0.23 millis 57.53 millis sys time 17.02 millis 1.28 millis 15.74 millis target/release/deno_main run -A --unstable ./cli/tests/testdata/benches/text_encoder_into_perf.js ________________________________________________________ Executed in 154.00 millis fish external usr time 67.14 millis 0.26 millis 66.88 millis sys time 38.82 millis 1.47 millis 37.35 millis ``` |
||
---|---|---|
.. | ||
optimizer_tests | ||
tests/compile_fail | ||
attrs.rs | ||
Cargo.toml | ||
deno.rs | ||
fast_call.rs | ||
lib.rs | ||
optimizer.rs | ||
README.md |
deno_ops
proc_macro
for generating highly optimized V8 functions from Deno ops.
// Declare an op.
#[op(fast)]
pub fn op_add(_: &mut OpState, a: i32, b: i32) -> i32 {
a + b
}
// Register with an extension.
Extension::builder()
.ops(vec![op_add::decl()])
.build();
Performance
The macro can optimize away code, short circuit fast paths and generate a Fast API impl.
Cases where code is optimized away:
-> ()
skips serde_v8 andrv.set
calls.-> Result<(), E>
skips serde_v8 andrv.set
calls forOk()
branch.-> ResourceId
or-> [int]
types will use specialized method likev8::ReturnValue::set_uint32
. A fast path for SMI.-> Result<ResourceId, E>
or-> Result<[int], E>
types will be optimized like above for theOk()
branch.
Fast calls
The macro will infer and try to auto generate V8 fast API call trait impl for
sync
ops with:
- arguments: integers, bool,
&mut OpState
,&[u8]
,&mut [u8]
,&[u32]
,&mut [u32]
- return_type: integers, bool
The #[op(fast)]
attribute should be used to enforce fast call generation at
compile time.
Trait gen for async
ops & a ZeroCopyBuf equivalent type is planned and will be
added soon.
Wasm calls
The #[op(wasm)]
attribute should be used for calls expected to be called from
Wasm. This enables the fast call generation and allows seamless WasmMemory
integration for generic and fast calls.
#[op(wasm)]
pub fn op_args_get(
offset: i32,
buffer_offset: i32,
memory: Option<&[u8]>, // Must be last parameter. Some(..) when entered from Wasm.
) {
// ...
}