Skip to content

Commit

Permalink
*: use generics to improve Clamp (#34586)
Browse files Browse the repository at this point in the history
close #34588
  • Loading branch information
hawkingrei authored May 13, 2022
1 parent 5b6bb13 commit 19b8565
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 40 deletions.
3 changes: 2 additions & 1 deletion br/pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/br/pkg/version"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util/mathutil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/multierr"
Expand Down Expand Up @@ -561,7 +562,7 @@ func RunRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf
}

// Restore sst files in batch.
batchSize := utils.ClampInt(int(cfg.Concurrency), defaultRestoreConcurrency, maxRestoreBatchSizeLimit)
batchSize := mathutil.Clamp(int(cfg.Concurrency), defaultRestoreConcurrency, maxRestoreBatchSizeLimit)
failpoint.Inject("small-batch-size", func(v failpoint.Value) {
log.Info("failpoint small batch size is on", zap.Int("size", v.(int)))
batchSize = v.(int)
Expand Down
15 changes: 0 additions & 15 deletions br/pkg/utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@

package utils

import (
"github.com/pingcap/log"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
)

// ClampInt restrict a value to a certain interval.
func ClampInt(n, min, max int) int {
if min > max {
log.Error("clamping integer with min > max", zap.Int("min", min), zap.Int("max", max))
}

return mathutil.Min(max, mathutil.Max(min, n))
}

// NextPowerOfTwo returns the smallest power of two greater than or equal to `i`
// Caller should guarantee that i > 0 and the return value is not overflow.
func NextPowerOfTwo(i int64) int64 {
Expand Down
8 changes: 0 additions & 8 deletions br/pkg/utils/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestClampInt(t *testing.T) {
require.Equal(t, 3, ClampInt(100, 1, 3))
require.Equal(t, 2, ClampInt(2, 1, 3))
require.Equal(t, 1, ClampInt(0, 1, 3))
require.Equal(t, 1, ClampInt(0, 1, 1))
require.Equal(t, 1, ClampInt(100, 1, 1))
}

func TestNextPowerOfTwo(t *testing.T) {
require.Equal(t, int64(1), NextPowerOfTwo(1))
require.Equal(t, int64(4), NextPowerOfTwo(3))
Expand Down
21 changes: 5 additions & 16 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/ranger"
"github.com/pingcap/tipb/go-tipb"
"github.com/twmb/murmur3"
Expand Down Expand Up @@ -1278,7 +1279,7 @@ func (c *Column) GetColumnRowCount(sctx sessionctx.Context, ranges []*ranger.Ran
return 0, errors.Trace(err)
}
cnt -= lowCnt
cnt = clampRowCount(cnt, c.notNullCount())
cnt = mathutil.Clamp(cnt, 0, c.notNullCount())
}
if !rg.LowExclude && lowVal.IsNull() {
cnt += float64(c.NullCount)
Expand All @@ -1291,7 +1292,7 @@ func (c *Column) GetColumnRowCount(sctx sessionctx.Context, ranges []*ranger.Ran
cnt += highCnt
}

cnt = clampRowCount(cnt, c.TotalRowCount())
cnt = mathutil.Clamp(cnt, 0, c.TotalRowCount())

// If the current table row count has changed, we should scale the row count accordingly.
cnt *= c.GetIncreaseFactor(realtimeRowCount)
Expand All @@ -1307,8 +1308,7 @@ func (c *Column) GetColumnRowCount(sctx sessionctx.Context, ranges []*ranger.Ran

rowCount += cnt
}

rowCount = clampRowCount(rowCount, float64(realtimeRowCount))
rowCount = mathutil.Clamp(rowCount, 0, float64(realtimeRowCount))
return rowCount, nil
}

Expand Down Expand Up @@ -1525,8 +1525,7 @@ func (idx *Index) GetRowCount(sctx sessionctx.Context, coll *HistColl, indexRang
totalCount += idx.Histogram.outOfRangeRowCount(&l, &r, increaseCount)
}
}

totalCount = clampRowCount(totalCount, float64(realtimeRowCount))
totalCount = mathutil.Clamp(totalCount, 0, float64(realtimeRowCount))
return totalCount, nil
}

Expand Down Expand Up @@ -1781,16 +1780,6 @@ func (idx *Index) outOfRange(val types.Datum) bool {
return true
}

func clampRowCount(rowCount, upperLimit float64) (clampedRowCount float64) {
if rowCount > upperLimit {
return upperLimit
}
if rowCount < 0 {
return 0
}
return rowCount
}

// matchPrefix checks whether ad is the prefix of value
func matchPrefix(row chunk.Row, colIdx int, ad *types.Datum) bool {
switch ad.Kind() {
Expand Down
10 changes: 10 additions & 0 deletions util/mathutil/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ func Min[v constraints.Ordered](x v, xs ...v) v {
}
return min
}

// Clamp restrict a value to a certain interval.
func Clamp[v constraints.Integer | constraints.Float](n, min, max v) v {
if n >= max {
return max
} else if n <= min {
return min
}
return n
}
8 changes: 8 additions & 0 deletions util/mathutil/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func TestMaxMin(t *testing.T) {
require.Equal(t, "xy", Max("ab", "xy"))
require.Equal(t, "ab", Max("ab", "ab"))
}

func TestClamp(t *testing.T) {
require.Equal(t, 3, Clamp(100, 1, 3))
require.Equal(t, 2.0, Clamp(float64(2), 1.0, 3.0))
require.Equal(t, float32(1.0), Clamp(float32(0), 1.0, 3.0))
require.Equal(t, 1, Clamp(0, 1, 1))
require.Equal(t, 1, Clamp(100, 1, 1))
}

0 comments on commit 19b8565

Please sign in to comment.