From 6842efdb98eb91077d849f0c3f18df06968a81ff Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 12 Oct 2023 20:01:57 +0800 Subject: [PATCH] statistics: avoid copy twice when to decode the topn (#47563) ref pingcap/tidb#47275 --- statistics/cmsketch.go | 12 +++++------- statistics/handle/storage/read.go | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/statistics/cmsketch.go b/statistics/cmsketch.go index 1ff5868803..4662618717 100644 --- a/statistics/cmsketch.go +++ b/statistics/cmsketch.go @@ -463,17 +463,15 @@ func DecodeCMSketchAndTopN(data []byte, topNRows []chunk.Row) (*CMSketch, *TopN, } // DecodeTopN decodes a TopN from the given byte slice. -func DecodeTopN(topNRows []chunk.Row) (*TopN, error) { - pbTopN := make([]*tipb.CMSketchTopN, 0, len(topNRows)) +func DecodeTopN(topNRows []chunk.Row) *TopN { + topN := NewTopN(len(topNRows)) for _, row := range topNRows { data := make([]byte, len(row.GetBytes(0))) copy(data, row.GetBytes(0)) - pbTopN = append(pbTopN, &tipb.CMSketchTopN{ - Data: data, - Count: row.GetUint64(1), - }) + topN.AppendTopN(data, row.GetUint64(1)) } - return TopNFromProto(pbTopN), nil + topN.Sort() + return topN } // DecodeCMSketch encodes the given CMSketch to byte slice. diff --git a/statistics/handle/storage/read.go b/statistics/handle/storage/read.go index f84fc1d1e9..eb8605a7b4 100644 --- a/statistics/handle/storage/read.go +++ b/statistics/handle/storage/read.go @@ -134,7 +134,7 @@ func TopNFromStorage(sctx sessionctx.Context, tblID int64, isIndex int, histID i if err != nil || len(rows) == 0 { return nil, err } - return statistics.DecodeTopN(rows) + return statistics.DecodeTopN(rows), nil } // FMSketchFromStorage reads FMSketch from storage