Skip to content
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

Direct Memory API #667

Closed
Tracked by #1004
mahesh-hegde opened this issue Apr 12, 2023 · 2 comments
Closed
Tracked by #1004

Direct Memory API #667

mahesh-hegde opened this issue Apr 12, 2023 · 2 comments
Assignees
Labels
package:jni package:jnigen type-enhancement A request for a change that isn't a bug

Comments

@mahesh-hegde
Copy link
Contributor

Status Quo:

We map JNI Arrays into a subscript-indexed linear dart type.

I wish JNI memory access is just as simple. But it's full of nuances.

Look at this FAQ on android developers page.

Ideally, there should be ways to utilize the primitives JNI gives us to maximum efficiency.

The idea

Map JNI arrays into implementations corresponding typed_data list interfaces. For example, intArray.getRegion() should return a typed list of int32s.

We also want easier API for manipulating DirectByteBuffer, for reasons outlined in above link.

However, this is not so simple complicated for few reasons.

  • We can't implement or extend types like Int32List which clutters the API.

  • There are times where region calls are more appropriate and where getArrayElements is more appropriate. Exposing both of these in a coherent API is hard.

We can probably create view classes (eg JIntArrayView), which

  • are returned by region calls, getArrayElements calls, and byte-buffer calls.
  • Upon mutation, provide an explicit call to write the results back. (flush).
    • Not applicable in case of byte buffer, however. flush could be no-op here.
  • Expose an interface similar to typed lists (While we can't subclass them, we can still wrap over their methods).
@mahesh-hegde mahesh-hegde added type-enhancement A request for a change that isn't a bug package:jni labels Apr 13, 2023
@mahesh-hegde
Copy link
Contributor Author

Also interesting: https://developer.android.com/reference/java/nio/CharBuffer

In APIs which accept CharSequence instead of String, this will be useful to pass text data without having to convert it into java string first.

@HosseinYousefi HosseinYousefi transferred this issue from dart-archive/jnigen Nov 17, 2023
@HosseinYousefi HosseinYousefi moved this to Backlog in JNIgen tracker Apr 11, 2024
@HosseinYousefi HosseinYousefi added this to the JNI / JNIgen 0.9.0 milestone Apr 17, 2024
@HosseinYousefi HosseinYousefi moved this from Backlog to Todo in JNIgen tracker Apr 17, 2024
@HosseinYousefi HosseinYousefi self-assigned this Apr 17, 2024
@HosseinYousefi HosseinYousefi moved this from Todo to In Progress in JNIgen tracker Apr 18, 2024
@HosseinYousefi
Copy link
Member

Map JNI arrays into implementations corresponding typed_data list interface

Will be closed by #1095.

We also want easier API for manipulating DirectByteBuffer.

This is done #563.

We can probably create view classes

I don't like having to flush and making sure that we release the elements afterwards.

Some Arena.use-esque API that gets a callback which will release resources automatically at the end might be a better design.

All in all, it's not guaranteed that Get<type>ArrayElements allows direct access to the native memory and it might copy it anyways, so providing array.getRange that returns a typed data might just be enough for users.

Closing this.

@github-project-automation github-project-automation bot moved this from In Progress to Done in JNIgen tracker Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:jni package:jnigen type-enhancement A request for a change that isn't a bug
Projects
Status: Done
Development

No branches or pull requests

2 participants