[vm/ffi] Proposal of (Dynamic/Unbound)TypedData #54876
Labels
area-vm
Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
library-ffi
type-enhancement
A request for a change that isn't a bug
Greetings.
Currently I am doing Dart-C interaction API based on io_uring only for Linux platform. This interaction API I will use with my io_uring based IO (tcp/udp/files) and also with one specific in-memory database (communication with database over io_uring messages).
In my implementation I use serialization with MsgPack.
Imagine that you need to serialize structure (Dart or Dart FFI Structure) to binary buffer. This buffer could be allocated by Dart, by C (malloc), by C (custom allocator) or by C (custom buffer structure). During serialization you want to use ByteData methods like setUint* and you want them to be the most optimal (close to native performance). Also you want to write strings and raw binary data so you need setRange.
Most of such tasks could be done by Uint8List combined with ByteData (a lot of current Dart serializers contains two fields in the their serialization class: Uint8List and ByteData).
But I also have a case when you have buffer backed by custom C structures. For example, it could be like
struct { char* read_pos, char* write_pos }
orstruct {struct iovec[N] data}
. I need them because in some "low-level" cases different buffer structures could be optimal: for example you can send iovec-s towritev
.What I propose is a new TypedData API:
(Unbounded/Dynamic)TypedData with the following features:
allocate()
should be implemented in the most optimal way: during construction, DynamicTypedData receives pointer to user native function, this function will receive native representation of MyCustomBuffer, context (pointer to custom user buffer structure) and a size to be allocated by user custom buffer. By using pointer to user buffer users can execute native code to extend available space and by using pointer to our DynamicTypedData object they can change pointers for serialization process in their native function.Similar to NativeFinalizer but NativeAllocator. And I know that this could be achieved by using FFI functions for working with your own buffer, but there will be a lot of FFI calls and Uint8List + ByteData manipulations because you should always synchronize Unit8List with native buffer (in case of allocation/reallocation/extension).
It would be very nice if with Dart could be possible to operate custom user-defined native-backed memory buffer/allocator without performance loss.
The text was updated successfully, but these errors were encountered: