optimize IOUtils

This commit is contained in:
wenshao 2025-02-23 12:37:15 +08:00
parent 0f5ddf1761
commit 86f155b5c6
4 changed files with 69 additions and 22 deletions

View File

@ -6567,7 +6567,10 @@ class JSONReaderUTF8
}
static long parse4Nibbles(byte[] bytes, int offset) {
int x = getIntLE(bytes, offset);
int x = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
if (BIG_ENDIAN) {
x = Integer.reverseBytes(x);
}
byte[] ns = NIBBLES;
return ns[x & 0xFF] << 12 | ns[(x >> 8) & 0xFF] << 8 | ns[(x >> 16) & 0xFF] << 4 | ns[(x >> 24) & 0xFF];
}

View File

@ -1067,8 +1067,11 @@ class JSONWriterUTF8
15 = 0b0000_1111 => m = ((i + 6) & 0x10); (m << 1) + (m >> 1) - (m >> 4) => 39 + 0x30 + (i & 0xF) => 'f'
*/
long m = (i + 0x0606_0606_0606_0606L) & 0x1010_1010_1010_1010L;
return convEndian(true,
((m << 1) + (m >> 1) - (m >> 4)) + 0x3030_3030_3030_3030L + i);
long v = ((m << 1) + (m >> 1) - (m >> 4)) + 0x3030_3030_3030_3030L + i;
if (!BIG_ENDIAN) {
v = Long.reverseBytes(v);
}
return v;
}
/**

View File

@ -9040,7 +9040,10 @@ public class DateUtils {
};
public static long hms(byte[] bytes, int off) {
long v = getLongLE(bytes, off);
long v = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + off);
if (BIG_ENDIAN) {
v = Long.reverseBytes(v);
}
long d;
if ((((v & 0xF0F0F0F0_F0F0F0F0L) - 0x30303030_30303030L) | (((d = v & 0x0F0F0F0F_0F0F0F0FL) + 0x06060006_06000606L) & 0xF0F000F0_F000F0F0L)) != 0
|| (d & 0x00000F00_000F0000L) != 0x00000a00_000a0000L) { // 00:00:00
@ -9050,7 +9053,10 @@ public class DateUtils {
}
public static long ymd(byte[] bytes, int off) {
long v = getLongLE(bytes, off);
long v = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + off);
if (BIG_ENDIAN) {
v = Long.reverseBytes(v);
}
long d;
if (((v & 0x0000FF00_00FF0000L) != 0x00002d00_002d0000L) // yy-mm-dd
|| (((v & 0xF0F000F0_F000F0F0L) - 0x30300030_30003030L) | (((d = v & 0x0F0F000F_0F000F0FL) + 0x06060006_06000606L) & 0xF0F000F0_F000F0F0L)) != 0) {
@ -9060,7 +9066,10 @@ public class DateUtils {
}
public static int yy(byte[] bytes, int off) {
int x = getShortLE(bytes, off);
short x = UNSAFE.getShort(bytes, ARRAY_BYTE_BASE_OFFSET + off);
if (BIG_ENDIAN) {
x = Short.reverseBytes(x);
}
int d;
if ((((x & 0xF0F0) - 0x3030) | (((d = x & 0x0F0F) + 0x0606) & 0xF0F0)) != 0
) {

View File

@ -909,11 +909,19 @@ public class IOUtils {
}
private static int mergeInt32(int v1, int v2) {
return convEndian(false, PACKED_DIGITS[v2 & 0x7f] | (PACKED_DIGITS[v1 & 0x7f] << 16));
int v = PACKED_DIGITS[v2 & 0x7f] | (PACKED_DIGITS[v1 & 0x7f] << 16);
if (BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
return v;
}
private static long mergeInt64(int v1, int v2) {
return convEndian(false, PACKED_DIGITS_UTF16[v2 & 0x7f] | ((long) PACKED_DIGITS_UTF16[v1 & 0x7f] << 32));
long v = PACKED_DIGITS_UTF16[v2 & 0x7f] | ((long) PACKED_DIGITS_UTF16[v1 & 0x7f] << 32);
if (BIG_ENDIAN) {
v = Long.reverseBytes(v);
}
return v;
}
private static int writeInt3(byte[] buf, int off, int val) {
@ -1268,15 +1276,24 @@ public class IOUtils {
}
public static void putIntBE(byte[] buf, int pos, int v) {
UNSAFE.putInt(buf, ARRAY_BYTE_BASE_OFFSET + pos, convEndian(true, v));
if (!BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
UNSAFE.putInt(buf, ARRAY_BYTE_BASE_OFFSET + pos, v);
}
public static void putIntLE(byte[] buf, int pos, int v) {
UNSAFE.putInt(buf, ARRAY_BYTE_BASE_OFFSET + pos, convEndian(false, v));
if (BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
UNSAFE.putInt(buf, ARRAY_BYTE_BASE_OFFSET + pos, v);
}
public static void putIntLE(char[] buf, int pos, int v) {
UNSAFE.putInt(buf, ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), convEndian(false, v));
if (BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
UNSAFE.putInt(buf, ARRAY_CHAR_BASE_OFFSET + ((long) pos << 1), v);
}
public static void putShortUnaligned(byte[] buf, int pos, short v) {
@ -1637,18 +1654,27 @@ public class IOUtils {
}
public static int getIntBE(byte[] bytes, int offset) {
return convEndian(true,
UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset));
int v = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
if (!BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
return v;
}
public static int getIntLE(byte[] bytes, int offset) {
return convEndian(false,
UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset));
int v = UNSAFE.getInt(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
if (BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
return v;
}
public static int getIntLE(char[] bytes, int offset) {
return convEndian(false,
UNSAFE.getInt(bytes, ARRAY_CHAR_BASE_OFFSET + ((long) offset << 1)));
int v = UNSAFE.getInt(bytes, ARRAY_CHAR_BASE_OFFSET + ((long) offset << 1));
if (BIG_ENDIAN) {
v = Integer.reverseBytes(v);
}
return v;
}
public static int getIntUnaligned(byte[] bytes, int offset) {
@ -1660,8 +1686,11 @@ public class IOUtils {
}
public static long getLongBE(byte[] bytes, int offset) {
return convEndian(true,
UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset));
long v = UNSAFE.getLong(bytes, ARRAY_BYTE_BASE_OFFSET + offset);
if (!BIG_ENDIAN) {
v = Long.reverseBytes(v);
}
return v;
}
public static long getLongUnaligned(byte[] bytes, int offset) {
@ -1678,8 +1707,11 @@ public class IOUtils {
}
public static long getLongLE(char[] bytes, int offset) {
return convEndian(false,
UNSAFE.getLong(bytes, ARRAY_CHAR_BASE_OFFSET + ((long) offset << 1)));
long v = UNSAFE.getLong(bytes, ARRAY_CHAR_BASE_OFFSET + ((long) offset << 1));
if (BIG_ENDIAN) {
v = Long.reverseBytes(v);
}
return v;
}
public static short hex2(int i) {
@ -1764,7 +1796,7 @@ public class IOUtils {
return ((i & 0xF000L) >> 12) | ((i & 0xF00L) << 8) | ((i & 0xF0L) << 28) | ((i & 0xFL) << 48);
}
static int convEndian(boolean big, int n) {
public static int convEndian(boolean big, int n) {
return big == BIG_ENDIAN ? n : Integer.reverseBytes(n);
}