Skip to content

Commit c787714

Browse files
committed
improved large object deserialize performance
1 parent abc326c commit c787714

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java

+36-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class JavaBeanDeserializer implements ObjectDeserializer {
3232
private ConcurrentMap<String, Object> extraFieldDeserializers;
3333

3434
private final Map<String, FieldDeserializer> alterNameFieldDeserializers;
35+
private Map<String, FieldDeserializer> fieldDeserializerMap;
3536

3637
private transient long[] smartMatchHashArray;
3738
private transient short[] smartMatchHashArrayMapping;
@@ -61,6 +62,13 @@ public JavaBeanDeserializer(ParserConfig config, JavaBeanInfo beanInfo){
6162

6263
sortedFieldDeserializers[i] = fieldDeserializer;
6364

65+
if (size > 128) {
66+
if (fieldDeserializerMap == null) {
67+
fieldDeserializerMap = new HashMap<String, FieldDeserializer>();
68+
}
69+
fieldDeserializerMap.put(fieldInfo.name, fieldDeserializer);
70+
}
71+
6472
for (String name : fieldInfo.alternateNames) {
6573
if (alterNameFieldDeserializers == null) {
6674
alterNameFieldDeserializers = new HashMap<String, FieldDeserializer>();
@@ -86,6 +94,13 @@ public FieldDeserializer getFieldDeserializer(String key, int[] setFlags) {
8694
if (key == null) {
8795
return null;
8896
}
97+
98+
if (fieldDeserializerMap != null) {
99+
FieldDeserializer fieldDeserializer = fieldDeserializerMap.get(key);
100+
if (fieldDeserializer != null) {
101+
return fieldDeserializer;
102+
}
103+
}
89104

90105
int low = 0;
91106
int high = sortedFieldDeserializers.length - 1;
@@ -479,14 +494,14 @@ protected <T> T deserialze(DefaultJSONParser parser, //
479494
}
480495

481496
String typeKey = beanInfo.typeKey;
482-
for (int fieldIndex = 0;; fieldIndex++) {
497+
for (int fieldIndex = 0, notMatchCount = 0;; fieldIndex++) {
483498
String key = null;
484499
FieldDeserializer fieldDeser = null;
485500
FieldInfo fieldInfo = null;
486501
Class<?> fieldClass = null;
487502
JSONField feildAnnotation = null;
488503
boolean customDeserilizer = false;
489-
if (fieldIndex < sortedFieldDeserializers.length) {
504+
if (fieldIndex < sortedFieldDeserializers.length && notMatchCount < 16) {
490505
fieldDeser = sortedFieldDeserializers[fieldIndex];
491506
fieldInfo = fieldDeser.fieldInfo;
492507
fieldClass = fieldInfo.fieldClass;
@@ -516,7 +531,8 @@ protected <T> T deserialze(DefaultJSONParser parser, //
516531
matchField = true;
517532
valueParsed = true;
518533
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
519-
continue;
534+
notMatchCount++;
535+
continue;
520536
}
521537
} else if (fieldClass == long.class || fieldClass == Long.class) {
522538
long longVal = lexer.scanFieldLong(name_chars);
@@ -530,7 +546,8 @@ protected <T> T deserialze(DefaultJSONParser parser, //
530546
matchField = true;
531547
valueParsed = true;
532548
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
533-
continue;
549+
notMatchCount++;
550+
continue;
534551
}
535552
} else if (fieldClass == String.class) {
536553
fieldValue = lexer.scanFieldString(name_chars);
@@ -539,7 +556,8 @@ protected <T> T deserialze(DefaultJSONParser parser, //
539556
matchField = true;
540557
valueParsed = true;
541558
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
542-
continue;
559+
notMatchCount++;
560+
continue;
543561
}
544562
} else if (fieldClass == java.util.Date.class && fieldInfo.format == null) {
545563
fieldValue = lexer.scanFieldDate(name_chars);
@@ -548,6 +566,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
548566
matchField = true;
549567
valueParsed = true;
550568
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
569+
notMatchCount++;
551570
continue;
552571
}
553572
} else if (fieldClass == BigDecimal.class) {
@@ -557,6 +576,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
557576
matchField = true;
558577
valueParsed = true;
559578
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
579+
notMatchCount++;
560580
continue;
561581
}
562582
} else if (fieldClass == BigInteger.class) {
@@ -566,6 +586,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
566586
matchField = true;
567587
valueParsed = true;
568588
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
589+
notMatchCount++;
569590
continue;
570591
}
571592
} else if (fieldClass == boolean.class || fieldClass == Boolean.class) {
@@ -581,7 +602,8 @@ protected <T> T deserialze(DefaultJSONParser parser, //
581602
matchField = true;
582603
valueParsed = true;
583604
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
584-
continue;
605+
notMatchCount++;
606+
continue;
585607
}
586608
} else if (fieldClass == float.class || fieldClass == Float.class) {
587609
float floatVal = lexer.scanFieldFloat(name_chars);
@@ -595,7 +617,8 @@ protected <T> T deserialze(DefaultJSONParser parser, //
595617
matchField = true;
596618
valueParsed = true;
597619
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
598-
continue;
620+
notMatchCount++;
621+
continue;
599622
}
600623
} else if (fieldClass == double.class || fieldClass == Double.class) {
601624
double doubleVal = lexer.scanFieldDouble(name_chars);
@@ -609,7 +632,8 @@ protected <T> T deserialze(DefaultJSONParser parser, //
609632
matchField = true;
610633
valueParsed = true;
611634
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
612-
continue;
635+
notMatchCount++;
636+
continue;
613637
}
614638
} else if (fieldClass.isEnum() //
615639
&& parser.getConfig().getDeserializer(fieldClass) instanceof EnumDeserializer
@@ -623,6 +647,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
623647
matchField = true;
624648
valueParsed = true;
625649
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
650+
notMatchCount++;
626651
continue;
627652
}
628653
}
@@ -633,6 +658,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
633658
matchField = true;
634659
valueParsed = true;
635660
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
661+
notMatchCount++;
636662
continue;
637663
}
638664
} else if (fieldClass == float[].class) {
@@ -642,6 +668,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
642668
matchField = true;
643669
valueParsed = true;
644670
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
671+
notMatchCount++;
645672
continue;
646673
}
647674
} else if (fieldClass == float[][].class) {
@@ -651,6 +678,7 @@ protected <T> T deserialze(DefaultJSONParser parser, //
651678
matchField = true;
652679
valueParsed = true;
653680
} else if (lexer.matchStat == JSONLexer.NOT_MATCH_NAME) {
681+
notMatchCount++;
654682
continue;
655683
}
656684
} else if (lexer.matchField(name_chars)) {

0 commit comments

Comments
 (0)