Second JNI extension for flattened arrays

Frederic Parain frederic.parain at
Tue Dec 17 17:01:01 UTC 2019

I’ve forgot to add the examples:


    inline class Conainee {
    	float f;
	short s;

	Containee(float f, short s) {
	    this.f = f;
	    this.s = s;

    inline class ValueWithOops {
	String s = "bonjour";
	int i = 0;
	Containee c = new Containee(2.3f, (short)4); // flattened inline field
	BigValue b = new BigValue();                 // non-flattened inline field

To create a subelement for an array of ValueWithOops:

   jobject selector1 = (*env)->CreateSubElementSelector(env, array);

This SubElementSelector designates a whole element in the array.

To create a sub-element selector for the field s of class ValueWithOops:

   jobject selector2  (*env)->GetSubElementSelector(env, selector1, "s", "Ljava/lang/String;");

To read field s of the element at index 4 of the array:

  jobject s = (*env)->GetObjectSubElement(env, array, selector2, 4);

To create a sub-element selector for the field s in the flattened field c of an element of type ValueWithOops:

  jobject selector3 = (*env)->GetSubElementSelector(env, selector1, "c", "QContainee;");
  jobject selector4 = (*env)->GetSubElementSelector(env, selector3, "s", "S");

To update the value of field s in the flattened field c in the element stored at index 8:

  (*env)->SetShortSubElement(env, array, selector4, 8, (short)12);

To read flattened field c from the element at index 1:

  jobject c = (*env)->GetObjectSubElement(env, array, selector3, 1);

The API takes care of extracting the flattened field before returning it as an oop.

> On Dec 17, 2019, at 11:37, Frederic Parain <frederic.parain at> wrote:
> Here’s a second JNI extension for flattened arrays.
> The first extension (JDK-8234761) was designed to provide fast native access
> to flattened arrays of pure primitives inline types.
> This second extension has a different goal: it aims at providing easy access
> to any kind of flattened array.
> One pain point of flattened arrays is that even if a code needs a single
> field from a flattened value stored in an array, it usually has to get
> the whole element first, and then get the field from the element. With
> flattened fields, this process can include several more steps to get the
> flattened field before being able to get one field from this flattened field.
> This proposal of a new JNI extension tries to avoid this burden by defining
> the notion of sub-element and sub-element selector.
> A sub-element represents either a whole element of a flattened array, or a
> field stored in an element of a flattened array. A sub-element can be at
> any level of nesting: if the array element includes flattened fields which
> themselves include flattened field, any field of any of these flattened
> fields is a sub-element. Any kind of field is a sub-element: primitive
> fields, reference fields, or flattened fields.
> The new JNI APIs provide a way to create sub-element selectors, Java objects
> designating a particular sub-element of a flattened array. They also provide
> methods to directly read or write sub-elements without having to go through
> all steps of nesting.
> Webrev:
> Comments are welcome.
> Thank you,
> Fred

More information about the valhalla-dev mailing list