[foreign-memaccess+abi] RFR: Add support for high-level functions to copy to and from Java arrays [v5]
John Rose
john.r.rose at oracle.com
Thu Jun 24 20:50:49 UTC 2021
On Jun 24, 2021, at 1:38 PM, Maurizio Cimadamore <maurizio.cimadamore at oracle.com<mailto:maurizio.cimadamore at oracle.com>> wrote:
So:
- copyToArray(srcSegment, srcOffsetBytes, dstArray, dstIndexChars, numChars, order);
- copyFromArray(srcArray, srcIndexChars, dstSegment, dstOffsetBytes, numChars, order);
or
- copyToArray(srcSegment, srcOffsetBytes, dstArray, dstIndexChars, numChars, order);
- copyFromArray(dstSegment, dstOffsetBytes, srcArray, srcIndexChars, numChars, order);
Depending on how the other parameters should be sorted. I personally prefer:
* source first
* destination second
* length last (and order after that)
(which means the first of the two proposal above).
As John says, that's how System::arrayCopy (and many other libs) do it, I don't think I see the need for going down another path.
Is there a specific concern you have against that?
It feels like violent agreement here: System::arraycopy
has left-to-right data flow (yay, also Lee’s point!), and
ends with the common length value.
(Plus the byte order at the end too. I’d sort that at
the very last, because it is the most neglect-able.)
You know, you could almost name this primitive
“arraycopy” and just rely on overloads. The “From”
and “To” words are a little noisy, and they may
distract readers into thinking that they affect
argument order. I think we agree they shouldn’t;
it’s simpler to have a left-to-right rule.
I was recently working on a non-static version of
a similar API (bulk copies!), and in that case the
receiver object was always the non-array. That
requires help from the name, in the form of
“In” and “Out” words which tell whether data
is entering or leaving the receiver object:
interface Words {
// bulk copy
default long copyIn(long destPos, long[] src, int srcStart, int srcEnd) {
for (int i = srcStart; i < srcEnd; i++)
set(destPos++, src[i]);
return destPos;
}
default int copyOut(long start, long end, long[] dest, int destPos) {
for (long i = start; i < end; i++)
dest[destPos++] = get(i);
return destPos;
}
long length();
long get(long index);
void set(long index, long value);
…
}
But for a static method, it’s better to have a static
ordering of arguments!
More information about the panama-dev
mailing list