Skip to content

Commit

Permalink
statistics: add branch check for BinarySearchRemoveVal (#47562)
Browse files Browse the repository at this point in the history
ref #47275
  • Loading branch information
hawkingrei authored Oct 13, 2023
1 parent 318e82b commit e146d4d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ func (hg *Histogram) BucketToString(bktID, idxCols int) string {
// BinarySearchRemoveVal removes the value from the TopN using binary search.
func (hg *Histogram) BinarySearchRemoveVal(valCntPairs TopNMeta) {
lowIdx, highIdx := 0, hg.Len()-1
// if hg is too small, we don't need to check the branch. because the cost is more than binary search.
if hg.Len() > 4 {
if cmpResult := bytes.Compare(hg.Bounds.Column(0).GetRaw(highIdx*2+1), valCntPairs.Encoded); cmpResult < 0 {
return
}
if cmpResult := bytes.Compare(hg.Bounds.Column(0).GetRaw(lowIdx), valCntPairs.Encoded); cmpResult > 0 {
return
}
}
for lowIdx <= highIdx {
midIdx := (lowIdx + highIdx) / 2
cmpResult := bytes.Compare(hg.Bounds.Column(0).GetRaw(midIdx*2), valCntPairs.Encoded)
Expand Down

0 comments on commit e146d4d

Please sign in to comment.