-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Pointer.asTypedList should allow user to specify a finalizer. #50507
Comments
Currently, the workaround to achieve zero-copying of an array which was created using native C code is something like this: First I define:
Then I use it to zero-copy arrays like this:
|
It would also be kind of weird to attach a native finalizer to an arbitrary typed list, since one does not have access to the backing store through that API.
When making a typed list from a pointer we do have access to the backing store, so I agree that that is the right place to add this API. |
Passing a
Possibly a better design is to make the API: XyzList Pointer<Xyz>.asTypedList(int length, {NativeFinalizer finalizer}); That would enable reuse of the finalizer object, and lets the just stick that single object in a global. |
I would have just expected us to create a finalizable handle with a peer that points to a struct which looks like this: struct Peer {
void* ptr;
NativeFinalizerFunction_t finalizer;
}; and associated finalizer: static void FinalizeHandle(void* whatever, void* peer) {
auto p = static_cast<Peer*>(peer);
(p->finalizer)(p->ptr);
free(peer);
} Even better we could consider making our finalizable persistent handles to support simple finalizer signatures (e.g. in addition to supporting |
Ah I was thinking to reuse the logic in the heap/GC rather than finalizable handles. Using finalizable handles would likely be slightly slower because it requires some VM state transitions. We optimzed away from runtime entries because it was slow [0]. Maybe we can do it via an FfiNative, which would be faster than a runtime entry. It cannot be a leaf call because we need Dart API access. WDYT about performance? From an API point of view either option could work, either providing a
Déjà vu, didn't I add such a bit in my first finalizer prototype where I reused the finalizable handles to handle a different signature? It feels very familiar. |
We already have |
FWIW we can also use as singular |
Ah yes, that would work indeed.
Yeah, I'd rather not modify I think I'm leaning towards your suggestion of sticking a singular |
Proposed extension:
You can't put typed list into
NativeFinalizer
because they don't extendFinalizable
(even though they behave a bit like one - we never cache internal data pointer and let typed data die).Users currently work around this limitation by creating a
Finalizable
object and linking its lifetime to that of typed data throughExpando
. It would be nice if such workarounds were unnecessaryWe should take this into account when exposing external strings as well.
/cc @dcharkes
/cc @Haidar0096
The text was updated successfully, but these errors were encountered: