mirror of
https://github.com/denoland/deno.git
synced 2024-11-28 16:20:57 -05:00
04ba709b6e
Currently fast ops will always check for the alignment of a TypedArray when getting a slice out of them. A match is then done to ensure that some slice was received and if not a fallback will be requested. For Uint8Arrays (and WasmMemory which is equivalent to a Uint8Array) the alignment will always be okay. Rust probably optimises this away for the most part (since the Uint8Array check is `x % 1 != 0`), but what it cannot optimise away is the fast ops path's request for fallback options parameter. The extra parameter's cost is likely negligible but V8 will need to check if a fallback was requested and prepare the fallback call just in case it was. In the future the lack of a fallback may also enable V8 to much better optimise the result handling. For V8 created buffers, it seems like all buffers are actually always guaranteed to be properly aligned: All buffers seem to always be created 8-byte aligned, and creating a 32 bit array or 64 bit array with a non-aligned offset from an ArrayBuffer is not allowed. Unfortunately, Deno FFI cannot give the same guarantees, and it is actually possible for eg. 32 bit arrays to be created unaligned using it. These arrays work fine (at least on Linux) so it seems like this is not illegal, it just means that we cannot remove the alignment checking for 32 bit arrays. |
||
---|---|---|
.. | ||
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.
) {
// ...
}