Skip to content

Commit

Permalink
refactor for serialize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 30, 2016
1 parent 495824f commit 9a38251
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
Expand Up @@ -1302,8 +1302,8 @@ private void _string(Class<?> clazz, MethodVisitor mw, FieldInfo property, Conte
mw.visitLabel(_end); mw.visitLabel(_end);
} }


private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) { private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo fieldInfo, Context context) {
Type propertyType = property.fieldType; Type propertyType = fieldInfo.fieldType;


Type elementType; Type elementType;
if (propertyType instanceof Class) { if (propertyType instanceof Class) {
Expand All @@ -1319,16 +1319,16 @@ private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context


Label _end = new Label(), _else = new Label(), _end_if = new Label(); Label _end = new Label(), _else = new Label(), _end_if = new Label();


_nameApply(mw, property, context, _end); _nameApply(mw, fieldInfo, context, _end);
_get(mw, context, property); _get(mw, context, fieldInfo);
mw.visitTypeInsn(CHECKCAST, "java/util/List"); // cast mw.visitTypeInsn(CHECKCAST, "java/util/List"); // cast
mw.visitVarInsn(ASTORE, context.var("list")); mw.visitVarInsn(ASTORE, context.var("list"));


_filters(mw, property, context, _end); _filters(mw, fieldInfo, context, _end);


mw.visitVarInsn(ALOAD, context.var("list")); mw.visitVarInsn(ALOAD, context.var("list"));
mw.visitJumpInsn(IFNONNULL, _else); mw.visitJumpInsn(IFNONNULL, _else);
_if_write_null(mw, property, context); _if_write_null(mw, fieldInfo, context);
mw.visitJumpInsn(GOTO, _end_if); mw.visitJumpInsn(GOTO, _end_if);


mw.visitLabel(_else); // else { mw.visitLabel(_else); // else {
Expand All @@ -1347,7 +1347,6 @@ private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context
mw.visitInsn(ICONST_0); mw.visitInsn(ICONST_0);
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldName", "(Ljava/lang/String;Z)V"); mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeFieldName", "(Ljava/lang/String;Z)V");
} }



// //
mw.visitVarInsn(ALOAD, context.var("list")); mw.visitVarInsn(ALOAD, context.var("list"));
Expand Down Expand Up @@ -1405,11 +1404,50 @@ private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V"); mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");


mw.visitLabel(forFirst_); mw.visitLabel(forFirst_);


mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitVarInsn(ALOAD, context.var("list")); mw.visitVarInsn(ALOAD, context.var("list"));
mw.visitVarInsn(ILOAD, context.var("i")); mw.visitVarInsn(ILOAD, context.var("i"));
mw.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;"); mw.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;");
mw.visitVarInsn(ASTORE, context.var("list_item"));

Label forItemNullEnd_ = new Label(), forItemNullElse_ = new Label();

mw.visitVarInsn(ALOAD, context.var("list_item"));
mw.visitJumpInsn(IFNONNULL, forItemNullElse_);

mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "writeNull", "()V");
mw.visitJumpInsn(GOTO, forItemNullEnd_);

mw.visitLabel(forItemNullElse_);

Label forItemClassIfEnd_ = new Label(), forItemClassIfElse_ = new Label();
if (elementClass != null) {
mw.visitVarInsn(ALOAD, context.var("list_item"));
mw.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(elementClass)));
mw.visitJumpInsn(IF_ACMPNE, forItemClassIfElse_);

_getListFieldItemSer(context, mw, fieldInfo, elementClass);
mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitVarInsn(ALOAD, context.var("list_item")); // object
if (context.nonContext) { // fieldName
mw.visitInsn(ACONST_NULL);
} else {
mw.visitVarInsn(ILOAD, context.var("i"));
mw.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
}
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(elementClass))); // fieldType
mw.visitLdcInsn(fieldInfo.serialzeFeatures); // features
mw.visitMethodInsn(INVOKEINTERFACE, type(ObjectSerializer.class), "write", //
"(L" + JSONSerializer + ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
mw.visitJumpInsn(GOTO, forItemClassIfEnd_);
}

mw.visitLabel(forItemClassIfElse_);

mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitVarInsn(ALOAD, context.var("list_item"));
if (context.nonContext) { if (context.nonContext) {
mw.visitInsn(ACONST_NULL); mw.visitInsn(ACONST_NULL);
} else { } else {
Expand All @@ -1419,13 +1457,16 @@ private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context


if (elementClass != null && Modifier.isPublic(elementClass.getModifiers())) { if (elementClass != null && Modifier.isPublic(elementClass.getModifiers())) {
mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc((Class<?>) elementType))); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc((Class<?>) elementType)));
mw.visitLdcInsn(property.serialzeFeatures); mw.visitLdcInsn(fieldInfo.serialzeFeatures);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName", mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName",
"(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V"); "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
} else { } else {
mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName", mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeWithFieldName",
"(Ljava/lang/Object;Ljava/lang/Object;)V"); "(Ljava/lang/Object;Ljava/lang/Object;)V");
} }

mw.visitLabel(forItemClassIfEnd_);
mw.visitLabel(forItemNullEnd_);


mw.visitIincInsn(context.var("i"), 1); mw.visitIincInsn(context.var("i"), 1);
mw.visitJumpInsn(GOTO, for_); mw.visitJumpInsn(GOTO, for_);
Expand Down
Expand Up @@ -11,7 +11,7 @@ public static void main(String[] args) throws Exception {
BenchmarkExecutor executor = new BenchmarkExecutor(); BenchmarkExecutor executor = new BenchmarkExecutor();
executor.setExecuteCount(5); executor.setExecuteCount(5);
// executor.getCodecList().add(new FastjsonManualCodec()); // executor.getCodecList().add(new FastjsonManualCodec());
// executor.getCodecList().add(new FastjsonCodec()); executor.getCodecList().add(new FastjsonCodec());
executor.getCodecList().add(new FastjsonBeanToArrayCodec()); executor.getCodecList().add(new FastjsonBeanToArrayCodec());
// executor.getCodecList().add(new FastjsonGenCodec()); // executor.getCodecList().add(new FastjsonGenCodec());
// executor.getCodecList().add(new FastjsonBeanToArrayCodec()); // executor.getCodecList().add(new FastjsonBeanToArrayCodec());
Expand Down

0 comments on commit 9a38251

Please sign in to comment.