This is an automated email from the ASF dual-hosted git repository. leerho pushed a commit to branch 6.0.X_cherrypick_tgt in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git
commit ed6c611f2a360c8ac8df79f1a88911a19b3f292e Author: Lee Rhodes <[email protected]> AuthorDate: Fri Jan 31 15:43:21 2025 -0800 Fixed all the tests which lacked the ability to test this bug. At the same time, I refactored the leaf classes to use the new APIs in Java 21 FFM, this should have been done before. Nonetheless, this eliminated a lot of unnecessary code. --- .../memory/internal/NativeWritableBufferImpl.java | 51 +--- .../memory/internal/NativeWritableMemoryImpl.java | 115 +------ .../internal/NonNativeWritableBufferImpl.java | 49 +-- .../internal/NonNativeWritableMemoryImpl.java | 117 +------- .../memory/internal/WritableBufferImpl.java | 13 - .../memory/internal/WritableMemoryImpl.java | 5 +- .../internal/NativeWritableBufferImplTest.java | 263 ++++++++++------ .../internal/NativeWritableMemoryImplTest.java | 239 ++++++++++----- .../internal/NonNativeWritableBufferImplTest.java | 330 +++++++++------------ .../internal/NonNativeWritableMemoryImplTest.java | 229 +++++++------- 10 files changed, 667 insertions(+), 744 deletions(-) diff --git a/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java index 31b2f64..27b7751 100644 --- a/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java +++ b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java @@ -25,18 +25,6 @@ import static java.lang.foreign.ValueLayout.JAVA_FLOAT_UNALIGNED; import static java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED; import static java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED; import static java.lang.foreign.ValueLayout.JAVA_SHORT_UNALIGNED; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.getCharArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.getDoubleArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.getFloatArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.getIntArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.getLongArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.getShortArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.putCharArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.putDoubleArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.putFloatArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.putIntArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.putLongArr; -import static org.apache.datasketches.memory.internal.NativeWritableMemoryImpl.putShortArr; import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; @@ -44,19 +32,6 @@ import java.lang.foreign.MemorySegment; import org.apache.datasketches.memory.MemoryRequestServer; import org.apache.datasketches.memory.WritableBuffer; -/* - * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill, - * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at - * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to - * the rest of the work these methods are doing. - * - * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and - * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove - * them entirely from production runtime code. The offset versions of the light weight methods will - * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is - * often a single CPU instruction. - */ - /** * Implementation of {@link WritableBuffer} for native endian byte order. * @author Roman Leventov @@ -64,6 +39,7 @@ import org.apache.datasketches.memory.WritableBuffer; */ final class NativeWritableBufferImpl extends WritableBufferImpl { + //Pass-through ctor NativeWritableBufferImpl( final Arena arena, final MemorySegment seg, @@ -88,7 +64,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void getCharArray(final char[] dstArray, final int dstOffsetChars, final int lengthChars) { final long pos = getPosition(); - getCharArr(seg, pos, dstArray, dstOffsetChars, lengthChars); + MemorySegment.copy(seg, JAVA_CHAR_UNALIGNED, pos, dstArray, dstOffsetChars, lengthChars); setPosition(pos + (lengthChars << CHAR_SHIFT)); } @@ -107,7 +83,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void getDoubleArray(final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) { final long pos = getPosition(); - getDoubleArr(seg, pos, dstArray, dstOffsetDoubles, lengthDoubles); + MemorySegment.copy(seg, JAVA_DOUBLE_UNALIGNED, pos, dstArray, dstOffsetDoubles, lengthDoubles); setPosition(pos + (lengthDoubles << DOUBLE_SHIFT)); } @@ -126,7 +102,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void getFloatArray(final float[] dstArray, final int dstOffsetFloats, final int lengthFloats) { final long pos = getPosition(); - getFloatArr(seg, pos, dstArray, dstOffsetFloats, lengthFloats); + MemorySegment.copy(seg, JAVA_FLOAT_UNALIGNED, pos, dstArray, dstOffsetFloats, lengthFloats); setPosition(pos + (lengthFloats << FLOAT_SHIFT)); } @@ -145,7 +121,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void getIntArray(final int[] dstArray, final int dstOffsetInts, final int lengthInts) { final long pos = getPosition(); - getIntArr(seg, pos, dstArray, dstOffsetInts, lengthInts); + MemorySegment.copy(seg, JAVA_INT_UNALIGNED, pos, dstArray, dstOffsetInts, lengthInts); setPosition(pos + (lengthInts << INT_SHIFT)); } @@ -164,7 +140,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void getLongArray(final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) { final long pos = getPosition(); - getLongArr(seg, pos, dstArray, dstOffsetLongs, lengthLongs); + MemorySegment.copy(seg, JAVA_LONG_UNALIGNED, pos, dstArray, dstOffsetLongs, lengthLongs); setPosition(pos + (lengthLongs << LONG_SHIFT)); } @@ -183,7 +159,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void getShortArray(final short[] dstArray, final int dstOffsetShorts, final int lengthShorts) { final long pos = getPosition(); - getShortArr(seg, pos, dstArray, dstOffsetShorts, lengthShorts); + MemorySegment.copy(seg, JAVA_SHORT_UNALIGNED, pos, dstArray, dstOffsetShorts, lengthShorts); setPosition(pos + (lengthShorts << SHORT_SHIFT)); } @@ -203,7 +179,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void putCharArray(final char[] srcArray, final int srcOffsetChars, final int lengthChars) { final long pos = getPosition(); - putCharArr(seg, pos, srcArray, srcOffsetChars, lengthChars); + MemorySegment.copy(srcArray, srcOffsetChars, seg, JAVA_CHAR_UNALIGNED, pos, lengthChars); setPosition(pos + (lengthChars << CHAR_SHIFT)); } @@ -222,7 +198,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void putDoubleArray(final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) { final long pos = getPosition(); - putDoubleArr(seg, pos, srcArray, srcOffsetDoubles, lengthDoubles); + MemorySegment.copy(srcArray, srcOffsetDoubles, seg, JAVA_DOUBLE_UNALIGNED, pos, lengthDoubles); setPosition(pos + (lengthDoubles << DOUBLE_SHIFT)); } @@ -241,7 +217,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void putFloatArray(final float[] srcArray, final int srcOffsetFloats, final int lengthFloats) { final long pos = getPosition(); - putFloatArr(seg, pos, srcArray, srcOffsetFloats, lengthFloats); + MemorySegment.copy(srcArray, srcOffsetFloats, seg, JAVA_FLOAT_UNALIGNED, pos, lengthFloats); setPosition(pos + (lengthFloats << FLOAT_SHIFT)); } @@ -260,7 +236,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void putIntArray(final int[] srcArray, final int srcOffsetInts, final int lengthInts) { final long pos = getPosition(); - putIntArr(seg, pos, srcArray, srcOffsetInts, lengthInts); + MemorySegment.copy(srcArray, srcOffsetInts, seg, JAVA_INT_UNALIGNED, pos, lengthInts); setPosition(pos + (lengthInts << INT_SHIFT)); } @@ -279,7 +255,7 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void putLongArray(final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) { final long pos = getPosition(); - putLongArr(seg, pos, srcArray, srcOffsetLongs, lengthLongs); + MemorySegment.copy(srcArray, srcOffsetLongs, seg, JAVA_LONG_UNALIGNED, pos, lengthLongs); setPosition(pos + (lengthLongs << LONG_SHIFT)); } @@ -298,7 +274,8 @@ final class NativeWritableBufferImpl extends WritableBufferImpl { @Override public void putShortArray(final short[] srcArray, final int srcOffsetShorts, final int lengthShorts) { final long pos = getPosition(); - putShortArr(seg, pos, srcArray, srcOffsetShorts, lengthShorts); + MemorySegment.copy(srcArray, srcOffsetShorts, seg, JAVA_SHORT_UNALIGNED, pos, lengthShorts); setPosition(pos + (lengthShorts << SHORT_SHIFT)); } + } diff --git a/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java index 4c5222a..57649a3 100644 --- a/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java +++ b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java @@ -19,17 +19,11 @@ package org.apache.datasketches.memory.internal; -import static java.lang.foreign.ValueLayout.JAVA_CHAR; import static java.lang.foreign.ValueLayout.JAVA_CHAR_UNALIGNED; -import static java.lang.foreign.ValueLayout.JAVA_DOUBLE; import static java.lang.foreign.ValueLayout.JAVA_DOUBLE_UNALIGNED; -import static java.lang.foreign.ValueLayout.JAVA_FLOAT; import static java.lang.foreign.ValueLayout.JAVA_FLOAT_UNALIGNED; -import static java.lang.foreign.ValueLayout.JAVA_INT; import static java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED; -import static java.lang.foreign.ValueLayout.JAVA_LONG; import static java.lang.foreign.ValueLayout.JAVA_LONG_UNALIGNED; -import static java.lang.foreign.ValueLayout.JAVA_SHORT; import static java.lang.foreign.ValueLayout.JAVA_SHORT_UNALIGNED; import java.lang.foreign.Arena; @@ -55,7 +49,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { super(seg, typeId, memReqSvr, arena); } - ///PRIMITIVE getX() and getXArray() + //PRIMITIVE getX() and getXArray() @Override public char getChar(final long offsetBytes) { return seg.get(ValueLayout.JAVA_CHAR_UNALIGNED, offsetBytes); @@ -63,14 +57,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getCharArray(final long offsetBytes, final char[] dstArray, final int dstOffsetChars, final int lengthChars) { - getCharArr(seg, offsetBytes, dstArray, dstOffsetChars, lengthChars); - } - - static void getCharArr( - final MemorySegment seg, final long offsetBytes, final char[] dstArray, final int dstOffsetChars, final int lengthChars) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetChars) << CHAR_SHIFT; - MemorySegment.copy(seg, JAVA_CHAR_UNALIGNED, offsetBytes, dstSeg, JAVA_CHAR, dstOffsetBytes, lengthChars); + MemorySegment.copy(seg, JAVA_CHAR_UNALIGNED, offsetBytes, dstArray, dstOffsetChars, lengthChars); } @Override @@ -80,14 +67,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getDoubleArray(final long offsetBytes, final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) { - getDoubleArr(seg, offsetBytes, dstArray, dstOffsetDoubles, lengthDoubles); - } - - static void getDoubleArr( - final MemorySegment seg, final long offsetBytes, final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetDoubles) << DOUBLE_SHIFT; - MemorySegment.copy(seg, JAVA_DOUBLE_UNALIGNED, offsetBytes, dstSeg, JAVA_DOUBLE, dstOffsetBytes, lengthDoubles); + MemorySegment.copy(seg, JAVA_DOUBLE_UNALIGNED, offsetBytes, dstArray, dstOffsetDoubles, lengthDoubles); } @Override @@ -97,14 +77,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getFloatArray(final long offsetBytes, final float[] dstArray, final int dstOffsetFloats, final int lengthFloats) { - getFloatArr(seg, offsetBytes, dstArray, dstOffsetFloats, lengthFloats); - } - - static void getFloatArr( - final MemorySegment seg, final long offsetBytes, final float[] dstArray, final int dstOffsetFloats, final int lengthFloats) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetFloats) << FLOAT_SHIFT; - MemorySegment.copy(seg, JAVA_FLOAT_UNALIGNED, offsetBytes, dstSeg, JAVA_FLOAT, dstOffsetBytes, lengthFloats); + MemorySegment.copy(seg, JAVA_FLOAT_UNALIGNED, offsetBytes, dstArray, dstOffsetFloats, lengthFloats); } @Override @@ -114,14 +87,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getIntArray(final long offsetBytes, final int[] dstArray, final int dstOffsetInts, final int lengthInts) { - getIntArr(seg, offsetBytes, dstArray, dstOffsetInts, lengthInts); - } - - static void getIntArr( - final MemorySegment seg, final long offsetBytes, final int[] dstArray, final int dstOffsetInts, final int lengthInts) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetInts) << INT_SHIFT; - MemorySegment.copy(seg, JAVA_INT_UNALIGNED, offsetBytes, dstSeg, JAVA_INT, dstOffsetBytes, lengthInts); + MemorySegment.copy(seg, JAVA_INT_UNALIGNED, offsetBytes, dstArray, dstOffsetInts, lengthInts); } @Override @@ -131,14 +97,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getLongArray(final long offsetBytes, final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) { - getLongArr(seg, offsetBytes, dstArray, dstOffsetLongs, lengthLongs); - } - - static void getLongArr( - final MemorySegment seg, final long offsetBytes, final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetLongs) << LONG_SHIFT; - MemorySegment.copy(seg, JAVA_LONG_UNALIGNED, offsetBytes, dstSeg, JAVA_LONG, dstOffsetBytes, lengthLongs); + MemorySegment.copy(seg, JAVA_LONG_UNALIGNED, offsetBytes, dstArray, dstOffsetLongs, lengthLongs); } @Override @@ -148,14 +107,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getShortArray(final long offsetBytes, final short[] dstArray, final int dstOffsetShorts, final int lengthShorts) { - getShortArr(seg, offsetBytes, dstArray, dstOffsetShorts, lengthShorts); - } - - static void getShortArr( - final MemorySegment seg, final long offsetBytes, final short[] dstArray, final int dstOffsetShorts, final int lengthShorts) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetShorts) << SHORT_SHIFT; - MemorySegment.copy(seg, JAVA_SHORT_UNALIGNED, offsetBytes, dstSeg, JAVA_SHORT, dstOffsetBytes, lengthShorts); + MemorySegment.copy(seg, JAVA_SHORT_UNALIGNED, offsetBytes, dstArray, dstOffsetShorts, lengthShorts); } //PRIMITIVE putX() and putXArray() implementations @@ -166,14 +118,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putCharArray(final long offsetBytes, final char[] srcArray, final int srcOffsetChars, final int lengthChars) { - putCharArr(seg, offsetBytes, srcArray, srcOffsetChars, lengthChars); - } - - static void putCharArr( - final MemorySegment seg, final long offsetBytes, final char[] srcArray, final int srcOffsetChars, final int lengthChars) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetChars) << CHAR_SHIFT; - MemorySegment.copy(srcSeg, JAVA_CHAR, srcOffsetBytes, seg, JAVA_CHAR_UNALIGNED, offsetBytes, lengthChars); + MemorySegment.copy(srcArray, srcOffsetChars, seg, JAVA_CHAR_UNALIGNED, offsetBytes, lengthChars); } @Override @@ -183,14 +128,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putDoubleArray(final long offsetBytes, final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) { - putDoubleArr(seg, offsetBytes, srcArray, srcOffsetDoubles, lengthDoubles); - } - - static void putDoubleArr( - final MemorySegment seg, final long offsetBytes, final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetDoubles) << DOUBLE_SHIFT; - MemorySegment.copy(srcSeg, JAVA_DOUBLE, srcOffsetBytes, seg, JAVA_DOUBLE_UNALIGNED, offsetBytes, lengthDoubles); + MemorySegment.copy(srcArray, srcOffsetDoubles, seg, JAVA_DOUBLE_UNALIGNED, offsetBytes, lengthDoubles); } @Override @@ -200,14 +138,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putFloatArray(final long offsetBytes, final float[] srcArray, final int srcOffsetFloats, final int lengthFloats) { - putFloatArr(seg, offsetBytes, srcArray, srcOffsetFloats, lengthFloats); - } - - static void putFloatArr( - final MemorySegment seg, final long offsetBytes, final float[] srcArray, final int srcOffsetFloats, final int lengthFloats) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetFloats) << FLOAT_SHIFT; - MemorySegment.copy(srcSeg, JAVA_FLOAT, srcOffsetBytes, seg, JAVA_FLOAT_UNALIGNED, offsetBytes, lengthFloats); + MemorySegment.copy(srcArray, srcOffsetFloats, seg, JAVA_FLOAT_UNALIGNED, offsetBytes, lengthFloats); } @Override @@ -217,14 +148,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putIntArray(final long offsetBytes, final int[] srcArray, final int srcOffsetInts, final int lengthInts) { - putIntArr(seg, offsetBytes, srcArray, srcOffsetInts, lengthInts); - } - - static void putIntArr( - final MemorySegment seg, final long offsetBytes, final int[] srcArray, final int srcOffsetInts, final int lengthInts) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetInts) << INT_SHIFT; - MemorySegment.copy(srcSeg, JAVA_INT, srcOffsetBytes, seg, JAVA_INT_UNALIGNED, offsetBytes, lengthInts); + MemorySegment.copy(srcArray, srcOffsetInts, seg, JAVA_INT_UNALIGNED, offsetBytes, lengthInts); } @Override @@ -234,14 +158,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putLongArray(final long offsetBytes, final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) { - putLongArr(seg, offsetBytes, srcArray, srcOffsetLongs, lengthLongs); - } - - static void putLongArr( - final MemorySegment seg, final long offsetBytes, final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetLongs) << LONG_SHIFT; - MemorySegment.copy(srcSeg, JAVA_LONG, srcOffsetBytes, seg, JAVA_LONG_UNALIGNED, offsetBytes, lengthLongs); + MemorySegment.copy(srcArray, srcOffsetLongs, seg, JAVA_LONG_UNALIGNED, offsetBytes, lengthLongs); } @Override @@ -251,13 +168,7 @@ final class NativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putShortArray(final long offsetBytes, final short[] srcArray, final int srcOffsetShorts, final int lengthShorts) { - putShortArr(seg, offsetBytes, srcArray, srcOffsetShorts, lengthShorts); + MemorySegment.copy(srcArray, srcOffsetShorts, seg, JAVA_SHORT_UNALIGNED, offsetBytes, lengthShorts); } - static void putShortArr( - final MemorySegment seg, final long offsetBytes, final short[] srcArray, final int srcOffsetShorts, final int lengthShorts) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetShorts) << SHORT_SHIFT; - MemorySegment.copy(srcSeg, JAVA_SHORT, srcOffsetBytes, seg, JAVA_SHORT_UNALIGNED, offsetBytes, lengthShorts); - } } diff --git a/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java index 29c5b1c..a527d3a 100644 --- a/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java +++ b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java @@ -25,18 +25,6 @@ import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA_INT_UNALIGNED_NON_NATIVE; import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA_LONG_UNALIGNED_NON_NATIVE; import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA_SHORT_UNALIGNED_NON_NATIVE; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.getCharArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.getDoubleArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.getFloatArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.getIntArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.getLongArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.getShortArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.putCharArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.putDoubleArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.putFloatArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.putIntArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.putLongArr; -import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImpl.putShortArr; import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; @@ -44,19 +32,6 @@ import java.lang.foreign.MemorySegment; import org.apache.datasketches.memory.MemoryRequestServer; import org.apache.datasketches.memory.WritableBuffer; -/* - * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill, - * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at - * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to - * the rest of the work these methods are doing. - * - * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and - * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove - * them entirely from production runtime code. The offset versions of the light weight methods will - * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is - * often a single CPU instruction. - */ - /** * Implementation of {@link WritableBuffer} for non-native endian byte order. * @author Roman Leventov @@ -89,7 +64,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void getCharArray(final char[] dstArray, final int dstOffsetChars, final int lengthChars) { final long pos = getPosition(); - getCharArr(seg, pos, dstArray, dstOffsetChars, lengthChars); + MemorySegment.copy(seg, JAVA_CHAR_UNALIGNED_NON_NATIVE, pos, dstArray, dstOffsetChars, lengthChars); setPosition(pos + (lengthChars << CHAR_SHIFT)); } @@ -108,7 +83,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void getDoubleArray(final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) { final long pos = getPosition(); - getDoubleArr(seg, pos, dstArray, dstOffsetDoubles, lengthDoubles); + MemorySegment.copy(seg, JAVA_DOUBLE_UNALIGNED_NON_NATIVE, pos, dstArray, dstOffsetDoubles, lengthDoubles); setPosition(pos + (lengthDoubles << DOUBLE_SHIFT)); } @@ -127,7 +102,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void getFloatArray(final float[] dstArray, final int dstOffsetFloats, final int lengthFloats) { final long pos = getPosition(); - getFloatArr(seg, pos, dstArray, dstOffsetFloats, lengthFloats); + MemorySegment.copy(seg, JAVA_FLOAT_UNALIGNED_NON_NATIVE, pos, dstArray, dstOffsetFloats, lengthFloats); setPosition(pos + (lengthFloats << FLOAT_SHIFT)); } @@ -146,7 +121,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void getIntArray(final int[] dstArray, final int dstOffsetInts, final int lengthInts) { final long pos = getPosition(); - getIntArr(seg, pos, dstArray, dstOffsetInts, lengthInts); + MemorySegment.copy(seg, JAVA_INT_UNALIGNED_NON_NATIVE, pos, dstArray, dstOffsetInts, lengthInts); setPosition(pos + (lengthInts << INT_SHIFT)); } @@ -165,7 +140,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void getLongArray(final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) { final long pos = getPosition(); - getLongArr(seg, pos, dstArray, dstOffsetLongs, lengthLongs); + MemorySegment.copy(seg, JAVA_LONG_UNALIGNED_NON_NATIVE, pos, dstArray, dstOffsetLongs, lengthLongs); setPosition(pos + (lengthLongs << LONG_SHIFT)); } @@ -184,7 +159,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void getShortArray(final short[] dstArray, final int dstOffsetShorts, final int lengthShorts) { final long pos = getPosition(); - getShortArr(seg, pos, dstArray, dstOffsetShorts, lengthShorts); + MemorySegment.copy(seg, JAVA_SHORT_UNALIGNED_NON_NATIVE, pos, dstArray, dstOffsetShorts, lengthShorts); setPosition(pos + (lengthShorts << SHORT_SHIFT)); } @@ -204,7 +179,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void putCharArray(final char[] srcArray, final int srcOffsetChars, final int lengthChars) { final long pos = getPosition(); - putCharArr(seg, pos, srcArray, srcOffsetChars, lengthChars); + MemorySegment.copy(srcArray, srcOffsetChars, seg, JAVA_CHAR_UNALIGNED_NON_NATIVE, pos, lengthChars); setPosition(pos + (lengthChars << CHAR_SHIFT)); } @@ -223,7 +198,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void putDoubleArray(final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) { final long pos = getPosition(); - putDoubleArr(seg, pos, srcArray, srcOffsetDoubles, lengthDoubles); + MemorySegment.copy(srcArray, srcOffsetDoubles, seg, JAVA_DOUBLE_UNALIGNED_NON_NATIVE, pos, lengthDoubles); setPosition(pos + (lengthDoubles << DOUBLE_SHIFT)); } @@ -242,7 +217,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void putFloatArray(final float[] srcArray, final int srcOffsetFloats, final int lengthFloats) { final long pos = getPosition(); - putFloatArr(seg, pos, srcArray, srcOffsetFloats, lengthFloats); + MemorySegment.copy(srcArray, srcOffsetFloats, seg, JAVA_FLOAT_UNALIGNED_NON_NATIVE, pos, lengthFloats); setPosition(pos + (lengthFloats << FLOAT_SHIFT)); } @@ -261,7 +236,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void putIntArray(final int[] srcArray, final int srcOffsetInts, final int lengthInts) { final long pos = getPosition(); - putIntArr(seg, pos, srcArray, srcOffsetInts, lengthInts); + MemorySegment.copy(srcArray, srcOffsetInts, seg, JAVA_INT_UNALIGNED_NON_NATIVE, pos, lengthInts); setPosition(pos + (lengthInts << INT_SHIFT)); } @@ -280,7 +255,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void putLongArray(final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) { final long pos = getPosition(); - putLongArr(seg, pos, srcArray, srcOffsetLongs, lengthLongs); + MemorySegment.copy(srcArray, srcOffsetLongs, seg, JAVA_LONG_UNALIGNED_NON_NATIVE, pos, lengthLongs); setPosition(pos + (lengthLongs << LONG_SHIFT)); } @@ -299,7 +274,7 @@ final class NonNativeWritableBufferImpl extends WritableBufferImpl { @Override public void putShortArray(final short[] srcArray, final int srcOffsetShorts, final int lengthShorts) { final long pos = getPosition(); - putShortArr(seg, pos, srcArray, srcOffsetShorts, lengthShorts); + MemorySegment.copy(srcArray, srcOffsetShorts, seg, JAVA_SHORT_UNALIGNED_NON_NATIVE, pos, lengthShorts); setPosition(pos + (lengthShorts << SHORT_SHIFT)); } diff --git a/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java index 5744ffd..68c9099 100644 --- a/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java +++ b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java @@ -19,12 +19,6 @@ package org.apache.datasketches.memory.internal; -import static java.lang.foreign.ValueLayout.JAVA_CHAR; -import static java.lang.foreign.ValueLayout.JAVA_DOUBLE; -import static java.lang.foreign.ValueLayout.JAVA_FLOAT; -import static java.lang.foreign.ValueLayout.JAVA_INT; -import static java.lang.foreign.ValueLayout.JAVA_LONG; -import static java.lang.foreign.ValueLayout.JAVA_SHORT; import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA_CHAR_UNALIGNED_NON_NATIVE; import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA_DOUBLE_UNALIGNED_NON_NATIVE; import static org.apache.datasketches.memory.internal.NonNativeValueLayouts.JAVA_FLOAT_UNALIGNED_NON_NATIVE; @@ -37,6 +31,7 @@ import java.lang.foreign.MemorySegment; import org.apache.datasketches.memory.MemoryRequestServer; import org.apache.datasketches.memory.WritableMemory; +// /** * Implementation of {@link WritableMemory} for non-native endian byte order. @@ -54,7 +49,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { super(seg, typeId, memReqSvr, arena); } - ///PRIMITIVE getX() and getXArray() + //PRIMITIVE getX() and getXArray() @Override public char getChar(final long offsetBytes) { return seg.get(JAVA_CHAR_UNALIGNED_NON_NATIVE, offsetBytes); @@ -62,14 +57,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getCharArray(final long offsetBytes, final char[] dstArray, final int dstOffsetChars, final int lengthChars) { - getCharArr(seg, offsetBytes, dstArray, dstOffsetChars, lengthChars); - } - - static void getCharArr( - final MemorySegment seg, final long offsetBytes, final char[] dstArray, final int dstOffsetChars, final int lengthChars) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetChars) << CHAR_SHIFT; - MemorySegment.copy(seg, JAVA_CHAR_UNALIGNED_NON_NATIVE, offsetBytes, dstSeg, JAVA_CHAR, dstOffsetBytes, lengthChars); + MemorySegment.copy(seg, JAVA_CHAR_UNALIGNED_NON_NATIVE, offsetBytes, dstArray, dstOffsetChars, lengthChars); } @Override @@ -79,14 +67,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getDoubleArray(final long offsetBytes, final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) { - getDoubleArr(seg, offsetBytes, dstArray, dstOffsetDoubles, lengthDoubles); - } - - static void getDoubleArr( - final MemorySegment seg, final long offsetBytes, final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetDoubles) << DOUBLE_SHIFT; - MemorySegment.copy(seg, JAVA_DOUBLE_UNALIGNED_NON_NATIVE, offsetBytes, dstSeg, JAVA_DOUBLE, dstOffsetBytes, lengthDoubles); + MemorySegment.copy(seg, JAVA_DOUBLE_UNALIGNED_NON_NATIVE, offsetBytes, dstArray, dstOffsetDoubles, lengthDoubles); } @Override @@ -96,14 +77,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getFloatArray(final long offsetBytes, final float[] dstArray, final int dstOffsetFloats, final int lengthFloats) { - getFloatArr(seg, offsetBytes, dstArray, dstOffsetFloats, lengthFloats); - } - - static void getFloatArr( - final MemorySegment seg, final long offsetBytes, final float[] dstArray, final int dstOffsetFloats, final int lengthFloats) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetFloats) << FLOAT_SHIFT; - MemorySegment.copy(seg, JAVA_FLOAT_UNALIGNED_NON_NATIVE, offsetBytes, dstSeg, JAVA_FLOAT, dstOffsetBytes, lengthFloats); + MemorySegment.copy(seg, JAVA_FLOAT_UNALIGNED_NON_NATIVE, offsetBytes, dstArray, dstOffsetFloats, lengthFloats); } @Override @@ -113,14 +87,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getIntArray(final long offsetBytes, final int[] dstArray, final int dstOffsetInts, final int lengthInts) { - getIntArr(seg, offsetBytes, dstArray, dstOffsetInts, lengthInts); - } - - static void getIntArr( - final MemorySegment seg, final long offsetBytes, final int[] dstArray, final int dstOffsetInts, final int lengthInts) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetInts) << INT_SHIFT; - MemorySegment.copy(seg, JAVA_INT_UNALIGNED_NON_NATIVE, offsetBytes, dstSeg, JAVA_INT, dstOffsetBytes, lengthInts); + MemorySegment.copy(seg, JAVA_INT_UNALIGNED_NON_NATIVE, offsetBytes, dstArray, dstOffsetInts, lengthInts); } @Override @@ -130,14 +97,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getLongArray(final long offsetBytes, final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) { - getLongArr(seg, offsetBytes, dstArray, dstOffsetLongs, lengthLongs); - } - - static void getLongArr( - final MemorySegment seg, final long offsetBytes, final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetLongs) << LONG_SHIFT; - MemorySegment.copy(seg, JAVA_LONG_UNALIGNED_NON_NATIVE, offsetBytes, dstSeg, JAVA_LONG, dstOffsetBytes, lengthLongs); + MemorySegment.copy(seg, JAVA_LONG_UNALIGNED_NON_NATIVE, offsetBytes, dstArray, dstOffsetLongs, lengthLongs); } @Override @@ -147,14 +107,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void getShortArray(final long offsetBytes, final short[] dstArray, final int dstOffsetShorts, final int lengthShorts) { - getShortArr(seg, offsetBytes, dstArray, dstOffsetShorts, lengthShorts); - } - - static void getShortArr( - final MemorySegment seg, final long offsetBytes, final short[] dstArray, final int dstOffsetShorts, final int lengthShorts) { - final MemorySegment dstSeg = MemorySegment.ofArray(dstArray); - final long dstOffsetBytes = ((long) dstOffsetShorts) << SHORT_SHIFT; - MemorySegment.copy(seg, JAVA_SHORT_UNALIGNED_NON_NATIVE, offsetBytes, dstSeg, JAVA_SHORT, dstOffsetBytes, lengthShorts); + MemorySegment.copy(seg, JAVA_SHORT_UNALIGNED_NON_NATIVE, offsetBytes, dstArray, dstOffsetShorts, lengthShorts); } //PRIMITIVE putX() and putXArray() implementations @@ -165,14 +118,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putCharArray(final long offsetBytes, final char[] srcArray, final int srcOffsetChars, final int lengthChars) { - putCharArr(seg, offsetBytes, srcArray, srcOffsetChars, lengthChars); - } - - static void putCharArr( - final MemorySegment seg, final long offsetBytes, final char[] srcArray, final int srcOffsetChars, final int lengthChars) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetChars) << CHAR_SHIFT; - MemorySegment.copy(srcSeg, JAVA_CHAR, srcOffsetBytes, seg, JAVA_CHAR_UNALIGNED_NON_NATIVE, offsetBytes, lengthChars); + MemorySegment.copy(srcArray, srcOffsetChars, seg, JAVA_CHAR_UNALIGNED_NON_NATIVE, offsetBytes, lengthChars); } @Override @@ -182,14 +128,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putDoubleArray(final long offsetBytes, final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) { - putDoubleArr(seg, offsetBytes, srcArray, srcOffsetDoubles, lengthDoubles); - } - - static void putDoubleArr( - final MemorySegment seg, final long offsetBytes, final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetDoubles) << DOUBLE_SHIFT; - MemorySegment.copy(srcSeg, JAVA_DOUBLE, srcOffsetBytes, seg, JAVA_DOUBLE_UNALIGNED_NON_NATIVE, offsetBytes, lengthDoubles); + MemorySegment.copy(srcArray, srcOffsetDoubles, seg, JAVA_DOUBLE_UNALIGNED_NON_NATIVE, offsetBytes, lengthDoubles); } @Override @@ -199,14 +138,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putFloatArray(final long offsetBytes, final float[] srcArray, final int srcOffsetFloats, final int lengthFloats) { - putFloatArr(seg, offsetBytes, srcArray, srcOffsetFloats, lengthFloats); - } - - static void putFloatArr( - final MemorySegment seg, final long offsetBytes, final float[] srcArray, final int srcOffsetFloats, final int lengthFloats) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetFloats) << FLOAT_SHIFT; - MemorySegment.copy(srcSeg, JAVA_FLOAT, srcOffsetBytes, seg, JAVA_FLOAT_UNALIGNED_NON_NATIVE, offsetBytes, lengthFloats); + MemorySegment.copy(srcArray, srcOffsetFloats, seg, JAVA_FLOAT_UNALIGNED_NON_NATIVE, offsetBytes, lengthFloats); } @Override @@ -216,14 +148,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putIntArray(final long offsetBytes, final int[] srcArray, final int srcOffsetInts, final int lengthInts) { - putIntArr(seg, offsetBytes, srcArray, srcOffsetInts, lengthInts); - } - - static void putIntArr( - final MemorySegment seg, final long offsetBytes, final int[] srcArray, final int srcOffsetInts, final int lengthInts) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetInts) << INT_SHIFT; - MemorySegment.copy(srcSeg, JAVA_INT, srcOffsetBytes, seg, JAVA_INT_UNALIGNED_NON_NATIVE, offsetBytes, lengthInts); + MemorySegment.copy(srcArray, srcOffsetInts, seg, JAVA_INT_UNALIGNED_NON_NATIVE, offsetBytes, lengthInts); } @Override @@ -233,14 +158,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putLongArray(final long offsetBytes, final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) { - putLongArr(seg, offsetBytes, srcArray, srcOffsetLongs, lengthLongs); - } - - static void putLongArr( - final MemorySegment seg, final long offsetBytes, final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetLongs) << LONG_SHIFT; - MemorySegment.copy(srcSeg, JAVA_LONG, srcOffsetBytes, seg, JAVA_LONG_UNALIGNED_NON_NATIVE, offsetBytes, lengthLongs); + MemorySegment.copy(srcArray, srcOffsetLongs, seg, JAVA_LONG_UNALIGNED_NON_NATIVE, offsetBytes, lengthLongs); } @Override @@ -250,14 +168,7 @@ final class NonNativeWritableMemoryImpl extends WritableMemoryImpl { @Override public void putShortArray(final long offsetBytes, final short[] srcArray, final int srcOffsetShorts, final int lengthShorts) { - putShortArr(seg, offsetBytes, srcArray, srcOffsetShorts, lengthShorts); - } - - static void putShortArr( - final MemorySegment seg, final long offsetBytes, final short[] srcArray, final int srcOffsetShorts, final int lengthShorts) { - final MemorySegment srcSeg = MemorySegment.ofArray(srcArray); - final long srcOffsetBytes = ((long) srcOffsetShorts) << SHORT_SHIFT; - MemorySegment.copy(srcSeg, JAVA_SHORT, srcOffsetBytes, seg, JAVA_SHORT_UNALIGNED_NON_NATIVE, offsetBytes, lengthShorts); + MemorySegment.copy(srcArray, srcOffsetShorts, seg, JAVA_SHORT_UNALIGNED_NON_NATIVE, offsetBytes, lengthShorts); } } diff --git a/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java index 5818cd9..b2e057f 100644 --- a/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java +++ b/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java @@ -32,19 +32,6 @@ import org.apache.datasketches.memory.MemoryRequestServer; import org.apache.datasketches.memory.WritableBuffer; import org.apache.datasketches.memory.WritableMemory; -/* - * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill, - * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at - * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to - * the rest of the work these methods are doing. - * - * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and - * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove - * them entirely from production runtime code. The offset versions of the light weight methods will - * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is - * often a single CPU instruction. - */ - /** * Common base of native-ordered and non-native-ordered {@link WritableBuffer} implementations. * Contains methods which are agnostic to the byte order. diff --git a/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java index a329911..4604e09 100644 --- a/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java +++ b/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java @@ -46,7 +46,6 @@ import org.apache.datasketches.memory.WritableMemory; * Common base of native-ordered and non-native-ordered {@link WritableMemory} implementations. * Contains methods which are agnostic to the byte order. */ -@SuppressWarnings("preview") public abstract class WritableMemoryImpl extends ResourceImpl implements WritableMemory { //Pass-through constructor @@ -147,7 +146,7 @@ public abstract class WritableMemoryImpl extends ResourceImpl implements Writabl * @throws IllegalArgumentException if file is not readable. * @throws IOException if mapping is not successful. */ - @SuppressWarnings("resource") + @SuppressWarnings({"resource","preview"}) public static WritableMemory wrapMap( final File file, final long fileOffsetBytes, @@ -339,7 +338,7 @@ public abstract class WritableMemoryImpl extends ResourceImpl implements Writabl out.writeBytes(bArr); } - // //PRIMITIVE putX() and putXArray() implementations + //PRIMITIVE putX() and putXArray() implementations @Override public final void putBoolean(final long offsetBytes, final boolean value) { diff --git a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java index a746ebc..c1fbc36 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java @@ -58,129 +58,236 @@ public class NativeWritableBufferImplTest { //Simple Heap arrays @Test - public void checkByteArray() { + public void checkGetByteArray() { byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - byte[] dstArray = new byte[8]; + final int len = srcArray.length; + final int half = len / 2; + byte[] dstArray = new byte[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getByteArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getByteArray(dstArray, 0, half); + buf.getByteArray(dstArray, half, half); + assertEquals(dstArray, srcArray); WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getByteArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getByteArray(dstArray, 0, half); + wbuf.getByteArray(dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutByteArray() { + byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + byte[] dstArray = new byte[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Byte.BYTES).asWritableBuffer(); + wbuf.putByteArray(srcArray, 0, half); + wbuf.putByteArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getByteArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test - public void checkCharArray() { + public void checkGetCharArray() { char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; - char[] dstArray = new char[8]; + final int len = srcArray.length; + final int half = len / 2; + char[] dstArray = new char[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getCharArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getCharArray(dstArray, 0, half); + buf.getCharArray(dstArray, half, half); + assertEquals(dstArray, srcArray); WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getCharArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getCharArray(dstArray, 0, half); + wbuf.getCharArray(dstArray, half, half); + assertEquals(dstArray, srcArray); } @Test - public void checkShortArray() { + public void checkPutCharArray() { + char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + char[] dstArray = new char[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Character.BYTES).asWritableBuffer(); + wbuf.putCharArray(srcArray, 0, half); + wbuf.putCharArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getCharArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkGetShortArray() { short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - short[] dstArray = new short[8]; + final int len = srcArray.length; + final int half = len / 2; + short[] dstArray = new short[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getShortArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getShortArray(dstArray, 0, half); + buf.getShortArray(dstArray, half, half); + assertEquals(dstArray, srcArray); WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getShortArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getShortArray(dstArray, 0, half); + wbuf.getShortArray(dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutShortArray() { + short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + short[] dstArray = new short[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Short.BYTES).asWritableBuffer(); + wbuf.putShortArray(srcArray, 0, half); + wbuf.putShortArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getShortArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test - public void checkIntArray() { + public void checkGetIntArray() { int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - int[] dstArray = new int[8]; + final int len = srcArray.length; + final int half = len / 2; + int[] dstArray = new int[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getIntArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getIntArray(dstArray, 0, half); + buf.getIntArray(dstArray, half, half); + assertEquals(dstArray, srcArray); WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getIntArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getIntArray(dstArray, 0, half); + wbuf.getIntArray(dstArray, half, half); + assertEquals(dstArray, srcArray); } @Test - public void checkLongArray() { + public void checkPutIntArray() { + int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + int[] dstArray = new int[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Integer.BYTES).asWritableBuffer(); + wbuf.putIntArray(srcArray, 0, half); + wbuf.putIntArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getIntArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkGetLongArray() { long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - long[] dstArray = new long[8]; + final int len = srcArray.length; + final int half = len / 2; + long[] dstArray = new long[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getLongArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getLongArray(dstArray, 0, half); + buf.getLongArray(dstArray, half, half); + assertEquals(dstArray, srcArray); WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getLongArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getLongArray(dstArray, 0, half); + wbuf.getLongArray(dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutLongArray() { + long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + long[] dstArray = new long[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Long.BYTES).asWritableBuffer(); + wbuf.putLongArray(srcArray, 0, half); + wbuf.putLongArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getLongArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test - public void checkFloatArray() { + public void checkGetFloatArray() { float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - float[] dstArray = new float[8]; + final int len = srcArray.length; + final int half = len / 2; + float[] dstArray = new float[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getFloatArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getFloatArray(dstArray, 0, half); + buf.getFloatArray(dstArray, half, half); + assertEquals(dstArray, srcArray); + WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getFloatArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getFloatArray(dstArray, 0, half); + wbuf.getFloatArray(dstArray, half, half); + assertEquals(dstArray, srcArray); } @Test - public void checkDoubleArray() { + public void checkPutFloatArray() { + float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + float[] dstArray = new float[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Float.BYTES).asWritableBuffer(); + wbuf.putFloatArray(srcArray, 0, half); + wbuf.putFloatArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getFloatArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkGetDoubleArray() { double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - double[] dstArray = new double[8]; + final int len = srcArray.length; + final int half = len / 2; + double[] dstArray = new double[len]; Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getDoubleArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + buf.getDoubleArray(dstArray, 0, half); + buf.getDoubleArray(dstArray, half, half); + assertEquals(dstArray, srcArray); + WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getDoubleArray(dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wbuf.getDoubleArray(dstArray, 0, half); + wbuf.getDoubleArray(dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutDoubleArray() { + double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + double[] dstArray = new double[len]; + + WritableBuffer wbuf = WritableMemory.allocate(len * Double.BYTES).asWritableBuffer(); + wbuf.putDoubleArray(srcArray, 0, half); + wbuf.putDoubleArray(srcArray, half, half); + wbuf.resetPosition(); + wbuf.getDoubleArray(dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test @@ -468,22 +575,6 @@ public class NativeWritableBufferImplTest { assertEquals(buf.getShort(0), 256); } - @Test - public void checkPutIntArray() { - WritableMemory wmem = WritableMemory.allocate(12); - WritableBuffer wbuf = wmem.asWritableBuffer(); - - wbuf.putInt(1); - int[] array = new int[] { 2 }; - wbuf.putIntArray(array, 0, 1); - wbuf.putInt(3); - - Buffer buf = wmem.asWritableBuffer(); - assertEquals(buf.getInt(), 1); - assertEquals(buf.getInt(), 2); - assertEquals(buf.getInt(), 3); - } - @Test public void printlnTest() { println("PRINTING: "+this.getClass().getName()); diff --git a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java index 7a410b7..c58c669 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java @@ -58,132 +58,231 @@ public class NativeWritableMemoryImplTest { try { wmem.getArena().close(); } catch (IllegalStateException e) { } } - //Simple Native arrays + //Simple Heap arrays @Test - public void checkByteArray() { + public void checkGetByteArray() { byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - byte[] dstArray = new byte[8]; + final int len = srcArray.length; + final int half = len / 2; + byte[] dstArray = new byte[len]; Memory mem = Memory.wrap(srcArray); - mem.getByteArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getByteArray(0, dstArray, 0, half); + mem.getByteArray(half, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getByteArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getByteArray(0, dstArray, 0, half); + wmem.getByteArray(half, dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutByteArray() { + byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + byte[] dstArray = new byte[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Byte.BYTES); + wmem.putByteArray(0, srcArray, 0, half); + wmem.putByteArray(half * Byte.BYTES, srcArray, half, half); + wmem.getByteArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test - public void checkCharArray() { + public void checkGetCharArray() { char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; - char[] dstArray = new char[8]; + final int len = srcArray.length; + final int half = len / 2; + char[] dstArray = new char[len]; Memory mem = Memory.wrap(srcArray); - mem.getCharArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getCharArray(0, dstArray, 0, half); + mem.getCharArray(half * Character.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getCharArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getCharArray(0, dstArray, 0, half); + wmem.getCharArray(half * Character.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); } @Test - public void checkShortArray() { + public void checkPutCharArray() { + char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + char[] dstArray = new char[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Character.BYTES); + wmem.putCharArray(0, srcArray, 0, half); + wmem.putCharArray(half * Character.BYTES, srcArray, half, half); + wmem.getCharArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkGetShortArray() { short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - short[] dstArray = new short[8]; + final int len = srcArray.length; + final int half = len / 2; + short[] dstArray = new short[len]; Memory mem = Memory.wrap(srcArray); - mem.getShortArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getShortArray(0, dstArray, 0, half); + mem.getShortArray(half * Short.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getShortArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getShortArray(0, dstArray, 0, half); + wmem.getShortArray(half * Short.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutShortArray() { + short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + short[] dstArray = new short[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Short.BYTES); + wmem.putShortArray(0, srcArray, 0, half); + wmem.putShortArray(half * Short.BYTES, srcArray, half, half); + wmem.getShortArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test - public void checkIntArray() { + public void checkGetIntArray() { int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - int[] dstArray = new int[8]; + final int len = srcArray.length; + final int half = len / 2; + int[] dstArray = new int[len]; Memory mem = Memory.wrap(srcArray); - mem.getIntArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getIntArray(0, dstArray, 0, half); + mem.getIntArray(half * Integer.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getIntArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getIntArray(0, dstArray, 0, half); + wmem.getIntArray(half * Integer.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); } @Test - public void checkLongArray() { + public void checkPutIntArray() { + int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + int[] dstArray = new int[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Integer.BYTES); + wmem.putIntArray(0, srcArray, 0, half); + wmem.putIntArray(half * Integer.BYTES, srcArray, half, half); + wmem.getIntArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkGetLongArray() { long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - long[] dstArray = new long[8]; + final int len = srcArray.length; + final int half = len / 2; + long[] dstArray = new long[len]; Memory mem = Memory.wrap(srcArray); - mem.getLongArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getLongArray(0, dstArray, 0, half); + mem.getLongArray(half * Long.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getLongArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getLongArray(0, dstArray, 0, half); + wmem.getLongArray(half * Long.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutLongArray() { + long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + long[] dstArray = new long[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Long.BYTES); + wmem.putLongArray(0, srcArray, 0, half); + wmem.putLongArray(half * Long.BYTES, srcArray, half, half); + wmem.getLongArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); } + @Test - public void checkFloatArray() { + public void checkGetFloatArray() { float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - float[] dstArray = new float[8]; + final int len = srcArray.length; + final int half = len / 2; + float[] dstArray = new float[len]; Memory mem = Memory.wrap(srcArray); - mem.getFloatArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getFloatArray(0, dstArray, 0, half); + mem.getFloatArray(half * Float.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getFloatArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getFloatArray(0, dstArray, 0, half); + wmem.getFloatArray(half * Float.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutFloatArray() { + float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + float[] dstArray = new float[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Float.BYTES); + wmem.putFloatArray(0, srcArray, 0, half); + wmem.putFloatArray(half * Float.BYTES, srcArray, half, half); + wmem.getFloatArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test - public void checkDoubleArray() { + public void checkGetDoubleArray() { double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; double[] dstArray = new double[8]; Memory mem = Memory.wrap(srcArray); - mem.getDoubleArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + mem.getDoubleArray(0, dstArray, 0, half); + mem.getDoubleArray(half * Double.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getDoubleArray(0, dstArray, 0, 8); - for (int i=0; i<8; i++) { - assertEquals(dstArray[i], srcArray[i]); - } + wmem.getDoubleArray(0, dstArray, 0, half); + wmem.getDoubleArray(half * Double.BYTES, dstArray, half, half); + assertEquals(dstArray, srcArray); + } + + @Test + public void checkPutDoubleArray() { + double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + final int len = srcArray.length; + final int half = len / 2; + double[] dstArray = new double[len]; + + WritableMemory wmem = WritableMemory.allocate(len * Double.BYTES); + wmem.putDoubleArray(0, srcArray, 0, half); + wmem.putDoubleArray(half * Double.BYTES, srcArray, half, half); + wmem.getDoubleArray(0, dstArray, 0, len); + assertEquals(dstArray, srcArray); } @Test diff --git a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java index 1de0c4c..8e59866 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java @@ -19,7 +19,12 @@ package org.apache.datasketches.memory.internal; +import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImplTest.doubleReverseBytes; +import static org.apache.datasketches.memory.internal.NonNativeWritableMemoryImplTest.floatReverseBytes; +import static org.apache.datasketches.memory.internal.ResourceImpl.NATIVE_BYTE_ORDER; +import static org.apache.datasketches.memory.internal.ResourceImpl.NON_NATIVE_BYTE_ORDER; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; import java.nio.ByteOrder; @@ -35,250 +40,203 @@ import org.testng.annotations.Test; public class NonNativeWritableBufferImplTest { //Check primitives + @Test - public void checkCharacters() { - int n = 8; - int m = Character.BYTES; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(); - char ch = 'a'; - for (int i = 0; i < n; i++) { wbuf.putChar(i * m, ch++); } - ch = 'a'; - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getChar(i * m), ch++); + public void checkPutGetNonNativeCharacters() { + char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * Character.BYTES, NON_NATIVE_BYTE_ORDER).asWritableBuffer(); + wbuf.putCharArray(srcArray, 0, half); + wbuf.putCharArray(srcArray, half, half); + wbuf.resetPosition(); + //confirm + WritableBuffer wbuf2 = WritableMemory.allocate(len * Character.BYTES, NATIVE_BYTE_ORDER).asWritableBuffer(); + for (int i = 0; i < len * Character.BYTES; i++) { wbuf2.putByte(wbuf.getByte()); } + wbuf.resetPosition(); + wbuf2.resetPosition(); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Character.reverseBytes(wbuf2.getChar())); } - ch = 'a'; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { wbuf.putChar(ch++); } - ch = 'a'; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getChar(), ch++); - } - //getArr & putArr - char[] cArr = new char[n]; //native - wbuf.setPosition(0); - wbuf.getCharArray(cArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf2 = wmem2.asWritableBuffer(); - wbuf2.putCharArray(cArr, 0, n); - assertEquals(arr2, arr1); + wbuf2.resetPosition(); + //get + char[] dstArray = new char[len]; + wbuf.getCharArray(dstArray, 0, half); + wbuf.getCharArray(dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkDoubles() { - int n = 8; - int m = Double.BYTES; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(); - double dbl = 1.0; - for (int i = 0; i < n; i++) { wbuf.putDouble(i * m, dbl++); } - dbl = 1.0; - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getDouble(i * m), dbl++); + public void checkPutGetNonNativeDoubles() { + double[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * Double.BYTES, NON_NATIVE_BYTE_ORDER).asWritableBuffer(); + wbuf.putDoubleArray(srcArray, 0, half); + wbuf.putDoubleArray(srcArray, half, half); + wbuf.resetPosition(); + //confirm + WritableBuffer wbuf2 = WritableMemory.allocate(len * Double.BYTES, NATIVE_BYTE_ORDER).asWritableBuffer(); + for (int i = 0; i < len * Double.BYTES; i++) { wbuf2.putByte(wbuf.getByte()); } + wbuf.resetPosition(); + wbuf2.resetPosition(); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == doubleReverseBytes(wbuf2.getDouble())); } - dbl = 1.0; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { wbuf.putDouble(dbl++); } - dbl = 1.0; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getDouble(), dbl++); - } - //getArr & putArr - double[] dblArr = new double[n]; //native - wbuf.setPosition(0); - wbuf.getDoubleArray(dblArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf2 = wmem2.asWritableBuffer(); - wbuf2.putDoubleArray(dblArr, 0, n); - assertEquals(arr2, arr1); + wbuf2.resetPosition(); + //get + double[] dstArray = new double[len]; + wbuf.getDoubleArray(dstArray, 0, half); + wbuf.getDoubleArray(dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkFloats() { - int n = 8; - int m = Float.BYTES; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(); - float flt = 1.0F; - for (int i = 0; i < n; i++) { wbuf.putFloat(i * m, flt++); } - flt = 1.0F; - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getFloat(i * m), flt++); - } - flt = 1.0F; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { wbuf.putFloat(flt++); } - flt = 1.0F; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getFloat(), flt++); + public void checkPutGetNonNativeFloats() { + float[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * Float.BYTES, NON_NATIVE_BYTE_ORDER).asWritableBuffer(); + wbuf.putFloatArray(srcArray, 0, half); + wbuf.putFloatArray(srcArray, half, half); + wbuf.resetPosition(); + //confirm + WritableBuffer wbuf2 = WritableMemory.allocate(len * Float.BYTES, NATIVE_BYTE_ORDER).asWritableBuffer(); + for (int i = 0; i < len * Float.BYTES; i++) { wbuf2.putByte(wbuf.getByte()); } + wbuf.resetPosition(); + wbuf2.resetPosition(); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == floatReverseBytes(wbuf2.getFloat())); } - //getArr & putArr - float[] fltArr = new float[n]; //native - wbuf.setPosition(0); - wbuf.getFloatArray(fltArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf2 = wmem2.asWritableBuffer(); - wbuf2.putFloatArray(fltArr, 0, n); - assertEquals(arr2, arr1); + wbuf2.resetPosition(); + //get + float[] dstArray = new float[len]; + wbuf.getFloatArray(dstArray, 0, half); + wbuf.getFloatArray(dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkInts() { - int n = 8; - int m = Integer.BYTES; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(); - int intg = 1; - for (int i = 0; i < n; i++) { wbuf.putInt(i * m, intg++); } - intg = 1; - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getInt(i * m), intg++); - } - intg = 1; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { wbuf.putInt(intg++); } - intg = 1; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getInt(), intg++); + public void checkPutGetNonNativeInts() { + int[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * Integer.BYTES, NON_NATIVE_BYTE_ORDER).asWritableBuffer(); + wbuf.putIntArray(srcArray, 0, half); + wbuf.putIntArray(srcArray, half, half); + wbuf.resetPosition(); + //confirm + WritableBuffer wbuf2 = WritableMemory.allocate(len * Integer.BYTES, NATIVE_BYTE_ORDER).asWritableBuffer(); + for (int i = 0; i < len * Integer.BYTES; i++) { wbuf2.putByte(wbuf.getByte()); } + wbuf.resetPosition(); + wbuf2.resetPosition(); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Integer.reverseBytes(wbuf2.getInt())); } - //getArr & putArr - int[] intArr = new int[n]; //native - wbuf.setPosition(0); - wbuf.getIntArray(intArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf2 = wmem2.asWritableBuffer(); - wbuf2.putIntArray(intArr, 0, n); - assertEquals(arr2, arr1); + wbuf2.resetPosition(); + //get + int[] dstArray = new int[len]; + wbuf.getIntArray(dstArray, 0, half); + wbuf.getIntArray(dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkLongs() { - int n = 8; - int m = Long.BYTES; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(); - long lng = 1; - for (int i = 0; i < n; i++) { wbuf.putLong(i * m, lng++); } - lng = 1; - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getLong(i * m), lng++); + public void checkPutGetNonNativeLongs() { + long[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * Long.BYTES, NON_NATIVE_BYTE_ORDER).asWritableBuffer(); + wbuf.putLongArray(srcArray, 0, half); + wbuf.putLongArray(srcArray, half, half); + wbuf.resetPosition(); + //confirm + WritableBuffer wbuf2 = WritableMemory.allocate(len * Long.BYTES, NATIVE_BYTE_ORDER).asWritableBuffer(); + for (int i = 0; i < len * Long.BYTES; i++) { wbuf2.putByte(wbuf.getByte()); } + wbuf.resetPosition(); + wbuf2.resetPosition(); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Long.reverseBytes(wbuf2.getLong())); } - lng = 1; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { wbuf.putLong(lng++); } - lng = 1; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getLong(), lng++); - } - //getArr & putArr - long[] longArr = new long[n]; //native - wbuf.setPosition(0); - wbuf.getLongArray(longArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf2 = wmem2.asWritableBuffer(); - wbuf2.putLongArray(longArr, 0, n); - assertEquals(arr2, arr1); + wbuf2.resetPosition(); + //get + long[] dstArray = new long[len]; + wbuf.getLongArray(dstArray, 0, half); + wbuf.getLongArray(dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkShorts() { - int n = 8; - int m = Short.BYTES; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(); - short sht = 1; - for (int i = 0; i < n; i++) { wbuf.putShort(i * m, sht++); } - sht = 1; - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getShort(i * m), sht++); - } - sht = 1; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { wbuf.putShort(sht++); } - sht = 1; - wbuf.setPosition(0); - for (int i = 0; i < n; i++) { - assertEquals(wbuf.getShort(), sht++); + public void checkPutGetNonNativeShorts() { + short[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * Short.BYTES, NON_NATIVE_BYTE_ORDER).asWritableBuffer(); + wbuf.putShortArray(srcArray, 0, half); + wbuf.putShortArray(srcArray, half, half); + wbuf.resetPosition(); + //confirm + WritableBuffer wbuf2 = WritableMemory.allocate(len * Short.BYTES, NATIVE_BYTE_ORDER).asWritableBuffer(); + for (int i = 0; i < len * Short.BYTES; i++) { wbuf2.putByte(wbuf.getByte()); } + wbuf.resetPosition(); + wbuf2.resetPosition(); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Short.reverseBytes(wbuf2.getShort())); } - //getArr & putArr - short[] shortArr = new short[n]; //native - wbuf.setPosition(0); - wbuf.getShortArray(shortArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf2 = wmem2.asWritableBuffer(); - wbuf2.putShortArray(shortArr, 0, n); - assertEquals(arr2, arr1); + wbuf2.resetPosition(); + //get + short[] dstArray = new short[len]; + wbuf.getShortArray(dstArray, 0, half); + wbuf.getShortArray(dstArray, half, half); + assertEquals(srcArray, dstArray); } //check Duplicate, Region @Test public void checkDuplicate() { byte[] bArr = new byte[8]; - WritableMemory wmem = WritableMemory.writableWrap(bArr, ByteOrder.BIG_ENDIAN); + WritableMemory wmem = WritableMemory.writableWrap(bArr, NON_NATIVE_BYTE_ORDER); WritableBuffer wbuf = wmem.asWritableBuffer(); WritableBuffer wdup = wbuf.writableDuplicate(); - assertEquals(wdup.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + assertEquals(wdup.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); WritableBuffer wreg = wbuf.writableRegion(); - assertEquals(wreg.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + assertEquals(wreg.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); } @Test public void checkConversionByteOrder() { byte[] bArr = new byte[8]; bArr[1] = 1; - WritableMemory wmem = WritableMemory.writableWrap(bArr, ByteOrder.BIG_ENDIAN); - assertEquals(wmem.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + WritableMemory wmem = WritableMemory.writableWrap(bArr, NON_NATIVE_BYTE_ORDER); + assertEquals(wmem.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); assertEquals(wmem.getChar(0), 1); Buffer buf = wmem.asBuffer(); - assertEquals(buf.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); // + assertEquals(buf.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); // assertEquals(buf.getChar(0), 1); Buffer dup = buf.duplicate(); - assertEquals(dup.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + assertEquals(dup.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); assertEquals(dup.getChar(0), 1); Buffer reg = buf.region(); - assertEquals(reg.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + assertEquals(reg.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); assertEquals(reg.getChar(0), 1); Memory mem = reg.asMemory(); - assertEquals(mem.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + assertEquals(mem.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); assertEquals(mem.getChar(0), 1); Memory mreg = mem.region(0, 8); - assertEquals(mreg.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); + assertEquals(mreg.getTypeByteOrder(), NON_NATIVE_BYTE_ORDER); assertEquals(mreg.getChar(0), 1); } @Test public void checkPutIntArray() { - WritableMemory wmem = WritableMemory.allocate(12, ByteOrder.BIG_ENDIAN); - WritableBuffer wbuf = wmem.asWritableBuffer(ByteOrder.BIG_ENDIAN); + WritableMemory wmem = WritableMemory.allocate(12, NON_NATIVE_BYTE_ORDER); + WritableBuffer wbuf = wmem.asWritableBuffer(NON_NATIVE_BYTE_ORDER); wbuf.putInt(1); int[] array = new int[] { 2 }; diff --git a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java index fcfff82..a43cb94 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java @@ -19,8 +19,12 @@ package org.apache.datasketches.memory.internal; +import static org.apache.datasketches.memory.internal.ResourceImpl.NATIVE_BYTE_ORDER; +import static org.apache.datasketches.memory.internal.ResourceImpl.NON_NATIVE_BYTE_ORDER; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import java.lang.foreign.MemorySegment; import java.nio.ByteOrder; import org.apache.datasketches.memory.WritableMemory; @@ -33,135 +37,132 @@ public class NonNativeWritableMemoryImplTest { private byte[] bArr = new byte[8]; private final WritableMemory wmem = WritableMemory.writableWrap(bArr, ByteOrder.BIG_ENDIAN); -//Check primitives + //Check primitives + @Test - public void checkCharacters() { - int m = Character.BYTES; - int n = ((1 << 20) / m) + m; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem1 = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - for (int i = 0; i < n; i++) { wmem1.putChar(i * m, (char) i++); } - for (int i = 0; i < n; i++) { - assertEquals(wmem1.getChar(i * m), (char) i++); + public void checkPutGetNonNativeCharacters() { + char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * Character.BYTES, NON_NATIVE_BYTE_ORDER); + wmem.putCharArray(0, srcArray, 0, half); + wmem.putCharArray(half * Character.BYTES, srcArray, half, half); + //confirm + WritableMemory wmem2 = WritableMemory.allocate(len * Character.BYTES, NATIVE_BYTE_ORDER); + wmem.copyTo(0, wmem2, 0, len * Character.BYTES); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Character.reverseBytes(wmem2.getChar(i * Character.BYTES))); } - //getArr & putArr - char[] cArr = new char[n]; //native - wmem1.getCharArray(0, cArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - wmem2.putCharArray(0, cArr, 0, n); - assertEquals(arr2, arr1); + //get + char[] dstArray = new char[len]; + wmem.getCharArray(0, dstArray, 0, half); + wmem.getCharArray(half * Character.BYTES, dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkDoubles() { - int m = Double.BYTES; - int n = ((1 << 20) / m) + m; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem1 = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - double dbl = 1.0; - for (int i = 0; i < n; i++) { wmem1.putDouble(i * m, dbl++); } - dbl = 1.0; - for (int i = 0; i < n; i++) { - assertEquals(wmem1.getDouble(i * m), dbl++); + public void checkPutGetNonNativeDoubles() { + double[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * Double.BYTES, NON_NATIVE_BYTE_ORDER); + wmem.putDoubleArray(0, srcArray, 0, half); + wmem.putDoubleArray(half * Double.BYTES, srcArray, half, half); + //confirm + WritableMemory wmem2 = WritableMemory.allocate(len * Double.BYTES, NATIVE_BYTE_ORDER); + wmem.copyTo(0, wmem2, 0, len * Double.BYTES); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == doubleReverseBytes(wmem2.getDouble(i * Double.BYTES))); } - //getArr & putArr - double[] dblArr = new double[n]; //native - wmem1.getDoubleArray(0, dblArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - wmem2.putDoubleArray(0, dblArr, 0, n); - assertEquals(arr2, arr1); + //get + double[] dstArray = new double[len]; + wmem.getDoubleArray(0, dstArray, 0, half); + wmem.getDoubleArray(half * Double.BYTES, dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkFloats() { - int m = Float.BYTES; - int n = ((1 << 20) / m) + m; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem1 = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - float flt = 1.0F; - for (int i = 0; i < n; i++) { wmem1.putFloat(i * m, flt++); } - flt = 1.0F; - for (int i = 0; i < n; i++) { - assertEquals(wmem1.getFloat(i * m), flt++); + public void checkPutGetNonNativeFloats() { + float[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * Float.BYTES, NON_NATIVE_BYTE_ORDER); + wmem.putFloatArray(0, srcArray, 0, half); + wmem.putFloatArray(half * Float.BYTES, srcArray, half, half); + //confirm + WritableMemory wmem2 = WritableMemory.allocate(len * Float.BYTES, NATIVE_BYTE_ORDER); + wmem.copyTo(0, wmem2, 0, len * Float.BYTES); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == floatReverseBytes(wmem2.getFloat(i * Float.BYTES))); } - //getArr & putArr - float[] fltArr = new float[n]; //native - wmem1.getFloatArray(0, fltArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - wmem2.putFloatArray(0, fltArr, 0, n); - assertEquals(arr2, arr1); + //get + float[] dstArray = new float[len]; + wmem.getFloatArray(0, dstArray, 0, half); + wmem.getFloatArray(half * Float.BYTES, dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkInts() { - int m = Integer.BYTES; - int n = ((1 << 20) / m) + m; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem1 = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - int intg = 1; - for (int i = 0; i < n; i++) { wmem1.putInt(i * m, intg++); } - intg = 1; - for (int i = 0; i < n; i++) { - assertEquals(wmem1.getInt(i * m), intg++); + public void checkPutGetNonNativeInts() { + int[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * Integer.BYTES, NON_NATIVE_BYTE_ORDER); + wmem.putIntArray(0, srcArray, 0, half); + wmem.putIntArray(half * Integer.BYTES, srcArray, half, half); + //confirm + WritableMemory wmem2 = WritableMemory.allocate(len * Integer.BYTES, NATIVE_BYTE_ORDER); + wmem.copyTo(0, wmem2, 0, len * Integer.BYTES); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Integer.reverseBytes(wmem2.getInt(i * Integer.BYTES))); } - //getArr & putArr - int[] intArr = new int[n]; //native - wmem1.getIntArray(0, intArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - wmem2.putIntArray(0, intArr, 0, n); - assertEquals(arr2, arr1); + //get + int[] dstArray = new int[len]; + wmem.getIntArray(0, dstArray, 0, half); + wmem.getIntArray(half * Integer.BYTES, dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkLongs() { - int m = Long.BYTES; - int n = ((1 << 20) / m) + m; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem1 = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - long lng = 1; - for (int i = 0; i < n; i++) { wmem1.putLong(i * m, lng++); } - lng = 1; - for (int i = 0; i < n; i++) { - assertEquals(wmem1.getLong(i * m), lng++); + public void checkPutGetNonNativeLongs() { + long[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * Long.BYTES, NON_NATIVE_BYTE_ORDER); + wmem.putLongArray(0, srcArray, 0, half); + wmem.putLongArray(half * Long.BYTES, srcArray, half, half); + //confirm + WritableMemory wmem2 = WritableMemory.allocate(len * Long.BYTES, NATIVE_BYTE_ORDER); + wmem.copyTo(0, wmem2, 0, len * Long.BYTES); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Long.reverseBytes(wmem2.getLong(i * Long.BYTES))); } - //getArr & putArr - long[] longArr = new long[n]; //native - wmem1.getLongArray(0, longArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - wmem2.putLongArray(0, longArr, 0, n); - assertEquals(arr2, arr1); + //get + long[] dstArray = new long[len]; + wmem.getLongArray(0, dstArray, 0, half); + wmem.getLongArray(half * Long.BYTES, dstArray, half, half); + assertEquals(srcArray, dstArray); } @Test - public void checkShorts() { - int m = Short.BYTES; - int n = ((1 << 20) / m) + m; - byte[] arr1 = new byte[n * m]; //non-native - //put & get - WritableMemory wmem1 = WritableMemory.writableWrap(arr1, ByteOrder.BIG_ENDIAN); - short sht = 1; - for (int i = 0; i < n; i++) { wmem1.putShort(i * m, sht++); } - sht = 1; - for (int i = 0; i < n; i++) { - assertEquals(wmem1.getShort(i * m), sht++); + public void checkPutGetNonNativeShorts() { + short[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + final int len = srcArray.length; + final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * Short.BYTES, NON_NATIVE_BYTE_ORDER); + wmem.putShortArray(0, srcArray, 0, half); + wmem.putShortArray(half * Short.BYTES, srcArray, half, half); + //confirm + WritableMemory wmem2 = WritableMemory.allocate(len * Short.BYTES, NATIVE_BYTE_ORDER); + wmem.copyTo(0, wmem2, 0, len * Short.BYTES); + for (int i = 0; i < len; i++) { + assertTrue(srcArray[i] == Short.reverseBytes(wmem2.getShort(i * Short.BYTES))); } - //getArr & putArr - short[] shortArr = new short[n]; //native - wmem1.getShortArray(0, shortArr, 0, n); //wmem is non-native - byte[] arr2 = new byte[n * m]; - WritableMemory wmem2 = WritableMemory.writableWrap(arr2, ByteOrder.BIG_ENDIAN); - wmem2.putShortArray(0, shortArr, 0, n); - assertEquals(arr2, arr1); + //get + short[] dstArray = new short[len]; + wmem.getShortArray(0, dstArray, 0, half); + wmem.getShortArray(half * Short.BYTES, dstArray, half, half); + assertEquals(srcArray, dstArray); } //check Atomic Write Methods @@ -173,4 +174,18 @@ public class NonNativeWritableMemoryImplTest { assertEquals(wreg.getTypeByteOrder(), ByteOrder.BIG_ENDIAN); } + //Java does not provide reverse bytes on doubles or floats + + static double doubleReverseBytes(double value) { + long longIn = Double.doubleToRawLongBits(value); + long longOut = Long.reverseBytes(longIn); + return Double.longBitsToDouble(longOut); + } + + static float floatReverseBytes(float value) { + int intIn = Float.floatToRawIntBits(value); + int intOut = Integer.reverseBytes(intIn); + return Float.intBitsToFloat(intOut); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
