diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-05-27 21:01:09 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-28 09:31:09 +0530 |
commit | 53606de6344fca63c40fde37056910b72a3c4f8d (patch) | |
tree | c2d31a7bded8ad5402ad8582d2e4c8df1a100bda /ext/ffi/00_ffi.js | |
parent | d99c6c1ea4f5711c3f48c5617d7224852fb294d2 (diff) |
BREAKING(ffi/unstable): always return u64 as bigint (#23981)
The mixed `number | bigint` representation was useful optimization for
pointers. Now, pointers are represented as V8 externals. As part of the
FFI stabilization effort we want to make `bigint` the only
representation for `u64` and `i64`.
BigInt representation performance is almost on par with mixed
representation with the added benefit that its less confusing and users
don't need manual checks and conversions for doing operations on the
value.
```
cpu: AMD Ryzen 5 7530U with Radeon Graphics
runtime: deno 1.43.6+92a8d09 (x86_64-unknown-linux-gnu)
file:///home/divy/gh/ffi/main.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
-------------------------------------------------------------------------- -----------------------------
nop 4.01 ns/iter 249,533,690.5 (3.97 ns … 10.8 ns) 3.97 ns 4.36 ns 9.03 ns
ret bigint 7.74 ns/iter 129,127,186.8 (7.72 ns … 10.46 ns) 7.72 ns 8.11 ns 8.82 ns
ret i32 7.81 ns/iter 128,087,100.5 (7.77 ns … 12.72 ns) 7.78 ns 8.57 ns 9.75 ns
ret bigint (add op) 15.02 ns/iter 66,588,253.2 (14.64 ns … 24.99 ns) 14.76 ns 19.13 ns 19.44 ns
ret i32 (add op) 12.02 ns/iter 83,209,131.8 (11.95 ns … 18.18 ns) 11.98 ns 13.11 ns 14.5 ns
```
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r-- | ext/ffi/00_ffi.js | 17 |
1 files changed, 1 insertions, 16 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index 19d62dfdd..7f39db13e 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -46,7 +46,6 @@ const { ObjectDefineProperty, ObjectHasOwn, ObjectPrototypeIsPrototypeOf, - Number, NumberIsSafeInteger, TypedArrayPrototypeGetBuffer, TypedArrayPrototypeGetByteLength, @@ -348,10 +347,6 @@ function isReturnedAsBigInt(type) { type === "usize" || type === "isize"; } -function isI64(type) { - return type === "i64" || type === "isize"; -} - function isStruct(type) { return typeof type === "object" && type !== null && typeof type.struct === "object"; @@ -562,7 +557,6 @@ class DynamicLibrary { const call = this.symbols[symbol]; const parameters = symbols[symbol].parameters; const vi = new Int32Array(2); - const vui = new Uint32Array(TypedArrayPrototypeGetBuffer(vi)); const b = new BigInt64Array(TypedArrayPrototypeGetBuffer(vi)); const params = ArrayPrototypeJoin( @@ -572,22 +566,13 @@ class DynamicLibrary { // Make sure V8 has no excuse to not optimize this function. this.symbols[symbol] = new Function( "vi", - "vui", "b", "call", - "NumberIsSafeInteger", - "Number", `return function (${params}) { call(${params}${parameters.length > 0 ? ", " : ""}vi); - ${ - isI64(resultType) - ? `const n1 = Number(b[0])` - : `const n1 = vui[0] + 2 ** 32 * vui[1]` // Faster path for u64 - }; - if (NumberIsSafeInteger(n1)) return n1; return b[0]; }`, - )(vi, vui, b, call, NumberIsSafeInteger, Number); + )(vi, b, call); } else if (isStructResult && !isNonBlocking) { const call = this.symbols[symbol]; const parameters = symbols[symbol].parameters; |