Skip to content

[vm/ffi] Proposal of (Dynamic/Unbound)TypedData #54876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
antonbashir opened this issue Feb 10, 2024 · 0 comments
Open

[vm/ffi] Proposal of (Dynamic/Unbound)TypedData #54876

antonbashir opened this issue Feb 10, 2024 · 0 comments
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

Comments

@antonbashir
Copy link

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 } or struct {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 to writev.

What I propose is a new TypedData API:

(Unbounded/Dynamic)TypedData with the following features:

  1. ByteData combined with setRange. Implemented by current optimal way but without length checking. So users will be able to serialize simple values and complex (like buffers and strings) using the same API and object.
  2. Using the user native function (but not with trampoline) by native function pointer for allocation new space for serialization buffer. For example:
class MyCustomBuffer extends DynamicTypedData {
  final Pointer<Uint8> pos;
  final Pointer<Uint8> end;
  final Pointer<my_custom_buffer_t> context;

   void serialize(myStructure) {
     ...
     var newBufferItemSize = ...
     if (pos.value + newBufferItemSize > pos.end) {
         allocate(this, context, newBufferItemSize); 
      }
      ...
   }
}
  1. Length-free and checks-free. Users will validate by themselves. Most optimal as possible. Because we don't know how large will be the buffer.

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.

@lrhn lrhn added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug library-ffi labels Feb 10, 2024
@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants