Skip to content

Commit

Permalink
statistics: avoid copy twice when to decode the topn (#47563)
Browse files Browse the repository at this point in the history
ref #47275
  • Loading branch information
hawkingrei authored Oct 12, 2023
1 parent 9353c68 commit 6842efd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions statistics/cmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion statistics/handle/storage/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6842efd

Please sign in to comment.