google / flutter-mediapipe

Apache License 2.0
212 stars 14 forks source link

Native memory helpers #18

Closed craiglabenz closed 9 months ago

craiglabenz commented 9 months ago

Adds helpers for converting between Dart and native memory for the following scenarios:

  1. A list of Dart doubles <-> A list of native floats
  2. A Dart Uint8List <-> A native Pointer<Char>.

Additionally, I would like to submit the function Uint8List toUint8List(Pointer<Char> val, {int? length}) for code review, even though it was committed previously.

--

The context for this PR is work around Embeddings, which are currently emitting random results on sequential runs, and for which I want to rule out these low level memory mapping helpers.

cc @Piinks

dcharkes commented 9 months ago

A Dart Uint8List <-> A native Pointer<Char>.

This is not entirely safe. The char type behaves as int8 on some platforms and as uint8 on other platforms. You can notice differences once using the indexed access operator in Dart for values 128-255 (or -128 to -1). Depending on the use case this might or might not be an issue. ByteBuffer and ByteData are classes that just passes around a piece of memory and does not promise a specific layout. The latter being able to pass around views with a non-zero offset. Again, it probably depends on the use case what solution makes sense.

We have a tracking issue for .asTypedList on Pointer<T extends AbiSpecificInteger>:

craiglabenz commented 9 months ago

This is not entirely safe. The char type behaves as int8 on some platforms and as uint8 on other platforms.

A Dart Uint8List <-> A native Pointer<Char>.

This is not entirely safe. The char type behaves as int8 on some platforms and as uint8 on other platforms. You can notice differences once using the indexed access operator in Dart for values 128-255 (or -128 to -1). Depending on the use case this might or might not be an issue. ByteBuffer and ByteData are classes that just passes around a piece of memory and does not promise a specific layout. The latter being able to pass around views with a non-zero offset. Again, it probably depends on the use case what solution makes sense.

We have a tracking issue for .asTypedList on Pointer<T extends AbiSpecificInteger>:

Do you have any estimates on the tracking issue? It doesn't seem about to land, given that you haven't gotten any responses to your questions.

Absent that catch-all implementation, I'd love to quickly sync on the edge cases here to make sure I'm properly handling everything. Even with your helpful answer, I'm not sure how to exhaustively handle these edge cases.

craiglabenz commented 9 months ago

This PR is being replaced.