Skip to content

Commit

Permalink
*: memory is not allocated first if the user does not use some inform…
Browse files Browse the repository at this point in the history
…ation from the table. (pingcap#50062)

close pingcap#50077
  • Loading branch information
zimulala authored and AilinKid committed Jan 17, 2024
1 parent 962dd2e commit 14b58e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
5 changes: 0 additions & 5 deletions pkg/ddl/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func TestAlterAddConstraintStateChange(t *testing.T) {
tableCommon, ok := constraintTable.(*tables.TableCommon)
require.True(t, ok)
originCons := tableCommon.Constraints
tableCommon.WritableConstraints = []*table.Constraint{}
tableCommon.Constraints = []*table.Constraint{}
// insert data
tk1.MustExec("insert into t values(1)")
Expand Down Expand Up @@ -146,7 +145,6 @@ func TestAlterAddConstraintStateChange1(t *testing.T) {
tableCommon, ok := constraintTable.(*tables.TableCommon)
require.True(t, ok)
originCons := tableCommon.Constraints
tableCommon.WritableConstraints = []*table.Constraint{}
tableCommon.Constraints = []*table.Constraint{}
// insert data
tk1.MustExec("insert into t values(1)")
Expand Down Expand Up @@ -190,7 +188,6 @@ func TestAlterAddConstraintStateChange2(t *testing.T) {
tableCommon, ok := constraintTable.(*tables.TableCommon)
require.True(t, ok)
tableCommon.Constraints[0].State = model.StateWriteOnly
tableCommon.WritableConstraints = []*table.Constraint{}
// insert data
tk1.MustGetErrMsg("insert into t values(1)", "[table:3819]Check constraint 'c2' is violated.")
// recover
Expand Down Expand Up @@ -233,7 +230,6 @@ func TestAlterAddConstraintStateChange3(t *testing.T) {
tableCommon, ok := constraintTable.(*tables.TableCommon)
require.True(t, ok)
tableCommon.Constraints[0].State = model.StateWriteReorganization
tableCommon.WritableConstraints = []*table.Constraint{}
// insert data
tk1.MustGetErrMsg("insert into t values(1)", "[table:3819]Check constraint 'c3' is violated.")
// recover
Expand Down Expand Up @@ -277,7 +273,6 @@ func TestAlterEnforcedConstraintStateChange(t *testing.T) {
tableCommon, ok := constraintTable.(*tables.TableCommon)
require.True(t, ok)
tableCommon.Constraints[0].State = model.StateWriteOnly
tableCommon.WritableConstraints = []*table.Constraint{}
// insert data
tk1.MustGetErrMsg("insert into t values(1)", "[table:3819]Check constraint 'c1' is violated.")
// recover
Expand Down
42 changes: 18 additions & 24 deletions pkg/table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ type TableCommon struct {
// physicalTableID is a unique int64 to identify a physical table.
physicalTableID int64
Columns []*table.Column
PublicColumns []*table.Column
VisibleColumns []*table.Column
HiddenColumns []*table.Column
WritableColumns []*table.Column
FullHiddenColsAndVisibleColumns []*table.Column
publicColumns []*table.Column
visibleColumns []*table.Column
hiddenColumns []*table.Column
writableColumns []*table.Column
fullHiddenColsAndVisibleColumns []*table.Column
indices []table.Index
meta *model.TableInfo
allocs autoid.Allocators
sequence *sequenceCommon
dependencyColumnOffsets []int
Constraints []*table.Constraint
WritableConstraints []*table.Constraint
writableConstraints []*table.Constraint

// recordPrefix and indexPrefix are generated using physicalTableID.
recordPrefix kv.Key
Expand Down Expand Up @@ -203,13 +203,7 @@ func initTableCommon(t *TableCommon, tblInfo *model.TableInfo, physicalTableID i
t.allocs = allocs
t.meta = tblInfo
t.Columns = cols
t.PublicColumns = t.Cols()
t.VisibleColumns = t.VisibleCols()
t.HiddenColumns = t.HiddenCols()
t.WritableColumns = t.WritableCols()
t.FullHiddenColsAndVisibleColumns = t.FullHiddenColsAndVisibleCols()
t.Constraints = constraints
t.WritableConstraints = t.WritableConstraint()
t.recordPrefix = tablecodec.GenTableRecordPrefix(physicalTableID)
t.indexPrefix = tablecodec.GenTableIndexPrefix(physicalTableID)
if tblInfo.IsSequence() {
Expand Down Expand Up @@ -306,32 +300,32 @@ func (t *TableCommon) getCols(mode getColsMode) []*table.Column {

// Cols implements table.Table Cols interface.
func (t *TableCommon) Cols() []*table.Column {
if len(t.PublicColumns) > 0 {
return t.PublicColumns
if len(t.publicColumns) > 0 {
return t.publicColumns
}
return t.getCols(full)
}

// VisibleCols implements table.Table VisibleCols interface.
func (t *TableCommon) VisibleCols() []*table.Column {
if len(t.VisibleColumns) > 0 {
return t.VisibleColumns
if len(t.visibleColumns) > 0 {
return t.visibleColumns
}
return t.getCols(visible)
}

// HiddenCols implements table.Table HiddenCols interface.
func (t *TableCommon) HiddenCols() []*table.Column {
if len(t.HiddenColumns) > 0 {
return t.HiddenColumns
if len(t.hiddenColumns) > 0 {
return t.hiddenColumns
}
return t.getCols(hidden)
}

// WritableCols implements table WritableCols interface.
func (t *TableCommon) WritableCols() []*table.Column {
if len(t.WritableColumns) > 0 {
return t.WritableColumns
if len(t.writableColumns) > 0 {
return t.writableColumns
}
writableColumns := make([]*table.Column, 0, len(t.Columns))
for _, col := range t.Columns {
Expand All @@ -350,8 +344,8 @@ func (t *TableCommon) DeletableCols() []*table.Column {

// WritableConstraint returns constraints of the table in writable states.
func (t *TableCommon) WritableConstraint() []*table.Constraint {
if len(t.WritableConstraints) > 0 {
return t.WritableConstraints
if len(t.writableConstraints) > 0 {
return t.writableConstraints
}
if t.Constraints == nil {
return nil
Expand Down Expand Up @@ -385,8 +379,8 @@ func (t *TableCommon) CheckRowConstraint(sctx sessionctx.Context, rowToCheck []t

// FullHiddenColsAndVisibleCols implements table FullHiddenColsAndVisibleCols interface.
func (t *TableCommon) FullHiddenColsAndVisibleCols() []*table.Column {
if len(t.FullHiddenColsAndVisibleColumns) > 0 {
return t.FullHiddenColsAndVisibleColumns
if len(t.fullHiddenColsAndVisibleColumns) > 0 {
return t.fullHiddenColsAndVisibleColumns
}

cols := make([]*table.Column, 0, len(t.Columns))
Expand Down
6 changes: 0 additions & 6 deletions pkg/table/tables/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,6 @@ func TestHiddenColumn(t *testing.T) {
colInfo[1].Hidden = true
colInfo[3].Hidden = true
colInfo[5].Hidden = true
tc := tb.(*tables.TableCommon)
// Reset related caches
tc.VisibleColumns = nil
tc.WritableColumns = nil
tc.HiddenColumns = nil
tc.FullHiddenColsAndVisibleColumns = nil

// Basic test
cols := tb.VisibleCols()
Expand Down
2 changes: 1 addition & 1 deletion pkg/tablecodec/tablecodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ func decodeIntHandleInIndexValue(data []byte) kv.Handle {

// EncodeTableIndexPrefix encodes index prefix with tableID and idxID.
func EncodeTableIndexPrefix(tableID, idxID int64) kv.Key {
key := make([]byte, 0, prefixLen)
key := make([]byte, 0, prefixLen+idLen)
key = appendTableIndexPrefix(key, tableID)
key = codec.EncodeInt(key, idxID)
return key
Expand Down

0 comments on commit 14b58e6

Please sign in to comment.