summaryrefslogtreecommitdiff
path: root/test_ffi
diff options
context:
space:
mode:
Diffstat (limited to 'test_ffi')
-rw-r--r--test_ffi/src/lib.rs10
-rw-r--r--test_ffi/tests/bench.js8
2 files changed, 18 insertions, 0 deletions
diff --git a/test_ffi/src/lib.rs b/test_ffi/src/lib.rs
index 95b7d900c..bc89bd66c 100644
--- a/test_ffi/src/lib.rs
+++ b/test_ffi/src/lib.rs
@@ -95,6 +95,16 @@ pub extern "C" fn add_f64(a: f64, b: f64) -> f64 {
}
#[no_mangle]
+unsafe extern "C" fn hash(ptr: *const u8, length: u32) -> u32 {
+ let buf = std::slice::from_raw_parts(ptr, length as usize);
+ let mut hash: u32 = 0;
+ for byte in buf {
+ hash = hash.wrapping_mul(0x10001000).wrapping_add(*byte as u32);
+ }
+ hash
+}
+
+#[no_mangle]
pub extern "C" fn sleep_blocking(ms: u64) {
let duration = Duration::from_millis(ms);
sleep(duration);
diff --git a/test_ffi/tests/bench.js b/test_ffi/tests/bench.js
index 8885c7d7e..da29f482f 100644
--- a/test_ffi/tests/bench.js
+++ b/test_ffi/tests/bench.js
@@ -12,6 +12,7 @@ const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;
const dylib = Deno.dlopen(libPath, {
"nop": { parameters: [], result: "void" },
"add_u32": { parameters: ["u32", "u32"], result: "u32" },
+ "hash": { parameters: ["pointer", "u32"], result: "u32" },
"nop_u8": { parameters: ["u8"], result: "void" },
"nop_i8": { parameters: ["i8"], result: "void" },
"nop_u16": { parameters: ["u16"], result: "void" },
@@ -231,6 +232,13 @@ Deno.bench("add_u32()", () => {
add_u32(1, 2);
});
+const bytes = new Uint8Array(64);
+
+const { hash } = dylib.symbols;
+Deno.bench("hash()", () => {
+ hash(bytes, bytes.byteLength);
+});
+
const { nop_u8 } = dylib.symbols;
Deno.bench("nop_u8()", () => {
nop_u8(100);