Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunk column reuse #28340

Closed
tiancaiamao opened this issue Sep 26, 2021 · 5 comments · Fixed by #29064
Closed

Chunk column reuse #28340

tiancaiamao opened this issue Sep 26, 2021 · 5 comments · Fixed by #29064
Assignees
Labels
type/enhancement The issue or PR belongs to an enhancement.

Comments

@tiancaiamao
Copy link
Contributor

Enhancement

Currently, TiDB allocation cause too much burden to the Go GC, and slow down the program.

Prepare plan cache can help a lot to reduce the allocation, unfortunately we still can not take full advantage of it.
I reported this issue #26868 in the past.
@qw4990 and @Reminiscent is handling it and we wish to make it GA soon.

Prepare plan cache ranks top1 in reducing the allocation.
So after that, I find chunk allocation also accounts a lot.

If we can reuse the chunk column, it may reduce the object allocation.

Chunk columns are feasible for reuse, as you can see they are fix sized depending on the field type, and there is no complex structure in it, only slice of bytes:

type Column struct {
length int
nullBitmap []byte // bit 0 is null, 1 is not null
offsets []int64 // used for varLen column. Row i starts from data[offsets[i]]
data []byte
elemBuf []byte
}
// NewColumn creates a new column with the specific type and capacity.
func NewColumn(ft *types.FieldType, cap int) *Column {
return newColumn(getFixedLen(ft), cap)
}
func newColumn(typeSize, cap int) *Column {
var col *Column
if typeSize == varElemLen {
col = newVarLenColumn(cap)
} else {
col = newFixedLenColumn(typeSize, cap)
}
return col
}

@tiancaiamao tiancaiamao added the type/enhancement The issue or PR belongs to an enhancement. label Sep 26, 2021
@tiancaiamao
Copy link
Contributor Author

@Defined2014

@Defined2014
Copy link
Contributor

Defined2014 commented Oct 25, 2021

We have localColumnPool in expression/builtin_vectorized.go. So when enabled tidb_enable_vectorized_expression, it will reuse column chunk.

type localColumnPool struct {
sync.Pool
}
func newLocalColumnPool() *localColumnPool {
newColumn := chunk.NewColumn(types.NewFieldType(mysql.TypeLonglong), chunk.InitialCapacity)
return &localColumnPool{
sync.Pool{
New: func() interface{} {
return newColumn.CopyConstruct(nil)
},
},
}
}
var globalColumnAllocator = newLocalColumnPool()
// GetColumn allocates a column. The allocator is not responsible for initializing the column, so please initialize it before using.
func GetColumn(_ types.EvalType, _ int) (*chunk.Column, error) {
return globalColumnAllocator.get()
}
// PutColumn releases a column buffer.
func PutColumn(buf *chunk.Column) {
globalColumnAllocator.put(buf)
}
func (r *localColumnPool) get() (*chunk.Column, error) {
col, ok := r.Pool.Get().(*chunk.Column)
if !ok {
return nil, errors.New("unexpected object in localColumnPool")
}
return col, nil
}
func (r *localColumnPool) put(col *chunk.Column) {
r.Pool.Put(col)
}

The implemention use sync.Pool to cache chunk column which will be recycled in 2 GC duration. It is introduced by #26078, not sure included in your test or not.

I'm not sure we need a true mempool for chunk column or not, because I can't reproduce high usage in chunk column right now. @tiancaiamao

@tiancaiamao
Copy link
Contributor Author

I guess it's for the OLAP query, and OLTP can't benefit from it. @Defined2014

@tiancaiamao
Copy link
Contributor Author

I found the old picture, the motivation for this issue:
image

We already have the code localColumnPool in expression/builtin_vectorized.go,
but as you can see, it doesn't eliminate the chunk allocation.

@tiancaiamao
Copy link
Contributor Author

I think maybe we can test the changes in the bank workload to verify it. @dbsid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants