RFR: JNI support for flattened arrays

John Rose john.r.rose at oracle.com
Thu Nov 28 03:44:44 UTC 2019


It would be reasonable to have similar functions that can
handle inlines that are general mixes of primitives and references.

For example, getters and setters of flat primitive data can leave
holes where the oops are, just padding with zeroes (on get) or
ignoring (on set).  That handles all primitive data in all kinds
of inlines.  This functionality could be added to your current
function for bulk getting.

To handle references, a 2-dimensional getter could be defined:

    jobject (JNICALL *GetFlattenedArrayElementReference)
      (JNIEnv *env, jobjectArray array, jsize index, int offset);
    void (JNICALL *SetFlattenedArrayElementReference)
      (JNIEnv *env, jobjectArray array, jsize index, int offset, jobject val);
    jsize (JNICALL *GetFlattenedArrayElementReferenceCount)
      (JNIEnv* env, jarray array);

The “offset” field would be whatever is returned by
GetFieldOffsetInFlattenedLayout.  Alternatively, it
could be an ordinal less than the ReferenceCount.

Since there is no single-element variation on GetFlattenedArrayElements,
there’s no way to stream over a flattened array.
Perhaps should GetFlattenedArrayElements should allow a slice of the
array to be grabbed, not always the whole array?

Or perhaps we just want this to do single element access:

    jsize (JNICALL *GetFlattenedArrayElement)
      (JNIEnv *env, jobjectArray array, jsize index,
         // two output buffers; oop positions in primBuf are garbage or zero
         jsize primBufSize, void* primBuf, jsize oopBufLength, jobject* oopBuf);
    jsize (JNICALL *SetFlattenedArrayElement)
      (JNIEnv *env, jobjectArray array, jsize index,
       // two input buffers; oop positions in primBuf are ignored
       jsize primBufSize, void* primBuf, jsize oopBufLength, jobject* oopBuf);

Returns -1 (JNI_ERR) if something goes wrong, such as a deficient
primBufSize or oopBufLength.  (User should check ahead on those.)


> On Nov 22, 2019, at 1:08 PM, Frederic Parain <frederic.parain at oracle.com> wrote:
> 
> Please review these changes adding JNI support for flattened arrays.
> 
> The model for accessing flattened arrays from native code is the same
> as for primitive arrays, with these two new methods:
> 
> void* (JNICALL *GetFlattenedArrayElements)
>       (JNIEnv* env, jarray array , jboolean *isCopy);
> void (JNICALL *ReleaseFlattenedArrayElements)
>       (JNIEnv* env, jarray, void* elem, jint mode);
> 
> Native access to inline types arrays is allowed only if the array is
> flattened and array elements do not contain oops.
> 
> However, native code needs more information to deal with inline types
> arrays:
> 
> In order to get the size of an element of the array:
> 
> jsize (JNICALL *GetFlattenedArrayElementSize)
>       (JNIEnv* env, jarray array);
> 
> In order to get the class of the array elements:
> 
> jclass (JNICALL *GetFlattenedArrayElementClass)
>       (JNIEnv* env, jarray array);
> 
> 
> An in order to get the offset of a field inside an array element, or
> inside a flattened field:
> 
> jsize (JNICALL *GetFieldOffsetInFlattenedLayout)
>       (JNIEnv* env, jclass clazz,  const char *name, const char *signature, jboolean* isFlattened);
> 
> The webrev includes tests showing examples on how to interact with
> a flattened array from native code (initialization, update, sorting).
> 
> Webrev:
> http://cr.openjdk.java.net/~fparain/jniflattenedarrays/webrev.00/index.html
> 
> Thank you,
> 
> Fred
> 
> 



More information about the valhalla-dev mailing list