aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-03 02:11:59 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-05-03 02:11:59 -0700
commit91e96a8da8aeb28ab39ab6a2642533956f58b648 (patch)
tree8b353a016e078876f292d55bfcd1458e66646b05
parent8b1924f6c2bfdc7f199e95e6564a8a51e4312ef5 (diff)
downloadbun-91e96a8da8aeb28ab39ab6a2642533956f58b648.tar.gz
bun-91e96a8da8aeb28ab39ab6a2642533956f58b648.tar.zst
bun-91e96a8da8aeb28ab39ab6a2642533956f58b648.zip
ffi overhead bench
Diffstat (limited to '')
-rw-r--r--bench/ffi/plus100/README.md14
-rw-r--r--bench/ffi/plus100/download-napi-plus100.sh2
-rw-r--r--bench/ffi/plus100/package.json4
-rw-r--r--bench/ffi/plus100/plus100.bun.js23
-rw-r--r--bench/ffi/plus100/plus100.c4
-rw-r--r--bench/ffi/plus100/plus100.deno.js14
-rwxr-xr-xbench/ffi/plus100/plus100.dylibbin16778 -> 16810 bytes
-rw-r--r--bench/ffi/plus100/plus100.napi.mjs12
-rwxr-xr-xbun.lockbbin7689 -> 8001 bytes
-rw-r--r--src/javascript/jsc/api/ffi.zig2
10 files changed, 56 insertions, 19 deletions
diff --git a/bench/ffi/plus100/README.md b/bench/ffi/plus100/README.md
index 418e7bd34..954eaa607 100644
--- a/bench/ffi/plus100/README.md
+++ b/bench/ffi/plus100/README.md
@@ -1,6 +1,6 @@
## FFI overhead comparison
-This compares the cost of a simple function call going from JavaScript to native code and back in:
+This compares the cost of simple function calls going from JavaScript to native code and back in:
- Bun v0.0.79
- napi.rs (Node v17.7.1)
@@ -18,10 +18,20 @@ To run the benchmark:
bun bench
```
+**add 100 to a number**:
+
| Overhead | Using | Version | Platform |
| -------- | ------- | ------- | --------------- |
| 7ns | bun:ffi | 0.0.79 | macOS (aarch64) |
| 18ns | napi.rs | 17.7.1 | macOS (aarch64) |
| 580ns | Deno | 1.21.1 | macOS (aarch64) |
-The native [function](./plus100.c) called in Deno & Bun are the same. The function called with napi.rs is from napi's official [package-template](https://github.com/napi-rs/package-template)
+**function that does nothing**:
+
+| Overhead | Using | Version | Platform |
+| -------- | ------- | ------- | --------------- |
+| 3ns | bun:ffi | 0.0.79 | macOS (aarch64) |
+| 15ns | napi.rs | 17.7.1 | macOS (aarch64) |
+| 431ns | Deno | 1.21.1 | macOS (aarch64) |
+
+The native [functions](./plus100.c) called in Deno & Bun are the same. The function called with napi.rs is based on napi's official [package-template](https://github.com/napi-rs/package-template) in https://github.com/Jarred-Sumner/napi-plus100
diff --git a/bench/ffi/plus100/download-napi-plus100.sh b/bench/ffi/plus100/download-napi-plus100.sh
index 9cd226857..431fa88b3 100644
--- a/bench/ffi/plus100/download-napi-plus100.sh
+++ b/bench/ffi/plus100/download-napi-plus100.sh
@@ -1,7 +1,7 @@
#!/bin/bash
rm -rf plus100-napi
-git clone https://github.com/napi-rs/package-template plus100-napi --depth=1
+git clone https://github.com/Jarred-Sumner/napi-plus100 plus100-napi --depth=1
cd plus100-napi
npm install
npm run build
diff --git a/bench/ffi/plus100/package.json b/bench/ffi/plus100/package.json
index ba7ef1fd7..6ac5d60e0 100644
--- a/bench/ffi/plus100/package.json
+++ b/bench/ffi/plus100/package.json
@@ -5,8 +5,8 @@
"bench-deno": "deno run --allow-ffi --unstable -A plus100.deno.js",
"napi-setup": "bash download-napi-plus100.sh",
"bench-napi": "node plus100.napi.mjs",
- "bench-bun": "bun run ./plus100.bun.js",
+ "bench-bun": "bun ./plus100.bun.js",
"compile": "clang -mtune=native -O3 -shared ./plus100.c -o plus100.dylib",
- "bench": "echo -e '\n--- Bun:\n' && bun run bench-bun && echo -e '\n--- Node:\n' && bun run bench-napi && echo -e '\n--- Deno:\n' && bun run bench-deno"
+ "bench": "bun run bench-bun && bun run bench-napi && bun run bench-deno"
}
}
diff --git a/bench/ffi/plus100/plus100.bun.js b/bench/ffi/plus100/plus100.bun.js
index ca4bf0f64..3ddaff975 100644
--- a/bench/ffi/plus100/plus100.bun.js
+++ b/bench/ffi/plus100/plus100.bun.js
@@ -1,19 +1,30 @@
import { run, bench, group, baseline } from "mitata";
-import { dlopen } from "bun:ffi";
+import { dlopen, suffix } from "bun:ffi";
const {
- symbols: { plus100: plus100 },
+ symbols: {
+ plus100: { native: plus100 },
+ noop,
+ },
close,
-} = dlopen("./plus100.dylib", {
+} = dlopen(`./plus100.${suffix}`, {
plus100: {
- params: ["int32_t"],
+ args: ["int32_t"],
returns: "int32_t",
},
+ noop: {
+ args: [],
+ },
});
-bench("plus100(1) (Bun FFI)", () => {
+bench("plus100(1) ", () => {
plus100(1);
});
+bench("noop() ", () => {
+ noop();
+});
+
// collect option collects benchmark returned values into array
// prevents gc and can help with jit optimizing out functions
-run({ collect: false, percentiles: true });
+await run({ collect: false, percentiles: true });
+console.log("\n");
diff --git a/bench/ffi/plus100/plus100.c b/bench/ffi/plus100/plus100.c
index c5b7933ea..bdecda7e0 100644
--- a/bench/ffi/plus100/plus100.c
+++ b/bench/ffi/plus100/plus100.c
@@ -2,5 +2,7 @@
#include <stdint.h>
int32_t plus100(int32_t a);
-
int32_t plus100(int32_t a) { return a + 100; }
+
+void noop(void);
+void noop(void) {} \ No newline at end of file
diff --git a/bench/ffi/plus100/plus100.deno.js b/bench/ffi/plus100/plus100.deno.js
index e6104efdd..83e4fc317 100644
--- a/bench/ffi/plus100/plus100.deno.js
+++ b/bench/ffi/plus100/plus100.deno.js
@@ -1,18 +1,26 @@
import { run, bench, group, baseline } from "https://esm.sh/mitata";
const {
- symbols: { plus100: plus100 },
+ symbols: { plus100: plus100, noop },
close,
} = Deno.dlopen("./plus100.dylib", {
plus100: {
parameters: ["i32"],
result: "i32",
},
+ noop: {
+ parameters: [],
+ result: "void",
+ },
});
-bench("plus100(1) (Deno FFI)", () => {
+bench("plus100(1) ", () => {
plus100(1);
});
+bench("noop() ", () => {
+ noop();
+});
+
// collect option collects benchmark returned values into array
// prevents gc and can help with jit optimizing out functions
-run({ collect: false, percentiles: true });
+await run({ collect: false, percentiles: true });
diff --git a/bench/ffi/plus100/plus100.dylib b/bench/ffi/plus100/plus100.dylib
index 030d1afef..7b5ec3962 100755
--- a/bench/ffi/plus100/plus100.dylib
+++ b/bench/ffi/plus100/plus100.dylib
Binary files differ
diff --git a/bench/ffi/plus100/plus100.napi.mjs b/bench/ffi/plus100/plus100.napi.mjs
index f4adda8d3..aa8d2636f 100644
--- a/bench/ffi/plus100/plus100.napi.mjs
+++ b/bench/ffi/plus100/plus100.napi.mjs
@@ -2,9 +2,15 @@ import { bench, run } from "mitata";
import module from "module";
-const { plus100 } = module.createRequire(import.meta.url)("./plus100-napi");
+const { plus100, noop } = module.createRequire(import.meta.url)(
+ "./plus100-napi"
+);
-bench("plus100(1) (napi.rs)", () => {
+bench("plus100(1) ", () => {
plus100(1);
});
-run({ collect: false, percentiles: true });
+bench("noop() ", () => {
+ noop();
+});
+await run({ collect: false, percentiles: true });
+console.log("\n");
diff --git a/bun.lockb b/bun.lockb
index 4e2475280..b4fe11747 100755
--- a/bun.lockb
+++ b/bun.lockb
Binary files differ
diff --git a/src/javascript/jsc/api/ffi.zig b/src/javascript/jsc/api/ffi.zig
index ce162f890..6e659837e 100644
--- a/src/javascript/jsc/api/ffi.zig
+++ b/src/javascript/jsc/api/ffi.zig
@@ -418,7 +418,7 @@ pub const FFI = struct {
// var function
var return_type = ABIType.@"void";
- if (value.get(global, "return_type")) |ret_value| brk: {
+ if (value.get(global, "returns")) |ret_value| brk: {
if (ret_value.isAnyInt()) {
const int = ret_value.toInt32();
switch (int) {