From 634602ff355a01ab4fa2a8db81fe52d8bab3244b Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 1 Feb 2024 18:55:55 +0800 Subject: [PATCH] *: memory is not allocated first if the user does not use some information from the table. (#50062) (#50169) close pingcap/tidb#50077 --- pkg/ddl/constraint_test.go | 5 ---- pkg/table/tables/tables.go | 42 ++++++++++++++------------------- pkg/table/tables/tables_test.go | 6 ----- pkg/tablecodec/tablecodec.go | 2 +- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/pkg/ddl/constraint_test.go b/pkg/ddl/constraint_test.go index c1837bcd54d6b..b603876ce3638 100644 --- a/pkg/ddl/constraint_test.go +++ b/pkg/ddl/constraint_test.go @@ -96,7 +96,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)") @@ -145,7 +144,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)") @@ -189,7 +187,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 @@ -232,7 +229,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 @@ -274,7 +270,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 diff --git a/pkg/table/tables/tables.go b/pkg/table/tables/tables.go index c54fb3f35fbe0..de3ca4d1e78e5 100644 --- a/pkg/table/tables/tables.go +++ b/pkg/table/tables/tables.go @@ -65,18 +65,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 @@ -184,13 +184,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() { @@ -287,32 +281,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 { @@ -331,8 +325,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 @@ -366,8 +360,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)) diff --git a/pkg/table/tables/tables_test.go b/pkg/table/tables/tables_test.go index 76561f3feca79..88f19b2e4b1f2 100644 --- a/pkg/table/tables/tables_test.go +++ b/pkg/table/tables/tables_test.go @@ -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() diff --git a/pkg/tablecodec/tablecodec.go b/pkg/tablecodec/tablecodec.go index 723426d7356bd..d374c2edd851e 100644 --- a/pkg/tablecodec/tablecodec.go +++ b/pkg/tablecodec/tablecodec.go @@ -992,7 +992,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