提交 988fa69b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Move compareNotNullSigned() and compareNotNullUnsigned() to Bits

to utilize vectorized implementation on Java 9+
上级 2f4fbec0
...@@ -8,6 +8,7 @@ package org.h2.util; ...@@ -8,6 +8,7 @@ package org.h2.util;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
/** /**
...@@ -28,6 +29,46 @@ public final class Bits { ...@@ -28,6 +29,46 @@ public final class Bits {
*/ */
private static final VarHandle LONG_VH = MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.BIG_ENDIAN); private static final VarHandle LONG_VH = MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.BIG_ENDIAN);
/**
* Compare the contents of two byte arrays. If the content or length of the
* first array is smaller than the second array, -1 is returned. If the content
* or length of the second array is smaller than the first array, 1 is returned.
* If the contents and lengths are the same, 0 is returned.
*
* <p>
* This method interprets bytes as signed.
* </p>
*
* @param data1
* the first byte array (must not be null)
* @param data2
* the second byte array (must not be null)
* @return the result of the comparison (-1, 1 or 0)
*/
public static int compareNotNullSigned(byte[] data1, byte[] data2) {
return Integer.signum(Arrays.compare(data1, data2));
}
/**
* Compare the contents of two byte arrays. If the content or length of the
* first array is smaller than the second array, -1 is returned. If the content
* or length of the second array is smaller than the first array, 1 is returned.
* If the contents and lengths are the same, 0 is returned.
*
* <p>
* This method interprets bytes as unsigned.
* </p>
*
* @param data1
* the first byte array (must not be null)
* @param data2
* the second byte array (must not be null)
* @return the result of the comparison (-1, 1 or 0)
*/
public static int compareNotNullUnsigned(byte[] data1, byte[] data2) {
return Integer.signum(Arrays.compareUnsigned(data1, data2));
}
/** /**
* Reads a int value from the byte array at the given position in big-endian * Reads a int value from the byte array at the given position in big-endian
* order. * order.
......
...@@ -20,6 +20,68 @@ public final class Bits { ...@@ -20,6 +20,68 @@ public final class Bits {
* h2/src/java9/precompiled/org/h2/util/Bits.class. * h2/src/java9/precompiled/org/h2/util/Bits.class.
*/ */
/**
* Compare the contents of two byte arrays. If the content or length of the
* first array is smaller than the second array, -1 is returned. If the content
* or length of the second array is smaller than the first array, 1 is returned.
* If the contents and lengths are the same, 0 is returned.
*
* <p>
* This method interprets bytes as signed.
* </p>
*
* @param data1
* the first byte array (must not be null)
* @param data2
* the second byte array (must not be null)
* @return the result of the comparison (-1, 1 or 0)
*/
public static int compareNotNullSigned(byte[] data1, byte[] data2) {
if (data1 == data2) {
return 0;
}
int len = Math.min(data1.length, data2.length);
for (int i = 0; i < len; i++) {
byte b = data1[i];
byte b2 = data2[i];
if (b != b2) {
return b > b2 ? 1 : -1;
}
}
return Integer.signum(data1.length - data2.length);
}
/**
* Compare the contents of two byte arrays. If the content or length of the
* first array is smaller than the second array, -1 is returned. If the content
* or length of the second array is smaller than the first array, 1 is returned.
* If the contents and lengths are the same, 0 is returned.
*
* <p>
* This method interprets bytes as unsigned.
* </p>
*
* @param data1
* the first byte array (must not be null)
* @param data2
* the second byte array (must not be null)
* @return the result of the comparison (-1, 1 or 0)
*/
public static int compareNotNullUnsigned(byte[] data1, byte[] data2) {
if (data1 == data2) {
return 0;
}
int len = Math.min(data1.length, data2.length);
for (int i = 0; i < len; i++) {
int b = data1[i] & 0xff;
int b2 = data2[i] & 0xff;
if (b != b2) {
return b > b2 ? 1 : -1;
}
}
return Integer.signum(data1.length - data2.length);
}
/** /**
* Reads a int value from the byte array at the given position in big-endian * Reads a int value from the byte array at the given position in big-endian
* order. * order.
......
...@@ -138,60 +138,6 @@ public class Utils { ...@@ -138,60 +138,6 @@ public class Utils {
return bits == 0; return bits == 0;
} }
/**
* Compare the contents of two byte arrays. If the content or length of the
* first array is smaller than the second array, -1 is returned. If the
* content or length of the second array is smaller than the first array, 1
* is returned. If the contents and lengths are the same, 0 is returned.
* <p>
* This method interprets bytes as signed.
*
* @param data1 the first byte array (must not be null)
* @param data2 the second byte array (must not be null)
* @return the result of the comparison (-1, 1 or 0)
*/
public static int compareNotNullSigned(byte[] data1, byte[] data2) {
if (data1 == data2) {
return 0;
}
int len = Math.min(data1.length, data2.length);
for (int i = 0; i < len; i++) {
byte b = data1[i];
byte b2 = data2[i];
if (b != b2) {
return b > b2 ? 1 : -1;
}
}
return Integer.signum(data1.length - data2.length);
}
/**
* Compare the contents of two byte arrays. If the content or length of the
* first array is smaller than the second array, -1 is returned. If the
* content or length of the second array is smaller than the first array, 1
* is returned. If the contents and lengths are the same, 0 is returned.
* <p>
* This method interprets bytes as unsigned.
*
* @param data1 the first byte array (must not be null)
* @param data2 the second byte array (must not be null)
* @return the result of the comparison (-1, 1 or 0)
*/
public static int compareNotNullUnsigned(byte[] data1, byte[] data2) {
if (data1 == data2) {
return 0;
}
int len = Math.min(data1.length, data2.length);
for (int i = 0; i < len; i++) {
int b = data1[i] & 0xff;
int b2 = data2[i] & 0xff;
if (b != b2) {
return b > b2 ? 1 : -1;
}
}
return Integer.signum(data1.length - data2.length);
}
/** /**
* Copy the contents of the source array to the target array. If the size if * Copy the contents of the source array to the target array. If the size if
* the target array is too small, a larger array is created. * the target array is too small, a larger array is created.
......
...@@ -10,6 +10,7 @@ import java.sql.SQLException; ...@@ -10,6 +10,7 @@ import java.sql.SQLException;
import java.util.Arrays; import java.util.Arrays;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.util.Bits;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -93,9 +94,9 @@ public class ValueBytes extends Value { ...@@ -93,9 +94,9 @@ public class ValueBytes extends Value {
protected int compareSecure(Value v, CompareMode mode) { protected int compareSecure(Value v, CompareMode mode) {
byte[] v2 = ((ValueBytes) v).value; byte[] v2 = ((ValueBytes) v).value;
if (mode.isBinaryUnsigned()) { if (mode.isBinaryUnsigned()) {
return Utils.compareNotNullUnsigned(value, v2); return Bits.compareNotNullUnsigned(value, v2);
} }
return Utils.compareNotNullSigned(value, v2); return Bits.compareNotNullSigned(value, v2);
} }
@Override @Override
......
...@@ -11,6 +11,7 @@ import java.sql.Types; ...@@ -11,6 +11,7 @@ import java.sql.Types;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.store.DataHandler; import org.h2.store.DataHandler;
import org.h2.util.Bits;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -131,8 +132,7 @@ public class ValueJavaObject extends ValueBytes { ...@@ -131,8 +132,7 @@ public class ValueJavaObject extends ValueBytes {
if (o1.equals(o2)) { if (o1.equals(o2)) {
return 0; return 0;
} }
return Bits.compareNotNullSigned(getBytesNoCopy(), v.getBytesNoCopy());
return Utils.compareNotNullSigned(getBytesNoCopy(), v.getBytesNoCopy());
} }
return h1 > h2 ? 1 : -1; return h1 > h2 ? 1 : -1;
......
...@@ -26,6 +26,7 @@ import org.h2.store.FileStoreOutputStream; ...@@ -26,6 +26,7 @@ import org.h2.store.FileStoreOutputStream;
import org.h2.store.RangeInputStream; import org.h2.store.RangeInputStream;
import org.h2.store.RangeReader; import org.h2.store.RangeReader;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.util.Bits;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.SmallLRUCache; import org.h2.util.SmallLRUCache;
...@@ -674,7 +675,7 @@ public class ValueLob extends Value { ...@@ -674,7 +675,7 @@ public class ValueLob extends Value {
return Integer.signum(getString().compareTo(v.getString())); return Integer.signum(getString().compareTo(v.getString()));
} }
byte[] v2 = v.getBytesNoCopy(); byte[] v2 = v.getBytesNoCopy();
return Utils.compareNotNullSigned(getBytes(), v2); return Bits.compareNotNullSigned(getBytesNoCopy(), v2);
} }
@Override @Override
......
...@@ -27,6 +27,7 @@ import org.h2.store.LobStorageFrontend; ...@@ -27,6 +27,7 @@ import org.h2.store.LobStorageFrontend;
import org.h2.store.LobStorageInterface; import org.h2.store.LobStorageInterface;
import org.h2.store.RangeReader; import org.h2.store.RangeReader;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.util.Bits;
import org.h2.util.IOUtils; import org.h2.util.IOUtils;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
...@@ -363,7 +364,7 @@ public class ValueLobDb extends Value implements Value.ValueClob, ...@@ -363,7 +364,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
return Integer.signum(getString().compareTo(v.getString())); return Integer.signum(getString().compareTo(v.getString()));
} }
byte[] v2 = v.getBytesNoCopy(); byte[] v2 = v.getBytesNoCopy();
return Utils.compareNotNullSigned(getBytes(), v2); return Bits.compareNotNullSigned(getBytesNoCopy(), v2);
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论