Skip to content

Commit

Permalink
*: reduce runtime.growslice by preallocation (pingcap#9946)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiekeyi98 authored and lzmhhh123 committed Mar 31, 2019
1 parent c612252 commit e31e8f1
Show file tree
Hide file tree
Showing 16 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ func (s *testColumnSuite) colDefStrToFieldType(c *C, str string) *types.FieldTyp
func (s *testColumnSuite) TestFieldCase(c *C) {
var fields = []string{"field", "Field"}
var colDefs = make([]*ast.ColumnDef, len(fields))
var colObjects []interface{}
colObjects := make([]interface{}, 0, len(fields))
for i, name := range fields {
colDefs[i] = &ast.ColumnDef{
Name: &ast.ColumnName{
Expand Down
6 changes: 3 additions & 3 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func setColumnFlagWithConstraint(colMap map[string]*table.Column, v *ast.Constra

func buildColumnsAndConstraints(ctx sessionctx.Context, colDefs []*ast.ColumnDef,
constraints []*ast.Constraint, tblCharset, dbCharset string) ([]*table.Column, []*ast.Constraint, error) {
var cols []*table.Column
colMap := map[string]*table.Column{}
// outPriKeyConstraint is the primary key constraint out of column definition. such as: create table t1 (id int , age int, primary key(id));
var outPriKeyConstraint *ast.Constraint
Expand All @@ -183,6 +182,7 @@ func buildColumnsAndConstraints(ctx sessionctx.Context, colDefs []*ast.ColumnDef
break
}
}
cols := make([]*table.Column, 0, len(colDefs))
for i, colDef := range colDefs {
col, cts, err := buildColumnAndConstraint(ctx, i, colDef, outPriKeyConstraint, tblCharset, dbCharset)
if err != nil {
Expand Down Expand Up @@ -1061,7 +1061,7 @@ func BuildTableInfoFromAST(s *ast.CreateTableStmt) (*model.TableInfo, error) {
func buildTableInfoWithCheck(ctx sessionctx.Context, d *ddl, s *ast.CreateTableStmt, dbCharset string) (*model.TableInfo, error) {
ident := ast.Ident{Schema: s.Table.Schema, Name: s.Table.Name}
colDefs := s.Cols
var colObjects []interface{}
colObjects := make([]interface{}, 0, len(colDefs))
for _, col := range colDefs {
colObjects = append(colObjects, col)
}
Expand Down Expand Up @@ -1245,7 +1245,7 @@ func (d *ddl) CreateView(ctx sessionctx.Context, s *ast.CreateViewStmt) (err err
}
viewInfo, cols := buildViewInfoWithTableColumns(ctx, s)

var colObjects []interface{}
colObjects := make([]interface{}, 0, len(viewInfo.Cols))
for _, col := range viewInfo.Cols {
colObjects = append(colObjects, col)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,7 @@ func (b *executorBuilder) buildWindow(v *plannercore.PhysicalWindow) *WindowExec
return nil
}
base := newBaseExecutor(b.ctx, v.Schema(), v.ExplainID(), childExec)
var groupByItems []expression.Expression
groupByItems := make([]expression.Expression, 0, len(v.PartitionBy))
for _, item := range v.PartitionBy {
groupByItems = append(groupByItems, item.Col)
}
Expand Down
4 changes: 1 addition & 3 deletions executor/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,12 @@ func newChecksumContext(db *model.DBInfo, table *model.TableInfo, startTs uint64
}

func (c *checksumContext) BuildRequests(ctx sessionctx.Context) ([]*kv.Request, error) {
var reqs []*kv.Request

reqs := make([]*kv.Request, 0, len(c.TableInfo.Indices)+1)
req, err := c.buildTableRequest(ctx)
if err != nil {
return nil, errors.Trace(err)
}
reqs = append(reqs, req)

for _, indexInfo := range c.TableInfo.Indices {
if indexInfo.State != model.StatePublic {
continue
Expand Down
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (e *ShowExec) fetchShowTables() error {
return ErrBadDB.GenWithStackByArgs(e.DBName)
}
// sort for tables
var tableNames []string
tableNames := make([]string, 0, len(e.is.SchemaTables(e.DBName)))
var tableTypes = make(map[string]string)
for _, v := range e.is.SchemaTables(e.DBName) {
// Test with mysql.AllPrivMask means any privilege would be OK.
Expand Down
6 changes: 3 additions & 3 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum {
}
}

var records [][]types.Datum
pl := sm.ShowProcessList()
records := make([][]types.Datum, 0, len(pl))
for _, pi := range pl {
// If you have the PROCESS privilege, you can see all threads.
// Otherwise, you can see only your own threads.
Expand Down Expand Up @@ -704,7 +704,7 @@ var filesCols = []columnInfo{

func dataForSchemata(schemas []*model.DBInfo) [][]types.Datum {

var rows [][]types.Datum
rows := make([][]types.Datum, 0, len(schemas))

for _, schema := range schemas {

Expand Down Expand Up @@ -1087,7 +1087,7 @@ func dataForColumns(ctx sessionctx.Context, schemas []*model.DBInfo) [][]types.D
}

func dataForColumnsInTable(schema *model.DBInfo, tbl *model.TableInfo) [][]types.Datum {
var rows [][]types.Datum
rows := make([][]types.Datum, 0, len(tbl.Columns))
for i, col := range tbl.Columns {
var charMaxLen, charOctLen, numericPrecision, numericScale, datetimePrecision interface{}
colLen, decimal := col.Flen, col.Decimal
Expand Down
2 changes: 1 addition & 1 deletion meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func (m *Meta) GetAllHistoryDDLJobs() ([]*model.Job, error) {
if err != nil {
return nil, errors.Trace(err)
}
var jobs []*model.Job
jobs := make([]*model.Job, 0, len(pairs))
for _, pair := range pairs {
job := &model.Job{}
err = job.Decode(pair.Value)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/rule_join_elimination.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (o *outerJoinEliminator) tryToEliminateOuterJoin(p *LogicalJoin, aggCols []

// extract join keys as a schema for inner child of a outer join
func (o *outerJoinEliminator) extractInnerJoinKeys(join *LogicalJoin, innerChildIdx int) *expression.Schema {
var joinKeys []*expression.Column
joinKeys := make([]*expression.Column, 0, len(join.EqualConditions))
for _, eqCond := range join.EqualConditions {
joinKeys = append(joinKeys, eqCond.GetArgs()[innerChildIdx].(*expression.Column))
}
Expand Down
2 changes: 1 addition & 1 deletion server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ func (h allServerInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
return
}
allVersionsMap := map[domain.ServerVersionInfo]struct{}{}
var allVersions []domain.ServerVersionInfo
allVersions := make([]domain.ServerVersionInfo, 0, len(allServersInfo))
for _, v := range allServersInfo {
if _, ok := allVersionsMap[v.ServerVersionInfo]; ok {
continue
Expand Down
2 changes: 1 addition & 1 deletion server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (ts *HTTPHandlerTestSuite) TestRegionIndexRange(c *C) {
types.NewBytesDatum([]byte("foobar")),
types.NewFloat64Datum(-100.25),
}
var expectIndexValues []string
expectIndexValues := make([]string, 0, len(indexValues))
for _, v := range indexValues {
str, err := v.ToString()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion store/mockstore/mocktikv/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func (c *Cluster) firstStoreID() uint64 {

// getRegionsCoverRange gets regions in the cluster that has intersection with [start, end).
func (c *Cluster) getRegionsCoverRange(start, end MvccKey) []*Region {
var regions []*Region
regions := make([]*Region, 0, len(c.regions))
for _, region := range c.regions {
onRight := bytes.Compare(end, region.Meta.StartKey) <= 0
onLeft := bytes.Compare(region.Meta.EndKey, start) <= 0
Expand Down
4 changes: 2 additions & 2 deletions store/mockstore/mocktikv/mvcc_leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (mvcc *MVCCLevelDB) BatchGet(ks [][]byte, startTS uint64, isoLevel kvrpcpb.
mvcc.mu.RLock()
defer mvcc.mu.RUnlock()

var pairs []Pair
pairs := make([]Pair, 0, len(ks))
for _, k := range ks {
v, err := mvcc.getValue(k, startTS, isoLevel)
if v == nil && err == nil {
Expand Down Expand Up @@ -959,7 +959,7 @@ func (mvcc *MVCCLevelDB) RawBatchGet(keys [][]byte) [][]byte {
mvcc.mu.Lock()
defer mvcc.mu.Unlock()

var values [][]byte
values := make([][]byte, 0, len(keys))
for _, key := range keys {
value, err := mvcc.db.Get(key, nil)
terror.Log(err)
Expand Down
4 changes: 2 additions & 2 deletions store/tikv/delete_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func (s *testDeleteRangeSuite) checkData(c *C, expectedData map[string]string) {
c.Assert(err, IsNil)

// Print log
var actualKeys []string
var expectedKeys []string
actualKeys := make([]string, 0, len(data))
expectedKeys := make([]string, 0, len(expectedData))
for key := range data {
actualKeys = append(actualKeys, key)
}
Expand Down
2 changes: 1 addition & 1 deletion store/tikv/lock_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (lr *LockResolver) BatchResolveLocks(bo *Backoffer, locks []*Lock, loc Regi
zap.Duration("cost time", time.Since(startTime)),
zap.Int("num of txn", len(txnInfos)))

var listTxnInfos []*kvrpcpb.TxnInfo
listTxnInfos := make([]*kvrpcpb.TxnInfo, 0, len(txnInfos))
for txnID, status := range txnInfos {
listTxnInfos = append(listTxnInfos, &kvrpcpb.TxnInfo{
Txn: txnID,
Expand Down
2 changes: 1 addition & 1 deletion tablecodec/tablecodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func (s *testTableCodecSuite) TestDecodeIndexKey(c *C) {
// Type: mysql.TypeTimestamp,
// }),
}
var valueStrs []string
valueStrs := make([]string, 0, len(values))
for _, v := range values {
str, err := v.ToString()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1798,7 +1798,7 @@ func handleTruncateError(sc *stmtctx.StatementContext) error {

// DatumsToString converts several datums to formatted string.
func DatumsToString(datums []Datum, handleSpecialValue bool) (string, error) {
var strs []string
strs := make([]string, 0, len(datums))
for _, datum := range datums {
if handleSpecialValue {
switch datum.Kind() {
Expand Down

0 comments on commit e31e8f1

Please sign in to comment.