Skip to content

Commit

Permalink
opt: memorize TableCommon.getCols
Browse files Browse the repository at this point in the history
Signed-off-by: ekexium <[email protected]>
  • Loading branch information
ekexium committed May 28, 2024
1 parent df64c34 commit 9bad6ad
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
39 changes: 39 additions & 0 deletions pkg/table/tables/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tables_test

import (
"context"
"testing"

"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/sessiontxn"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/types"
_ "github.com/pingcap/tidb/pkg/util/context"
"github.com/stretchr/testify/require"
)

func BenchmarkAddRecordInPipelinedDML(b *testing.B) {
store, dom := testkit.CreateMockStoreAndDomain(b)
tk := testkit.NewTestKit(b, store)
_, err := tk.Session().Execute(context.Background(), "CREATE TABLE test.t (a int primary key auto_increment, b varchar(255))")
require.NoError(b, err)
ctx := tk.Session()
vars := ctx.GetSessionVars()
vars.BulkDMLEnabled = true
vars.TxnCtx.EnableMDL = true
vars.StmtCtx.InInsertStmt = true
require.Nil(b, sessiontxn.NewTxn(context.Background(), tk.Session()))
txn, _ := ctx.Txn(true)
require.True(b, txn.IsPipelined())
tb, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(b, err)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := tb.AddRecord(ctx.GetTableCtx(), types.MakeDatums(i, "test"))
if err != nil {
b.Fatal(err)
}
}
}
33 changes: 25 additions & 8 deletions pkg/table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type TableCommon struct {
// recordPrefix and indexPrefix are generated using physicalTableID.
recordPrefix kv.Key
indexPrefix kv.Key

// avoid duplicated calculations of Cols
// mode -> cols
getColsCache [][]*table.Column
}

// MockTableFromMeta only serves for test.
Expand Down Expand Up @@ -282,20 +286,33 @@ const (
visible
hidden
full
// numMode is only used to get the total of modes
numMode
)

func (t *TableCommon) getCols(mode getColsMode) []*table.Column {
columns := make([]*table.Column, 0, len(t.Columns))
for _, col := range t.Columns {
if col.State != model.StatePublic {
continue
if t.getColsCache == nil {
t.getColsCache = make([][]*table.Column, numMode)
for i := range t.getColsCache {
t.getColsCache[i] = nil
}
if (mode == visible && col.Hidden) || (mode == hidden && !col.Hidden) {
continue
}
if t.getColsCache[mode] != nil {
return t.getColsCache[mode]
} else {
columns := make([]*table.Column, 0, len(t.Columns))
for _, col := range t.Columns {
if col.State != model.StatePublic {
continue
}
if (mode == visible && col.Hidden) || (mode == hidden && !col.Hidden) {
continue
}
columns = append(columns, col)
}
columns = append(columns, col)
t.getColsCache[mode] = columns
return columns
}
return columns
}

// Cols implements table.Table Cols interface.
Expand Down

0 comments on commit 9bad6ad

Please sign in to comment.