You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by ma...@apache.org on 2021/04/22 13:08:16 UTC

[rocketmq-client-go] branch master updated: feat(coded): improve decoding performance by using io.ReadFull while reading byte slices

This is an automated email from the ASF dual-hosted git repository.

maixiaohai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 745ac16  feat(coded): improve decoding performance by using io.ReadFull while reading byte slices
     new dfa26d1  Merge pull request #636 from czah/remoting-cmd-decoding-optimization
745ac16 is described below

commit 745ac1659cf629f56f79d6a692d3ea21901197c8
Author: czah <cz...@users.noreply.github.com>
AuthorDate: Mon Mar 29 13:43:48 2021 +0800

    feat(coded): improve decoding performance by using io.ReadFull while reading byte slices
---
 internal/remote/codec.go | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/internal/remote/codec.go b/internal/remote/codec.go
index 1c6e0a5..6b3d3d6 100644
--- a/internal/remote/codec.go
+++ b/internal/remote/codec.go
@@ -215,7 +215,7 @@ func encode(command *RemotingCommand) ([]byte, error) {
 }
 
 func decode(data []byte) (*RemotingCommand, error) {
-	buf := bytes.NewBuffer(data)
+	buf := bytes.NewReader(data)
 	length := int32(len(data))
 	var oriHeaderLen int32
 	err := binary.Read(buf, binary.BigEndian, &oriHeaderLen)
@@ -225,8 +225,7 @@ func decode(data []byte) (*RemotingCommand, error) {
 
 	headerLength := oriHeaderLen & 0xFFFFFF
 	headerData := make([]byte, headerLength)
-	err = binary.Read(buf, binary.BigEndian, &headerData)
-	if err != nil {
+	if _, err = io.ReadFull(buf, headerData); err != nil {
 		return nil, err
 	}
 
@@ -246,8 +245,7 @@ func decode(data []byte) (*RemotingCommand, error) {
 	bodyLength := length - 4 - headerLength
 	if bodyLength > 0 {
 		bodyData := make([]byte, bodyLength)
-		err = binary.Read(buf, binary.BigEndian, &bodyData)
-		if err != nil {
+		if _, err = io.ReadFull(buf, bodyData); err != nil {
 			return nil, err
 		}
 		command.Body = bodyData
@@ -463,8 +461,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
 
 	if remarkLen > 0 {
 		var remarkData = make([]byte, remarkLen)
-		err = binary.Read(buf, binary.BigEndian, &remarkData)
-		if err != nil {
+		if _, err = io.ReadFull(buf, remarkData); err != nil {
 			return nil, err
 		}
 		command.Remark = string(remarkData)
@@ -477,8 +474,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
 
 	if extFieldsLen > 0 {
 		extFieldsData := make([]byte, extFieldsLen)
-		err = binary.Read(buf, binary.BigEndian, &extFieldsData)
-		if err != nil {
+		if _, err := io.ReadFull(buf, extFieldsData); err != nil {
 			return nil, err
 		}
 
@@ -515,10 +511,9 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
 	return command, nil
 }
 
-func getExtFieldsData(buff *bytes.Buffer, length int32) (string, error) {
+func getExtFieldsData(buff io.Reader, length int32) (string, error) {
 	var data = make([]byte, length)
-	err := binary.Read(buff, binary.BigEndian, &data)
-	if err != nil {
+	if _, err := io.ReadFull(buff, data); err != nil {
 		return "", err
 	}