pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
37.03k stars 5.82k forks source link

[enhance] Try to optimize memory cost of the value copy in the initlialize.go #19627

Open markjenny opened 4 years ago

markjenny commented 4 years ago

topology

running the tiup playground on my laptop. 1 TiKV, 1 TiDB and 1 PD.

workload

using sysbench to create one database consist of 32 tables. There're 10000 records in each table.

sysbench oltp_update_non_index --config-file=config --threads=32 --tables=32 --table-size=10000 prepare

profile

generate the profile using the method below

curl http://{TiDBIP}:10080/debug/zip?seconds=60 --output debug.zip

the heap profile is here The most memory cost is in the fuction buildValueListOfInsert, So try to find the memory optimization.

In the file initialize.go, there are some Init function using the value receiver, like this:

// Init initializes Delete.
func (p Delete) Init(ctx sessionctx.Context) *Delete {
    p.basePlan = newBasePlan(ctx, plancodec.TypeDelete, 0)
    return &p
}

// Init initializes Insert.
func (p Insert) Init(ctx sessionctx.Context) *Insert {
    p.basePlan = newBasePlan(ctx, plancodec.TypeInsert, 0)
    return &p
}

We can use the pointer receiver to finish the same init as the value receiver using the below code. And there is no need of value copy, costing less memory. copy

// Init initializes Insert.
func (p *Insert) Init(ctx sessionctx.Context) *Insert {
    p.basePlan = newBasePlan(ctx, plancodec.TypeInsert, 0)
    return p
}
ZenoTan commented 4 years ago

/label type/suggestion

ZenoTan commented 4 years ago

/label status/PTAL

ZenoTan commented 4 years ago

/label sig/planner