Skip to content

Commit

Permalink
optimize: deserialize performance optimize (#6727)
Browse files Browse the repository at this point in the history
  • Loading branch information
wt-better authored Aug 6, 2024
1 parent d0f8c6f commit c1515ac
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 131 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6667](https://github.com/apache/incubator-seata/pull/6667)] optimize Namingserver log output
- [[#6687](https://github.com/apache/incubator-seata/pull/6687)] delete static code built on the frontend
- [[#6700](https://github.com/apache/incubator-seata/pull/6700)] remove sdk version checking
- [[#6727](https://github.com/apache/incubator-seata/pull/6727)] deserialize performance optimize

### refactor:

Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- [[#6667](https://github.com/apache/incubator-seata/pull/6667)] 优化Namingserver日志输出
- [[#6687](https://github.com/apache/incubator-seata/pull/6687)] 删除前端构建的静态代码
- [[#6700](https://github.com/apache/incubator-seata/pull/6700)] 去掉sdk版本检查
- [[#6727](https://github.com/apache/incubator-seata/pull/6727)] 反序列化性能优化

### refactor:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,90 +137,69 @@ public static MessageSeataCodec getMessageCodec(short typeCode, byte version) {
case MessageType.TYPE_BATCH_RESULT_MSG:
msgCodec = new BatchResultMessageCodec(version);
break;
default:
break;
}

if (msgCodec != null) {
return msgCodec;
}

try {
msgCodec = getMergeRequestMessageSeataCodec(typeCode, version);
} catch (Exception exx) {
}

if (msgCodec != null) {
return msgCodec;
}

msgCodec = getMergeResponseMessageSeataCodec(typeCode, version);

return msgCodec;
}

/**
* Gets merge request instance by code.
*
* @param typeCode the type code
* @return the merge request instance by code
*/
protected static MessageSeataCodec getMergeRequestMessageSeataCodec(int typeCode, byte version) {
switch (typeCode) {
case MessageType.TYPE_GLOBAL_BEGIN:
return new GlobalBeginRequestCodec();
msgCodec = new GlobalBeginRequestCodec();
break;
case MessageType.TYPE_GLOBAL_COMMIT:
return new GlobalCommitRequestCodec();
msgCodec = new GlobalCommitRequestCodec();
break;
case MessageType.TYPE_GLOBAL_ROLLBACK:
return new GlobalRollbackRequestCodec();
msgCodec = new GlobalRollbackRequestCodec();
break;
case MessageType.TYPE_GLOBAL_STATUS:
return new GlobalStatusRequestCodec();
msgCodec = new GlobalStatusRequestCodec();
break;
case MessageType.TYPE_GLOBAL_LOCK_QUERY:
return new GlobalLockQueryRequestCodec();
msgCodec = new GlobalLockQueryRequestCodec();
break;
case MessageType.TYPE_BRANCH_REGISTER:
return new BranchRegisterRequestCodec();
msgCodec = new BranchRegisterRequestCodec();
break;
case MessageType.TYPE_BRANCH_STATUS_REPORT:
return new BranchReportRequestCodec();
case MessageType.TYPE_GLOBAL_REPORT:
return new GlobalReportRequestCodec();
default:
throw new IllegalArgumentException("not support typeCode," + typeCode);
}
}

/**
* Gets merge response instance by code.
*
* @param typeCode the type code
* @return the merge response instance by code
*/
protected static MessageSeataCodec getMergeResponseMessageSeataCodec(int typeCode, byte version) {
switch (typeCode) {
msgCodec = new BranchReportRequestCodec();
break;
case MessageType.TYPE_GLOBAL_BEGIN_RESULT:
return new GlobalBeginResponseCodec();
msgCodec = new GlobalBeginResponseCodec();
break;
case MessageType.TYPE_GLOBAL_COMMIT_RESULT:
return new GlobalCommitResponseCodec();
msgCodec = new GlobalCommitResponseCodec();
break;
case MessageType.TYPE_GLOBAL_ROLLBACK_RESULT:
return new GlobalRollbackResponseCodec();
msgCodec = new GlobalRollbackResponseCodec();
break;
case MessageType.TYPE_GLOBAL_STATUS_RESULT:
return new GlobalStatusResponseCodec();
msgCodec = new GlobalStatusResponseCodec();
break;
case MessageType.TYPE_GLOBAL_LOCK_QUERY_RESULT:
return new GlobalLockQueryResponseCodec();
msgCodec = new GlobalLockQueryResponseCodec();
break;
case MessageType.TYPE_BRANCH_REGISTER_RESULT:
return new BranchRegisterResponseCodec();
msgCodec = new BranchRegisterResponseCodec();
break;
case MessageType.TYPE_BRANCH_STATUS_REPORT_RESULT:
return new BranchReportResponseCodec();
msgCodec = new BranchReportResponseCodec();
break;
case MessageType.TYPE_BRANCH_COMMIT_RESULT:
return new BranchCommitResponseCodec();
msgCodec = new BranchCommitResponseCodec();
break;
case MessageType.TYPE_BRANCH_ROLLBACK_RESULT:
return new BranchRollbackResponseCodec();
msgCodec = new BranchRollbackResponseCodec();
break;
case MessageType.TYPE_RM_DELETE_UNDOLOG:
return new UndoLogDeleteRequestCodec();
msgCodec = new UndoLogDeleteRequestCodec();
break;
case MessageType.TYPE_GLOBAL_REPORT_RESULT:
return new GlobalReportResponseCodec();
msgCodec = new GlobalReportResponseCodec();
break;
default:
throw new IllegalArgumentException("not support typeCode," + typeCode);
break;
}

if (msgCodec != null) {
return msgCodec;
}

throw new IllegalArgumentException("not support typeCode," + typeCode);
}

/**
Expand Down Expand Up @@ -268,86 +247,63 @@ public static AbstractMessage getMessage(short typeCode) {
case MessageType.TYPE_BATCH_RESULT_MSG:
abstractMessage = new BatchResultMessage();
break;
default:
break;
}

if (abstractMessage != null) {
return abstractMessage;
}

try {
abstractMessage = getMergeRequestInstanceByCode(typeCode);
} catch (Exception exx) {
}

if (abstractMessage != null) {
return abstractMessage;
}

return getMergeResponseInstanceByCode(typeCode);
}

/**
* Gets merge request instance by code.
*
* @param typeCode the type code
* @return the merge request instance by code
*/
protected static AbstractMessage getMergeRequestInstanceByCode(int typeCode) {
switch (typeCode) {
case MessageType.TYPE_GLOBAL_BEGIN:
return new GlobalBeginRequest();
abstractMessage = new GlobalBeginRequest();
break;
case MessageType.TYPE_GLOBAL_COMMIT:
return new GlobalCommitRequest();
abstractMessage = new GlobalCommitRequest();
break;
case MessageType.TYPE_GLOBAL_ROLLBACK:
return new GlobalRollbackRequest();
abstractMessage = new GlobalRollbackRequest();
break;
case MessageType.TYPE_GLOBAL_STATUS:
return new GlobalStatusRequest();
abstractMessage = new GlobalStatusRequest();
break;
case MessageType.TYPE_GLOBAL_LOCK_QUERY:
return new GlobalLockQueryRequest();
abstractMessage = new GlobalLockQueryRequest();
break;
case MessageType.TYPE_BRANCH_REGISTER:
return new BranchRegisterRequest();
abstractMessage = new BranchRegisterRequest();
break;
case MessageType.TYPE_BRANCH_STATUS_REPORT:
return new BranchReportRequest();
case MessageType.TYPE_GLOBAL_REPORT:
return new GlobalReportRequest();
default:
throw new IllegalArgumentException("not support typeCode," + typeCode);
}
}

/**
* Gets merge response instance by code.
*
* @param typeCode the type code
* @return the merge response instance by code
*/
protected static AbstractMessage getMergeResponseInstanceByCode(int typeCode) {
switch (typeCode) {
abstractMessage = new BranchReportRequest();
break;
case MessageType.TYPE_GLOBAL_BEGIN_RESULT:
return new GlobalBeginResponse();
abstractMessage = new GlobalBeginResponse();
break;
case MessageType.TYPE_GLOBAL_COMMIT_RESULT:
return new GlobalCommitResponse();
abstractMessage = new GlobalCommitResponse();
break;
case MessageType.TYPE_GLOBAL_ROLLBACK_RESULT:
return new GlobalRollbackResponse();
abstractMessage = new GlobalRollbackResponse();
break;
case MessageType.TYPE_GLOBAL_STATUS_RESULT:
return new GlobalStatusResponse();
abstractMessage = new GlobalStatusResponse();
break;
case MessageType.TYPE_GLOBAL_LOCK_QUERY_RESULT:
return new GlobalLockQueryResponse();
abstractMessage = new GlobalLockQueryResponse();
break;
case MessageType.TYPE_BRANCH_REGISTER_RESULT:
return new BranchRegisterResponse();
abstractMessage = new BranchRegisterResponse();
break;
case MessageType.TYPE_BRANCH_STATUS_REPORT_RESULT:
return new BranchReportResponse();
abstractMessage = new BranchReportResponse();
break;
case MessageType.TYPE_BRANCH_COMMIT_RESULT:
return new BranchCommitResponse();
abstractMessage = new BranchCommitResponse();
break;
case MessageType.TYPE_BRANCH_ROLLBACK_RESULT:
return new BranchRollbackResponse();
case MessageType.TYPE_GLOBAL_REPORT_RESULT:
return new GlobalReportResponse();
abstractMessage = new BranchRollbackResponse();
break;
default:
throw new IllegalArgumentException("not support typeCode," + typeCode);
break;
}

if (abstractMessage != null) {
return abstractMessage;
}

throw new IllegalArgumentException("not support typeCode," + typeCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ private static <T> T deserializeByVersion(byte[] bytes, byte version) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
//typecode
short typecode = byteBuffer.getShort();
//msg body
byte[] body = new byte[byteBuffer.remaining()];
byteBuffer.get(body);
ByteBuffer in = ByteBuffer.wrap(body);
//new Messgae
ByteBuffer in = byteBuffer.slice();
//new message
AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode);
//get messageCodec
MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode, version);
Expand Down

0 comments on commit c1515ac

Please sign in to comment.