-
Notifications
You must be signed in to change notification settings - Fork 60
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
Arena use with Java references #766
Comments
Side note that might be a bit too far fetched: could an |
If the optional argument is implemented, it has to be generated as a param declaration in dart binding methods anyway. So can as well pass an extra argument to C bindings. The question is whether it's worth the extra code, since local references aren't theoretically consistent with dart threading anyway. |
The best code is the one you don't have to write. Because of that, I like the native finalizer approach, if it works. It might still be possible to use an What if If If we can't get that to work, then some kind of explicit registration is needed. |
As I understand you're proposing writing something like
But testWidgets is an example picked from integration test. In reality I'm leaning towards |
I think it should. Also performance should be good, but we should verify that. I've filed a separate issue for implementing finalizers: If finalizers have significant overhead or it turns out that theres another issue with the finalizer approach, we can consider API 1-3 again.
Zones are much harder for users to understand. So I'd want to avoid that. With memory allocation we've got a fairly clean design that just enables |
I also prefer the native finalizer approach. This is what we do for the objective C bindings. Maybe I'm missing something, but I don't see what the argument against native finalizers are. |
I think finalizers works for global references, but don't really fit local references that well. Local references are supposed to be used in the scoped fashion just like Dart VM handles. My personal inclination here would be to wrap
|
Interesting, if we have thread-pinning in the Dart VM we could use the local JNI references in such a way. We currently do not have thread-pinning in the VM, so we're not guaranteed to that a next JNI call from Dart will be on the same thread. This means we cannot use the local references from JNI. We're currently refactoring the jni helper package and the code generator to only use global references. We should maybe revisit that decision once we have thread-pinning. |
If the code is synchronous then thread pinning is not a concern - synchronous code is naturally pinned to a single thread. Note that |
Hi @mraleph,
In fact the current PR dart-archive/jnigen#53 completes this change. I wonder if there's a clean design such that generated binding API (which now mirrors java classes and methods exactly), can be told to return global or local reference at call site, without generating an additional parameter and corresponding code at each method. (Although this is not strictly necessary since local reference can be converted to global in Dart code later. I am just wondering where is the local optimum of designing this API.)
|
Well, this is not documented, but for foreseeable future if the stack is not empty then execution can't switch between threads. All synchronous code will execute to completion on the same thread. This might change at some point if we decide to implement some variant of lightweight threads or fibers, but this is far out. I think in ART both local and global references are backed by the same structure called indirect reference table, https://cs.android.com/android/platform/superproject/+/master:art/runtime/indirect_reference_table.h;drc=dd6f7c69627e6d24c2cc026654f5ca118224f6db;l=46, so their performance characteristics might be pretty close. Though it would help if we did some benchmarking. At the very least if you "globalise" all references that means you are effectively allocating twice as many references to begin with. You also risk extending lifetimes of some objects (unless you have some sort of scoped API - like the one @dcharkes describes above with arenas). I do agree that choosing the right API here is difficult. The main concern here is around |
Yeah. If local reference is accidentally used in another thread, I think it will segfault directly. I don't think there's a way to catch it reliably, on Android, CheckJNI will produce a stack trace but it's not as reliable as throwing an exception. In the beginning of this GSoC project the API I designed did use local references by default and required the user to manually create global reference to use across async calls, but looking back, it was a bad choice. It's very easy to segfault accidentally. |
I suggested this earlier as well.
With this principal we should go for |
@dcharkes can this be closed? Now we have NativeFinalizer and deletedIn. |
Yes, its basically API 2 with a better name. :) |
Problem
When using JNI, references to Java objects need to be deleted. Manual management leads to the following type of code.
This type of code smells like it should use the
Arena
frompackage:ffi
.https://github.com/dart-lang/ffi/blob/master/lib/src/arena.dart
The question is what API we should use.
API 1: Extension method on
Arena
Pro:
manage
is managed by the arena so that the API user doesn't have to worry about managing it.Con:
arena.manage
before the thing they are thinking about.API 2: Method on objects with
delete
Pro:
delete
anddeletedBy
are clearly related.Con:
..
feels like it is easily forgotten.deletedBy
doesn't feel like a natural method name. Should it bedeleter
?setDeleter
?Side note: should it be
delete
? It isDeleteGlobalRef
in JNI, but if we want to talk about the abstraction of native resource management, we've usedrelease
as verb. Notfree
ordelete
.API 3: Optional
arena
parameterPro:
Con:
This approach would be similar to the optional
allocator
parameter intoNativeUtf8
.https://github.com/dart-lang/ffi/blob/master/lib/src/utf8.dart#L81
API 4:
NativeFinalizer
sWe could attach a
NativeFinalizer
to all objects and have them be cleaned up automatically.However, even in that case we could keep
delete
andArena
support. Those would cancel the native finalizer.@mahesh-hegde and I are slightly leaning towards API 2.
@liamappelbe @lrhn @mkustermann WDYT?
The text was updated successfully, but these errors were encountered: