8149596: Remove java.nio.Bits copy wrapper methods

Reviewed-by: bpb, chegar, psandoz
This commit is contained in:
Mikael Vidstedt 2016-03-02 13:21:20 -08:00
parent 525c0c0972
commit 2255be0267
2 changed files with 25 additions and 208 deletions

View File

@ -736,202 +736,9 @@ class Bits { // package-private
}); });
} }
// -- Bulk get/put acceleration --
// These numbers represent the point at which we have empirically // These numbers represent the point at which we have empirically
// determined that the average cost of a JNI call exceeds the expense // determined that the average cost of a JNI call exceeds the expense
// of an element by element copy. These numbers may change over time. // of an element by element copy. These numbers may change over time.
static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6; static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6;
static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6; static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6;
// This number limits the number of bytes to copy per call to Unsafe's
// copyMemory method. A limit is imposed to allow for safepoint polling
// during a large copy
static final long UNSAFE_COPY_THRESHOLD = 1024L * 1024L;
// These methods do no bounds checking. Verification that the copy will not
// result in memory corruption should be done prior to invocation.
// All positions and lengths are specified in bytes.
/**
* Copy from given source array to destination address.
*
* @param src
* source array
* @param srcBaseOffset
* offset of first element of storage in source array
* @param srcPos
* offset within source array of the first element to read
* @param dstAddr
* destination address
* @param length
* number of bytes to copy
*/
static void copyFromArray(Object src, long srcBaseOffset, long srcPos,
long dstAddr, long length)
{
long offset = srcBaseOffset + srcPos;
while (length > 0) {
long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length;
unsafe.copyMemory(src, offset, null, dstAddr, size);
length -= size;
offset += size;
dstAddr += size;
}
}
/**
* Copy from source address into given destination array.
*
* @param srcAddr
* source address
* @param dst
* destination array
* @param dstBaseOffset
* offset of first element of storage in destination array
* @param dstPos
* offset within destination array of the first element to write
* @param length
* number of bytes to copy
*/
static void copyToArray(long srcAddr, Object dst, long dstBaseOffset, long dstPos,
long length)
{
long offset = dstBaseOffset + dstPos;
while (length > 0) {
long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length;
unsafe.copyMemory(null, srcAddr, dst, offset, size);
length -= size;
srcAddr += size;
offset += size;
}
}
/**
* Copy and unconditionally byte swap 16 bit elements from a heap array to off-heap memory
*
* @param src
* the source array, must be a 16-bit primitive array type
* @param srcPos
* byte offset within source array of the first element to read
* @param dstAddr
* destination address
* @param length
* number of bytes to copy
*/
static void copyFromCharArray(Object src, long srcPos, long dstAddr, long length) {
unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 2);
}
/**
* Copy and unconditionally byte swap 16 bit elements from off-heap memory to a heap array
*
* @param srcAddr
* source address
* @param dst
* destination array, must be a 16-bit primitive array type
* @param dstPos
* byte offset within the destination array of the first element to write
* @param length
* number of bytes to copy
*/
static void copyToCharArray(long srcAddr, Object dst, long dstPos, long length) {
unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 2);
}
/**
* Copy and unconditionally byte swap 16 bit elements from a heap array to off-heap memory
*
* @param src
* the source array, must be a 16-bit primitive array type
* @param srcPos
* byte offset within source array of the first element to read
* @param dstAddr
* destination address
* @param length
* number of bytes to copy
*/
static void copyFromShortArray(Object src, long srcPos, long dstAddr, long length) {
unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 2);
}
/**
* Copy and unconditionally byte swap 16 bit elements from off-heap memory to a heap array
*
* @param srcAddr
* source address
* @param dst
* destination array, must be a 16-bit primitive array type
* @param dstPos
* byte offset within the destination array of the first element to write
* @param length
* number of bytes to copy
*/
static void copyToShortArray(long srcAddr, Object dst, long dstPos, long length) {
unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 2);
}
/**
* Copy and unconditionally byte swap 32 bit elements from a heap array to off-heap memory
*
* @param src
* the source array, must be a 32-bit primitive array type
* @param srcPos
* byte offset within source array of the first element to read
* @param dstAddr
* destination address
* @param length
* number of bytes to copy
*/
static void copyFromIntArray(Object src, long srcPos, long dstAddr, long length) {
unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 4);
}
/**
* Copy and unconditionally byte swap 32 bit elements from off-heap memory to a heap array
*
* @param srcAddr
* source address
* @param dst
* destination array, must be a 32-bit primitive array type
* @param dstPos
* byte offset within the destination array of the first element to write
* @param length
* number of bytes to copy
*/
static void copyToIntArray(long srcAddr, Object dst, long dstPos, long length) {
unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 4);
}
/**
* Copy and unconditionally byte swap 64 bit elements from a heap array to off-heap memory
*
* @param src
* the source array, must be a 64-bit primitive array type
* @param srcPos
* byte offset within source array of the first element to read
* @param dstAddr
* destination address
* @param length
* number of bytes to copy
*/
static void copyFromLongArray(Object src, long srcPos, long dstAddr, long length) {
unsafe.copySwapMemory(src, unsafe.arrayBaseOffset(src.getClass()) + srcPos, null, dstAddr, length, 8);
}
/**
* Copy and unconditionally byte swap 64 bit elements from off-heap memory to a heap array
*
* @param srcAddr
* source address
* @param dst
* destination array, must be a 64-bit primitive array type
* @param dstPos
* byte offset within the destination array of the first element to write
* @param length
* number of bytes to copy
*/
static void copyToLongArray(long srcAddr, Object dst, long dstPos, long length) {
unsafe.copySwapMemory(null, srcAddr, dst, unsafe.arrayBaseOffset(dst.getClass()) + dstPos, length, 8);
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -270,16 +270,22 @@ class Direct$Type$Buffer$RW$$BO$
if (length > rem) if (length > rem)
throw new BufferUnderflowException(); throw new BufferUnderflowException();
long dstOffset = arrayBaseOffset + ((long)offset << $LG_BYTES_PER_VALUE$);
#if[!byte] #if[!byte]
if (order() != ByteOrder.nativeOrder()) if (order() != ByteOrder.nativeOrder())
Bits.copyTo$Memtype$Array(ix(pos), dst, unsafe.copySwapMemory(null,
(long)offset << $LG_BYTES_PER_VALUE$, ix(pos),
(long)length << $LG_BYTES_PER_VALUE$); dst,
dstOffset,
(long)length << $LG_BYTES_PER_VALUE$,
(long)1 << $LG_BYTES_PER_VALUE$);
else else
#end[!byte] #end[!byte]
Bits.copyToArray(ix(pos), dst, arrayBaseOffset, unsafe.copyMemory(null,
(long)offset << $LG_BYTES_PER_VALUE$, ix(pos),
(long)length << $LG_BYTES_PER_VALUE$); dst,
dstOffset,
(long)length << $LG_BYTES_PER_VALUE$);
position(pos + length); position(pos + length);
} else { } else {
super.get(dst, offset, length); super.get(dst, offset, length);
@ -362,18 +368,22 @@ class Direct$Type$Buffer$RW$$BO$
if (length > rem) if (length > rem)
throw new BufferOverflowException(); throw new BufferOverflowException();
long srcOffset = arrayBaseOffset + ((long)offset << $LG_BYTES_PER_VALUE$);
#if[!byte] #if[!byte]
if (order() != ByteOrder.nativeOrder()) if (order() != ByteOrder.nativeOrder())
Bits.copyFrom$Memtype$Array(src, unsafe.copySwapMemory(src,
(long)offset << $LG_BYTES_PER_VALUE$, srcOffset,
ix(pos), null,
(long)length << $LG_BYTES_PER_VALUE$); ix(pos),
(long)length << $LG_BYTES_PER_VALUE$,
(long)1 << $LG_BYTES_PER_VALUE$);
else else
#end[!byte] #end[!byte]
Bits.copyFromArray(src, arrayBaseOffset, unsafe.copyMemory(src,
(long)offset << $LG_BYTES_PER_VALUE$, srcOffset,
ix(pos), null,
(long)length << $LG_BYTES_PER_VALUE$); ix(pos),
(long)length << $LG_BYTES_PER_VALUE$);
position(pos + length); position(pos + length);
} else { } else {
super.put(src, offset, length); super.put(src, offset, length);