Skip to content

Commit

Permalink
improved write int & long performance
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 29, 2023
1 parent f7344bb commit 9251364
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions core/src/main/java/com/alibaba/fastjson2/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,8 @@ public static int writeInt64(final byte[] buf, int pos, final long value) {
} else {
i = value;
}
final long q1 = i / 1000;
if (q1 == 0) {

if (i < 1000) {
int v = DIGITS_K[(int) i];
int start = v >> 24;
if (start == 0) {
Expand All @@ -901,10 +901,11 @@ public static int writeInt64(final byte[] buf, int pos, final long value) {
buf[pos++] = (byte) v;
return pos;
}

final long q1 = i / 1000;
final int r1 = (int) (i - q1 * 1000);
final long q2 = q1 / 1000;
final int v1 = DIGITS_K[r1];
if (q2 == 0) {
if (i < 1000000) {
final int v2 = DIGITS_K[(int) q1];

int start = v2 >> 24;
Expand All @@ -921,6 +922,8 @@ public static int writeInt64(final byte[] buf, int pos, final long value) {
buf[pos + 3] = (byte) v1;
return pos + 4;
}

final long q2 = q1 / 1000;
final int r2 = (int) (q1 - q2 * 1000);
final long q3 = q2 / 1000;
final int v2 = DIGITS_K[r2];
Expand Down Expand Up @@ -1054,8 +1057,8 @@ public static int writeInt64(final char[] buf, int pos, final long value) {
} else {
i = value;
}
final long q1 = i / 1000;
if (q1 == 0) {

if (i < 1000) {
int v = DIGITS_K[(int) i];
int start = v >> 24;
if (start == 0) {
Expand All @@ -1068,10 +1071,11 @@ public static int writeInt64(final char[] buf, int pos, final long value) {
buf[pos++] = (char) (byte) v;
return pos;
}

final long q1 = i / 1000;
final int r1 = (int) (i - q1 * 1000);
final long q2 = q1 / 1000;
final int v1 = DIGITS_K[r1];
if (q2 == 0) {
if (i < 1000000) {
final int v2 = DIGITS_K[(int) q1];

int start = v2 >> 24;
Expand All @@ -1088,6 +1092,8 @@ public static int writeInt64(final char[] buf, int pos, final long value) {
buf[pos + 3] = (char) (byte) v1;
return pos + 4;
}

final long q2 = q1 / 1000;
final int r2 = (int) (q1 - q2 * 1000);
final long q3 = q2 / 1000;
final int v2 = DIGITS_K[r2];
Expand Down Expand Up @@ -1218,8 +1224,8 @@ public static int writeInt32(final byte[] buf, int pos, final int value) {
} else {
i = value;
}
final int q1 = i / 1000;
if (q1 == 0) {

if (i < 1000) {
int v = DIGITS_K[i];
final int start = v >> 24;
if (start == 0) {
Expand All @@ -1232,10 +1238,11 @@ public static int writeInt32(final byte[] buf, int pos, final int value) {
buf[pos] = (byte) v;
return pos + 1;
}

final int q1 = i / 1000;
final int r1 = i - q1 * 1000;
final int q2 = q1 / 1000;
final int v1 = DIGITS_K[r1];
if (q2 == 0) {
if (i < 1000000) {
final int v2 = DIGITS_K[q1];
int start = v2 >> 24;
if (start == 0) {
Expand All @@ -1251,6 +1258,7 @@ public static int writeInt32(final byte[] buf, int pos, final int value) {
buf[pos + 3] = (byte) v1;
return pos + 4;
}
final int q2 = q1 / 1000;
final int r2 = q1 - q2 * 1000;
final int q3 = q2 / 1000;
final int v2 = DIGITS_K[r2];
Expand Down Expand Up @@ -1296,8 +1304,7 @@ public static int writeInt32(final char[] buf, int pos, final int value) {
} else {
i = value;
}
final int q1 = i / 1000;
if (q1 == 0) {
if (i < 1000) {
int v = DIGITS_K[i];
final int start = v >> 24;
if (start == 0) {
Expand All @@ -1310,10 +1317,10 @@ public static int writeInt32(final char[] buf, int pos, final int value) {
buf[pos] = (char) (byte) v;
return pos + 1;
}
final int q1 = i / 1000;
final int r1 = i - q1 * 1000;
final int q2 = q1 / 1000;
final int v1 = DIGITS_K[r1];
if (q2 == 0) {
if (i < 1000000) {
final int v2 = DIGITS_K[q1];
int start = v2 >> 24;
if (start == 0) {
Expand All @@ -1329,6 +1336,7 @@ public static int writeInt32(final char[] buf, int pos, final int value) {
buf[pos + 3] = (char) (byte) v1;
return pos + 4;
}
final int q2 = q1 / 1000;
final int r2 = q1 - q2 * 1000;
final int q3 = q2 / 1000;
final int v2 = DIGITS_K[r2];
Expand Down

0 comments on commit 9251364

Please sign in to comment.