Skip to content

Commit

Permalink
perf: check LE/BE to use ByteOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
kraity committed Jul 23, 2022
1 parent 1039d92 commit 9218295
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 43 deletions.
18 changes: 9 additions & 9 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 6 additions & 27 deletions core/src/main/java/com/alibaba/fastjson2/util/JDKUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand All @@ -30,7 +31,6 @@ public class JDKUtils {
public static final Function<byte[], String> UNSAFE_ASCII_CREATOR;

static {
boolean android = false;
boolean openj9 = false;
int jvmVersion = -1;
try {
Expand Down Expand Up @@ -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<byte[], String> utf16Creator = null, asciiCreator = null;
if (unsafeSupport) {
Expand All @@ -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));
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/com/alibaba/fastjson2/util/JSONBDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 9218295

Please sign in to comment.