Skip to content

Commit

Permalink
make MIXED_HASH_ALGORITHM as default
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 authored and wenshao committed Jun 14, 2023
1 parent e179bfd commit e18acc3
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 477 deletions.
23 changes: 0 additions & 23 deletions core/src/main/java/com/alibaba/fastjson2/JSONFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.function.Function;
import java.util.function.Supplier;

import static com.alibaba.fastjson2.util.JDKUtils.JVM_VERSION;
import static com.alibaba.fastjson2.util.JDKUtils.VECTOR_BIT_LENGTH;

public final class JSONFactory {
Expand All @@ -31,8 +30,6 @@ public final class JSONFactory {
public static final String PROPERTY_AUTO_TYPE_HANDLER = "fastjson2.autoTypeHandler";
public static final String PROPERTY_AUTO_TYPE_BEFORE_HANDLER = "fastjson2.autoTypeBeforeHandler";

public static final boolean MIXED_HASH_ALGORITHM;

static boolean useJacksonAnnotation;

public static String getProperty(String key) {
Expand Down Expand Up @@ -168,26 +165,6 @@ public NameCacheEntry2(String name, long value0, long value1) {
CREATOR = property == null ? "asm" : property;
}

{
String property = System.getProperty("fastjson2.hash-algorithm");
if (property != null) {
property = property.trim();
}

if (property == null || property.isEmpty()) {
property = properties.getProperty("fastjson2.hash-algorithm");
if (property != null) {
property = property.trim();
}
}

if ("mixed".equals(property)) {
MIXED_HASH_ALGORITHM = true;
} else {
MIXED_HASH_ALGORITHM = JVM_VERSION > 8;
}
}

{
String property = System.getProperty("fastjson2.useJacksonAnnotation");
if (property != null) {
Expand Down
279 changes: 137 additions & 142 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderASCII.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public final long readFieldNameHashCode() {
int offset = this.nameBegin = this.offset;

long nameValue = 0;
if (MIXED_HASH_ALGORITHM && offset + 9 < end) {
if (offset + 9 < end) {
byte c0, c1, c2, c3, c4, c5, c6, c7;

if ((c0 = bytes[offset]) == quote) {
Expand Down Expand Up @@ -335,7 +335,7 @@ public final long readFieldNameHashCode() {
}
}

if (MIXED_HASH_ALGORITHM && nameValue == 0) {
if (nameValue == 0) {
for (int i = 0; offset < end; offset++, i++) {
int c = bytes[offset];

Expand Down Expand Up @@ -542,84 +542,82 @@ public final long readValueHashCode() {
int offset = this.nameBegin = this.offset;

long nameValue = 0;
if (MIXED_HASH_ALGORITHM) {
for (int i = 0; offset < end; offset++, i++) {
int c = bytes[offset];

if (c == quote) {
if (i == 0) {
nameValue = 0;
offset = this.nameBegin;
break;
}

this.nameLength = i;
this.nameEnd = offset;
offset++;
break;
}

if (c == '\\') {
nameEscape = true;
c = bytes[++offset];
switch (c) {
case 'u': {
byte c1 = bytes[++offset];
byte c2 = bytes[++offset];
byte c3 = bytes[++offset];
byte c4 = bytes[++offset];
c = char4(c1, c2, c3, c4);
break;
}
case 'x': {
byte c1 = bytes[++offset];
byte c2 = bytes[++offset];
c = char2(c1, c2);
break;
}
case '\\':
case '"':
default:
c = char1(c);
break;
}
}
for (int i = 0; offset < end; offset++, i++) {
int c = bytes[offset];

if (c > 0xFF || i >= 8 || (i == 0 && c == 0)) {
if (c == quote) {
if (i == 0) {
nameValue = 0;
offset = this.nameBegin;
break;
}

switch (i) {
case 0:
nameValue = (byte) c;
break;
case 1:
nameValue = (((byte) c) << 8) + (nameValue & 0xFFL);
break;
case 2:
nameValue = (((byte) c) << 16) + (nameValue & 0xFFFFL);
break;
case 3:
nameValue = (((byte) c) << 24) + (nameValue & 0xFFFFFFL);
break;
case 4:
nameValue = (((long) (byte) c) << 32) + (nameValue & 0xFFFFFFFFL);
break;
case 5:
nameValue = (((long) (byte) c) << 40L) + (nameValue & 0xFFFFFFFFFFL);
break;
case 6:
nameValue = (((long) (byte) c) << 48L) + (nameValue & 0xFFFFFFFFFFFFL);
this.nameLength = i;
this.nameEnd = offset;
offset++;
break;
}

if (c == '\\') {
nameEscape = true;
c = bytes[++offset];
switch (c) {
case 'u': {
byte c1 = bytes[++offset];
byte c2 = bytes[++offset];
byte c3 = bytes[++offset];
byte c4 = bytes[++offset];
c = char4(c1, c2, c3, c4);
break;
case 7:
nameValue = (((long) (byte) c) << 56L) + (nameValue & 0xFFFFFFFFFFFFFFL);
}
case 'x': {
byte c1 = bytes[++offset];
byte c2 = bytes[++offset];
c = char2(c1, c2);
break;
}
case '\\':
case '"':
default:
c = char1(c);
break;
}
}

if (c > 0xFF || i >= 8 || (i == 0 && c == 0)) {
nameValue = 0;
offset = this.nameBegin;
break;
}

switch (i) {
case 0:
nameValue = (byte) c;
break;
case 1:
nameValue = (((byte) c) << 8) + (nameValue & 0xFFL);
break;
case 2:
nameValue = (((byte) c) << 16) + (nameValue & 0xFFFFL);
break;
case 3:
nameValue = (((byte) c) << 24) + (nameValue & 0xFFFFFFL);
break;
case 4:
nameValue = (((long) (byte) c) << 32) + (nameValue & 0xFFFFFFFFL);
break;
case 5:
nameValue = (((long) (byte) c) << 40L) + (nameValue & 0xFFFFFFFFFFL);
break;
case 6:
nameValue = (((long) (byte) c) << 48L) + (nameValue & 0xFFFFFFFFFFFFL);
break;
case 7:
nameValue = (((long) (byte) c) << 56L) + (nameValue & 0xFFFFFFFFFFFFFFL);
break;
default:
break;
}
}

long hashCode;
Expand Down Expand Up @@ -708,90 +706,87 @@ public final long readValueHashCode() {
@Override
public final long getNameHashCodeLCase() {
int offset = nameBegin;
long nameValue = 0;
for (int i = 0; offset < end; offset++) {
int c = bytes[offset];

if (MIXED_HASH_ALGORITHM) {
long nameValue = 0;
for (int i = 0; offset < end; offset++) {
int c = bytes[offset];

if (c == '\\') {
c = bytes[++offset];
switch (c) {
case 'u': {
int c1 = bytes[++offset];
int c2 = bytes[++offset];
int c3 = bytes[++offset];
int c4 = bytes[++offset];
c = char4(c1, c2, c3, c4);
break;
}
case 'x': {
int c1 = bytes[++offset];
int c2 = bytes[++offset];
c = char2(c1, c2);
break;
}
case '\\':
case '"':
default:
c = char1(c);
break;
if (c == '\\') {
c = bytes[++offset];
switch (c) {
case 'u': {
int c1 = bytes[++offset];
int c2 = bytes[++offset];
int c3 = bytes[++offset];
int c4 = bytes[++offset];
c = char4(c1, c2, c3, c4);
break;
}
} else if (c == '"') {
break;
}

if (c > 0xFF || c < 0 || i >= 8 || (i == 0 && c == 0)) {
nameValue = 0;
offset = this.nameBegin;
break;
}

if (c == '_' || c == '-' || c == ' ') {
byte c1 = bytes[offset + 1];
if (c1 != '"' && c1 != '\'' && c1 != c) {
continue;
case 'x': {
int c1 = bytes[++offset];
int c2 = bytes[++offset];
c = char2(c1, c2);
break;
}
case '\\':
case '"':
default:
c = char1(c);
break;
}
} else if (c == '"') {
break;
}

if (c >= 'A' && c <= 'Z') {
c = (char) (c + 32);
}
if (c > 0xFF || c < 0 || i >= 8 || (i == 0 && c == 0)) {
nameValue = 0;
offset = this.nameBegin;
break;
}

switch (i) {
case 0:
nameValue = (byte) c;
break;
case 1:
nameValue = (((byte) c) << 8) + (nameValue & 0xFFL);
break;
case 2:
nameValue = (((byte) c) << 16) + (nameValue & 0xFFFFL);
break;
case 3:
nameValue = (((byte) c) << 24) + (nameValue & 0xFFFFFFL);
break;
case 4:
nameValue = (((long) (byte) c) << 32) + (nameValue & 0xFFFFFFFFL);
break;
case 5:
nameValue = (((long) (byte) c) << 40) + (nameValue & 0xFFFFFFFFFFL);
break;
case 6:
nameValue = (((long) (byte) c) << 48) + (nameValue & 0xFFFFFFFFFFFFL);
break;
case 7:
nameValue = (((long) (byte) c) << 56) + (nameValue & 0xFFFFFFFFFFFFFFL);
break;
default:
break;
if (c == '_' || c == '-' || c == ' ') {
byte c1 = bytes[offset + 1];
if (c1 != '"' && c1 != '\'' && c1 != c) {
continue;
}
++i;
}

if (nameValue != 0) {
return nameValue;
if (c >= 'A' && c <= 'Z') {
c = (char) (c + 32);
}

switch (i) {
case 0:
nameValue = (byte) c;
break;
case 1:
nameValue = (((byte) c) << 8) + (nameValue & 0xFFL);
break;
case 2:
nameValue = (((byte) c) << 16) + (nameValue & 0xFFFFL);
break;
case 3:
nameValue = (((byte) c) << 24) + (nameValue & 0xFFFFFFL);
break;
case 4:
nameValue = (((long) (byte) c) << 32) + (nameValue & 0xFFFFFFFFL);
break;
case 5:
nameValue = (((long) (byte) c) << 40) + (nameValue & 0xFFFFFFFFFFL);
break;
case 6:
nameValue = (((long) (byte) c) << 48) + (nameValue & 0xFFFFFFFFFFFFL);
break;
case 7:
nameValue = (((long) (byte) c) << 56) + (nameValue & 0xFFFFFFFFFFFFFFL);
break;
default:
break;
}
++i;
}

if (nameValue != 0) {
return nameValue;
}

long hashCode = Fnv.MAGIC_HASH_CODE;
Expand Down
Loading

0 comments on commit e18acc3

Please sign in to comment.