[foreign] Pointer to fixed-size array: is jexctract's mapping to Java correct?

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Feb 28 13:50:20 UTC 2019


On 28/02/2019 13:16, Lev Serebryakov wrote:
> Yep. And copy data from "java native" array and back. It will dominate
> execution time for small transform sizes, I'm afraid — copying from
> native Java array to these pairs. Performance-wise, best solution will
> be to have `Array<Double>` transparently backed by `double[]`, it will
> mimic C code as close as possibly (and as fast as possibly).

So, if efficiency is your main concern, I'd suggest to:

1) allocate a double array, as this:

Array<Double> buffer  = Scope.allocateArray(NativeTypes.DOUBLE, npairs * 
sizeofDouble * 2);

2) Fill in the buffer with a for loop - no allocation is required - just 
set elements at position 'n'

3) When you are done, do an (unsafe!) cast to what you want:

Array<Array<Double>> arr = 
buffer.elementPointer().cast(NativeTypes.VOID).cast(NativeTypes.DOUBLE.array(2)).withSize(npairs);

This should give you what you want. No allocation, just fill in the 
buffer, convert to the type the API expects, pass it onto the API.

I'm also thinking about ways to let the API see that this cast is not as 
'unsafe' as it looks (as the underlying layout is the same) - more on 
that in a separate email.

Maurizio



More information about the panama-dev mailing list