From f73b5595633d828823cdbe263e56f9c931c6c817 Mon Sep 17 00:00:00 2001 From: xlf <1396510465@qq.com> Date: Mon, 23 Sep 2024 16:11:15 +0800 Subject: [PATCH 1/2] Optimize Map2Kvs functions by pre-allocating memory --- client/dtmgrpc/dtmgimp/utils.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/dtmgrpc/dtmgimp/utils.go b/client/dtmgrpc/dtmgimp/utils.go index 2bb6ba5..a41aa40 100644 --- a/client/dtmgrpc/dtmgimp/utils.go +++ b/client/dtmgrpc/dtmgimp/utils.go @@ -7,14 +7,14 @@ package dtmgimp import ( - context "context" + "context" "github.com/dtm-labs/dtm/client/dtmcli/dtmimp" "github.com/dtm-labs/dtm/client/dtmgrpc/dtmgpb" "github.com/dtm-labs/logger" "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" - emptypb "google.golang.org/protobuf/types/known/emptypb" + "google.golang.org/protobuf/types/known/emptypb" ) // MustProtoMarshal must version of proto.Marshal @@ -77,7 +77,7 @@ func TransInfo2Ctx(ctx context.Context, gid, transType, branchID, op, dtm string // Map2Kvs map to metadata kv func Map2Kvs(m map[string]string) []string { - kvs := []string{} + kvs := make([]string, 0, len(m)<<1) for k, v := range m { kvs = append(kvs, k, v) } From d34bbd1ba487fad0f8b99ca0a54a8fea065f4597 Mon Sep 17 00:00:00 2001 From: xlf <1396510465@qq.com> Date: Tue, 19 Nov 2024 15:05:56 +0800 Subject: [PATCH 2/2] update:use len(m) * 2 because it is more readable. --- client/dtmgrpc/dtmgimp/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/dtmgrpc/dtmgimp/utils.go b/client/dtmgrpc/dtmgimp/utils.go index a41aa40..ca7af52 100644 --- a/client/dtmgrpc/dtmgimp/utils.go +++ b/client/dtmgrpc/dtmgimp/utils.go @@ -77,7 +77,7 @@ func TransInfo2Ctx(ctx context.Context, gid, transType, branchID, op, dtm string // Map2Kvs map to metadata kv func Map2Kvs(m map[string]string) []string { - kvs := make([]string, 0, len(m)<<1) + kvs := make([]string, 0, len(m)*2) for k, v := range m { kvs = append(kvs, k, v) }