-
Hi! Yesterday I was reading through the book again and came to wonder if it's possible to zero copy a big vec of typed instances. I initially thought about using a ZeroCopyBuffer and cast it to the Dart type on the other side of the wire, but quickly recalled that what Rust will send probably cannot be cast into a Dart type as I guess its underlying representation to be different. How would you approach this? Are there alternatives that I completely overlooked? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
ZeroCopyBuffer relys on the underlying Dart C api: https://github.com/sunshine-protocol/allo-isolate/blob/913b17dfb34b3a63bbbf4ddb282e97b8340e3e0e/src/ffi.rs#L56 i.e. https://github.com/dart-lang/sdk/blob/main/runtime/include/dart_native_api.h Thus, given that Dart only exposes a C api that represents u8/i8/... arrays, we seem to have no other approaches at the first glance. However there do seem to exist an approachyou may use "wrappers" around raw bytes. For example, say I have
class VecOfS {
final Uint8List data;
VecOfS(this.data);
ProxyOfS operator[](int index) => ProxyOfS(this, index);
}
class ProxyOfS {
final VecOfS vec;
final int index;
int get a => vec.data[index * 8];
int get b => vec.data[index * 8 + 4];
} Usage:
Feel free to PR! |
Beta Was this translation helpful? Give feedback.
-
I am not familiar with the DART API, or the nature of fridge, so I welcome any reply. Or point out the ones I forget. @fzyzcjy As Fzyzcjy put it:
Meanwhile, in the fridge, we used the Nest IntoDart to generate nest DartCObjectType.
2.1. Then it becomes a question of needing another, faster method, The first scenario, I didn't test it, I just follow Fzyzcjy's reply, And with the above supplementary information. But there maybe no mistakes.
For the second scenario, which has the same memory across languages (so you don't need a CPU to compute it).
if in second scenario, your idea maybe work. But, you need to implement a lib like arrow2. |
Beta Was this translation helpful? Give feedback.
ZeroCopyBuffer relys on the underlying Dart C api: https://github.com/sunshine-protocol/allo-isolate/blob/913b17dfb34b3a63bbbf4ddb282e97b8340e3e0e/src/ffi.rs#L56 i.e. https://github.com/dart-lang/sdk/blob/main/runtime/include/dart_native_api.h
Thus, given that Dart only exposes a C api that represents u8/i8/... arrays, we seem to have no other approaches at the first glance.
However there do seem to exist an approach
you may use "wrappers" around raw bytes. For example, say I have
struct S {a: u8, b: u8}
and suppose each field align to 4 bytes (only for simplicity!). Then we can do the following to zero-copy aVec<S>
to Dart:Vec<S>
bytemuck
or other crate, to convert to