D
Deno

help

FFI Difference between buffer and pointer

Jjavi2/24/2023
I’ve been using interchangeably Deno.UnsafePointer.of(new Uint8Array()) with type pointer and new Uint8Array() with type buffer. Is there any major difference? Per se I don’t use char[] arrays, just pointers.
Ddjdev2/24/2023
If you're only passing typed arrays (prefer Uint8Arrays as they can be optimized in Deno FFI), use buffer type. If you're only passing pointers that some other function returns, use the pointer type. Using pointer type + Deno.UnsafePointer.of(TypedArray) it will have some performance overhead due to additional call. Prefer buffer type for passing typed arrays
AAapoAlas2/24/2023
On the C / native code level there is no difference. The only real difference is what types are accepted, ie. TypedArrays or pointer numbers (starting 1.31 now pointer objects). The split exists for optimisation reasons: V8 gives us very fast calls for FFI but requires us to choose between some buffer type and a pointer object type. So, we choose to expose that choice for users.
Jjavi2/24/2023
Great, thanks to both <:cookie_deno:1002977285734932480>

Looking for more? Join the community!