Faster memory copy between direct buffer and byte array
This commit is contained in:
parent
e424a2f4b3
commit
9a676bc7d5
@ -106,7 +106,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
PooledUnsafeDirectByteBuf bbdst = (PooledUnsafeDirectByteBuf) dst;
|
||||
PlatformDependent.copyMemory(addr(index), bbdst.addr(dstIndex), length);
|
||||
} else if (dst.hasArray()) {
|
||||
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
} else {
|
||||
dst.setBytes(dstIndex, this, index, length);
|
||||
}
|
||||
@ -207,7 +207,7 @@ final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
PooledUnsafeDirectByteBuf bbsrc = (PooledUnsafeDirectByteBuf) src;
|
||||
PlatformDependent.copyMemory(bbsrc.addr(srcIndex), addr(index), length);
|
||||
} else if (src.hasArray()) {
|
||||
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
|
||||
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
|
||||
} else {
|
||||
src.getBytes(srcIndex, this, index, length);
|
||||
}
|
||||
|
@ -47,9 +47,11 @@ public final class PlatformDependent {
|
||||
private static final boolean HAS_UNSAFE = hasUnsafe0();
|
||||
private static final boolean CAN_FREE_DIRECT_BUFFER = canFreeDirectBuffer0();
|
||||
private static final boolean IS_UNALIGNED = isUnaligned0();
|
||||
private static final long ARRAY_BASE_OFFSET = arrayBaseOffset0();
|
||||
|
||||
private static final boolean HAS_JAVASSIST = hasJavassist0();
|
||||
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the current platform is Android
|
||||
*/
|
||||
@ -173,6 +175,14 @@ public final class PlatformDependent {
|
||||
PlatformDependent0.copyMemory(srcAddr, dstAddr, length);
|
||||
}
|
||||
|
||||
public static void copyMemory(byte[] src, int srcIndex, long dstAddr, long length) {
|
||||
PlatformDependent0.copyMemory(src, ARRAY_BASE_OFFSET + srcIndex, null, dstAddr, length);
|
||||
}
|
||||
|
||||
public static void copyMemory(long srcAddr, byte[] dst, int dstIndex, long length) {
|
||||
PlatformDependent0.copyMemory(null, srcAddr, dst, ARRAY_BASE_OFFSET + dstIndex, length);
|
||||
}
|
||||
|
||||
private static boolean isAndroid0() {
|
||||
boolean android;
|
||||
try {
|
||||
@ -284,6 +294,14 @@ public final class PlatformDependent {
|
||||
return PlatformDependent0.isUnaligned();
|
||||
}
|
||||
|
||||
private static long arrayBaseOffset0() {
|
||||
if (!hasUnsafe()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return PlatformDependent0.arrayBaseOffset();
|
||||
}
|
||||
|
||||
private static boolean hasJavassist0() {
|
||||
boolean noJavassist = SystemPropertyUtil.getBoolean("io.netty.noJavassist", false);
|
||||
if (noJavassist) {
|
||||
|
@ -127,6 +127,10 @@ final class PlatformDependent0 {
|
||||
return UNALIGNED;
|
||||
}
|
||||
|
||||
static long arrayBaseOffset() {
|
||||
return UNSAFE.arrayBaseOffset(byte[].class);
|
||||
}
|
||||
|
||||
static Object getObject(Object object, long fieldOffset) {
|
||||
return UNSAFE.getObject(object, fieldOffset);
|
||||
}
|
||||
@ -175,6 +179,10 @@ final class PlatformDependent0 {
|
||||
UNSAFE.copyMemory(srcAddr, dstAddr, length);
|
||||
}
|
||||
|
||||
static void copyMemory(Object src, long srcOffset, Object dst, long dstOffset, long length) {
|
||||
UNSAFE.copyMemory(src, srcOffset, dst, dstOffset, length);
|
||||
}
|
||||
|
||||
private PlatformDependent0() {
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user