From 92182952379483384b4d665ba4af489d5097d37d Mon Sep 17 00:00:00 2001 From: Kraity Date: Sat, 23 Jul 2022 08:50:41 +0800 Subject: [PATCH] perf: check LE/BE to use ByteOrder --- .../alibaba/fastjson2/JSONReaderJSONB.java | 18 +++++----- .../alibaba/fastjson2/JSONWriterJSONB.java | 4 +-- .../com/alibaba/fastjson2/util/JDKUtils.java | 33 ++++--------------- .../com/alibaba/fastjson2/util/JSONBDump.java | 8 ++--- .../alibaba/fastjson2/util/UnsafeUtils.java | 2 +- 5 files changed, 22 insertions(+), 43 deletions(-) diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java index 98d0fbbe5d..589fc13991 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java @@ -364,7 +364,7 @@ public Object readAny() { case BC_STR_UTF8: { int strlen = readLength(); - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { if (valueBytes == null) { valueBytes = CACHE_BYTES.getAndSet(cachedIndex, null); } @@ -402,7 +402,7 @@ public Object readAny() { int strlen = readLength(); String str; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -417,7 +417,7 @@ public Object readAny() { int strlen = readLength(); String str; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 1) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -1560,7 +1560,7 @@ public String readFieldName() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { if (valueBytes == null) { valueBytes = CACHE_BYTES.getAndSet(cachedIndex, null); } @@ -1592,7 +1592,7 @@ public String readFieldName() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -1604,7 +1604,7 @@ public String readFieldName() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 1) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -1720,7 +1720,7 @@ public String readString() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { if (valueBytes == null) { valueBytes = CACHE_BYTES.getAndSet(cachedIndex, null); } @@ -1762,7 +1762,7 @@ public String readString() { return ""; } - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -1779,7 +1779,7 @@ public String readString() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 1) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java index ced268f2a2..4f63f75d47 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java @@ -574,9 +574,9 @@ public void writeString(String str) { } } - if (utf16 && (JDKUtils.BIG_ENDIAN == 1 || JDKUtils.BIG_ENDIAN == 0)) { + if (utf16) { ensureCapacity(off + 5 + value.length); - bytes[off++] = JDKUtils.BIG_ENDIAN == 1 ? BC_STR_UTF16BE : BC_STR_UTF16LE; + bytes[off++] = JDKUtils.BIG_ENDIAN ? BC_STR_UTF16BE : BC_STR_UTF16LE; writeInt32(value.length); System.arraycopy(value, 0, bytes, off, value.length); off += value.length; diff --git a/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java index 9ba61f4181..7fe3cfc916 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java @@ -3,6 +3,7 @@ import java.lang.invoke.*; import java.lang.reflect.Constructor; import java.lang.reflect.Field; +import java.nio.ByteOrder; import java.util.function.*; public class JDKUtils { @@ -12,12 +13,12 @@ public class JDKUtils { static final long FIELD_STRING_VALUE_OFFSET; static volatile boolean FIELD_STRING_ERROR; - static final Class CLASS_SQL_DATASOURCE; - static final Class CLASS_SQL_ROW_SET; + static final Class CLASS_SQL_DATASOURCE; + static final Class CLASS_SQL_ROW_SET; public static final boolean HAS_SQL; public static final Class CLASS_TRANSIENT; - public static final byte BIG_ENDIAN; + public static final boolean BIG_ENDIAN; public static final boolean UNSAFE_SUPPORT; @@ -30,7 +31,6 @@ public class JDKUtils { public static final Function UNSAFE_ASCII_CREATOR; static { - boolean android = false; boolean openj9 = false; int jvmVersion = -1; try { @@ -99,28 +99,7 @@ public class JDKUtils { }).test(null); UNSAFE_SUPPORT = unsafeSupport; - Boolean bigEndian = null; - if (JDKUtils.JVM_VERSION > 8 && UNSAFE_SUPPORT) { - Class clazz; - try { - clazz = Class.forName("java.lang.StringUTF16"); - Field field = clazz.getDeclaredField("HI_BYTE_SHIFT"); - long fieldOffset = UnsafeUtils.UNSAFE.staticFieldOffset(field); - int hiByteShift = UnsafeUtils.UNSAFE.getInt(clazz, fieldOffset); - if (hiByteShift == 8) { - bigEndian = true; - } else { - if (hiByteShift == 0) { - bigEndian = false; - } - } - } catch (Exception ignored) { - ignored.printStackTrace(); - } - } - BIG_ENDIAN = bigEndian == null - ? -1 - : bigEndian.booleanValue() ? (byte) 1 : (byte) 0; + BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN; Function utf16Creator = null, asciiCreator = null; if (unsafeSupport) { @@ -136,7 +115,7 @@ public class JDKUtils { UNSAFE_ASCII_CREATOR = asciiCreator; } - public static boolean isSQLDataSourceOrRowSet(Class type) { + public static boolean isSQLDataSourceOrRowSet(Class type) { return (CLASS_SQL_DATASOURCE != null && CLASS_SQL_DATASOURCE.isAssignableFrom(type)) || (CLASS_SQL_ROW_SET != null && CLASS_SQL_ROW_SET.isAssignableFrom(type)); } diff --git a/core/src/main/java/com/alibaba/fastjson2/util/JSONBDump.java b/core/src/main/java/com/alibaba/fastjson2/util/JSONBDump.java index 5504cd8aed..d88ae22df6 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/JSONBDump.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/JSONBDump.java @@ -213,7 +213,7 @@ private void dumpAny() { int strlen = readLength(); String str; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -229,7 +229,7 @@ private void dumpAny() { int strlen = readLength(); String str; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 1) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -871,7 +871,7 @@ String readString() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 0) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && !JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); String str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); @@ -884,7 +884,7 @@ String readString() { strlen = readLength(); strBegin = offset; - if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN == 1) { + if (JDKUtils.UNSAFE_UTF16_CREATOR != null && JDKUtils.BIG_ENDIAN) { byte[] chars = new byte[strlen]; System.arraycopy(bytes, offset, chars, 0, strlen); String str = JDKUtils.UNSAFE_UTF16_CREATOR.apply(chars); diff --git a/core/src/main/java/com/alibaba/fastjson2/util/UnsafeUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/UnsafeUtils.java index 41d6792199..3b90644216 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/UnsafeUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/UnsafeUtils.java @@ -124,7 +124,7 @@ public String apply(byte[] bytes) { try { Object str = UNSAFE.allocateInstance(String.class); UNSAFE.putByte(str, coderOffset, (byte) 1); - UNSAFE.putObject(str, valueOffset, (byte[]) bytes); + UNSAFE.putObject(str, valueOffset, bytes); return (String) str; } catch (Throwable ex) { throw new JSONException("create string error");