-
Notifications
You must be signed in to change notification settings - Fork 1.7k
More documentation needed for NativePointer #48023
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
Comments
Vote for its importance as well. This is a critical problem and I am also looking forward to it. Without using this feature, https://github.com/fzyzcjy/flutter_rust_bridge, the open source library between Dart/Flutter and Rust, cannot implement the opaque pointers and the expressiveness is much lower. |
[Quick answer: because most of us are on end-of-year holidays]. The API added by @aam only would call a finalizer you supply if the message is not delivered for some reason (e.g. isolate shutting down). FWIW I think the API looks a bit confusing especially in contrast with external typed data one which would attach the finalizer to the object allocated on the receiving end. Also the clash of naming (native pointer vs FFI Pointer) is also a bit confusing. |
Thanks for the quick answer and no rush, enjoy the holiday season! For when folks are back: |
@raphaelrobert wrote
Do you know you can attach finalizer to native bytes when you send them via @mraleph wrote
cc @a-siva who actually added this. |
Thanks, yes, that's the functionality I am after with the exception that the data shouldn't be typed or exposed to Dart. I assumed |
I see. Having dart vm know about the size of externally allocated memory associated with dart objects can be useful for gc purposes, can help vm do better gc if it knows which potentially gc'able objects are holding on to external memory. You might address misuse concern by limiting visibility of external typed data received from receive port: you can put it immediately inside another opaque dart object of your own and widely use that instead. |
So the size can already be reported when sending the native pointer to Dart: struct {
intptr_t ptr;
intptr_t size;
Dart_HandleFinalizer callback;
} as_native_pointer; This could be used by the GC. I understand that it's not the case right now, and also that the callback serves a different purpose than the finalizer that gets attached to external typed data. My question is: would it be possible to align the behavior so that native pointer objects would be treated similarly to external typed data? This wouldn't preclude the original use case of the callback in case the isolate shuts down too early. Two changes would be required for that:
That being said, since I still don't fully understand the current purpose of native pointers my suggestions might not make sense. @a-siva maybe you can shed some light on that?
Right, that would be the obvious workaround. |
There is no
|
Here are my two cents about using
Here, if I want to interpret Dart VM should know that this object occupies 1GB memory, such that it can determine its GC pace. However, if we interpret it as a |
Thanks for all the insights @aam! It appears
|
Firstly, thanks for all the excellent work you do!
I have recently started playing with the new
NativePointer
introduced in https://dart-review.googlesource.com/c/sdk/+/211340. I read through all the documentation and issues I could find, but I still have open questions.The problem I'm trying to solve is the following:
Why is this important? I think having finalizers and a well-understood memory management would allow deep integration between Dart and other languages.
All of the above seems to be doable with
NativePointer
, but I came across a few things that are not straightforward. This is possibly a lack of understanding on my side, and/or a lack of documentation.Here are my findings:
Dart_PostCObject
) causes Dart to see this anint
. Theint
contains the raw value of the pointer. I haven't seen this documented anywhere and I was a bit surprised by it.int
can be used in subsequent calls to Rust, however, it's really just a reference. On the Rust side I have to callmem::forget()
before returning, otherwise Dart will segfault. This makes sense, since the ownership is now fully on the Dart side.dart run --observe
to have access to the debugger where GC can be manually triggered. This had no effect whatsoever. I also tried to allocate several GB on the Rust side and reported the size back to Dart in order to create greater memory pressure. Dart counts the Rust heap in the RSS part of the memory, not its own heap (which actually makes sense). Question: does the reported size have any effect at all?In summary, I feel we are close to having a great mechanism. Hopefully it's just a matter of better understanding/documentation.
The text was updated successfully, but these errors were encountered: