Skip to content

Commit

Permalink
statistics: avoid unnecessary copy at CMSketchAndTopNFromProto (#47598)
Browse files Browse the repository at this point in the history
ref #47275
  • Loading branch information
hawkingrei authored Oct 17, 2023
1 parent 59258d9 commit f69f37a
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions pkg/statistics/cmsketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,30 +440,21 @@ func DecodeCMSketchAndTopN(data []byte, topNRows []chunk.Row) (*CMSketch, *TopN,
if data == nil && len(topNRows) == 0 {
return nil, nil, nil
}
pbTopN := make([]*tipb.CMSketchTopN, 0, 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),
})
}
if len(data) == 0 {
return nil, TopNFromProto(pbTopN), nil
return nil, DecodeTopN(topNRows), nil
}
p := &tipb.CMSketch{}
err := p.Unmarshal(data)
cm, err := DecodeCMSketch(data)
if err != nil {
return nil, nil, errors.Trace(err)
}
p.TopN = pbTopN
cm, topN := CMSketchAndTopNFromProto(p)
return cm, topN, nil
return cm, DecodeTopN(topNRows), nil
}

// DecodeTopN decodes a TopN from the given byte slice.
func DecodeTopN(topNRows []chunk.Row) *TopN {
if len(topNRows) == 0 {
return nil
}
topN := NewTopN(len(topNRows))
for _, row := range topNRows {
data := make([]byte, len(row.GetBytes(0)))
Expand Down

0 comments on commit f69f37a

Please sign in to comment.