javi
javi2y ago

FFI Difference between buffer and pointer

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.
3 Replies
dj
dj2y ago
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
AapoAlas
AapoAlas2y ago
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.
javi
javiOP2y ago
Great, thanks to both <:cookie_deno:1002977285734932480>

Did you find this page helpful?