From 1dec0c787f9ea2ab62d89bd67b61731bf3406fa9 Mon Sep 17 00:00:00 2001 From: crazycs Date: Mon, 15 Oct 2018 13:06:19 +0800 Subject: [PATCH] admin: remove reflect deepEqual --- types/datum_test.go | 38 ++++++++++++++++++++++++++++++++++++++ util/admin/admin.go | 17 +++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/types/datum_test.go b/types/datum_test.go index bbb66780aae3f..f2317a77792a6 100644 --- a/types/datum_test.go +++ b/types/datum_test.go @@ -14,6 +14,8 @@ package types import ( + "reflect" + "testing" "time" . "github.com/pingcap/check" @@ -362,3 +364,39 @@ func (ts *testDatumSuite) TestCopyDatum(c *C) { } } } + +func prepareCompareDatums() ([]Datum, []Datum) { + vals := make([]Datum, 0, 5) + vals = append(vals, NewIntDatum(1)) + vals = append(vals, NewFloat64Datum(1.23)) + vals = append(vals, NewStringDatum("abcde")) + vals = append(vals, NewDecimalDatum(NewDecFromStringForTest("1.2345"))) + vals = append(vals, NewTimeDatum(Time{Time: FromGoTime(time.Date(2018, 3, 8, 16, 1, 0, 315313000, time.UTC)), Fsp: 6, Type: mysql.TypeTimestamp})) + + vals1 := make([]Datum, 0, 5) + vals1 = append(vals1, NewIntDatum(1)) + vals1 = append(vals1, NewFloat64Datum(1.23)) + vals1 = append(vals1, NewStringDatum("abcde")) + vals1 = append(vals1, NewDecimalDatum(NewDecFromStringForTest("1.2345"))) + vals1 = append(vals1, NewTimeDatum(Time{Time: FromGoTime(time.Date(2018, 3, 8, 16, 1, 0, 315313000, time.UTC)), Fsp: 6, Type: mysql.TypeTimestamp})) + return vals, vals1 +} + +func BenchmarkCompareDatum(b *testing.B) { + vals, vals1 := prepareCompareDatums() + sc := new(stmtctx.StatementContext) + b.ResetTimer() + for i := 0; i < b.N; i++ { + for j, v := range vals { + v.CompareDatum(sc, &vals1[j]) + } + } +} + +func BenchmarkCompareDatumByReflect(b *testing.B) { + vals, vals1 := prepareCompareDatums() + b.ResetTimer() + for i := 0; i < b.N; i++ { + reflect.DeepEqual(vals, vals1) + } +} diff --git a/util/admin/admin.go b/util/admin/admin.go index 98ae13811badd..b536c13f95493 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -16,7 +16,6 @@ package admin import ( "fmt" "io" - "reflect" "sort" "github.com/pingcap/tidb/expression" @@ -391,16 +390,9 @@ func compareDatumSlice(sc *stmtctx.StatementContext, val1s, val2s []types.Datum) return false } for i, v := range val1s { - switch v.Kind() { - case types.KindMysqlDecimal, types.KindBytes: - res, err := v.CompareDatum(sc, &val2s[i]) - if err != nil || res != 0 { - return false - } - default: - if !reflect.DeepEqual(v, val2s[i]) { - return false - } + res, err := v.CompareDatum(sc, &val2s[i]) + if err != nil || res != 0 { + return false } } return true @@ -524,6 +516,7 @@ func CompareTableRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table. } startKey := t.RecordKey(0) + sc := new(stmtctx.StatementContext) filterFunc := func(h int64, vals []types.Datum, cols []*table.Column) (bool, error) { vals2, ok := m[h] if !ok { @@ -535,7 +528,7 @@ func CompareTableRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table. return true, nil } - if !reflect.DeepEqual(vals, vals2) { + if !compareDatumSlice(sc, vals, vals2) { record1 := &RecordData{Handle: h, Values: vals2} record2 := &RecordData{Handle: h, Values: vals} return false, ErrDataInConsistent.GenWithStack("data:%#v != record:%#v", record1, record2)