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

*: reuse chunk in the histogram #48583

Merged
merged 9 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/statistics/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (a *AnalyzeResult) DestroyAndPutToPool() {
for _, f := range a.Fms {
f.DestroyAndPutToPool()
}
for _, h := range a.Hist {
h.DestroyAndPutToPool()
}
}

// AnalyzeResults represents the analyze results of a task.
Expand Down
1 change: 1 addition & 0 deletions pkg/statistics/handle/storage/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func GenJSONTableFromStats(sctx sessionctx.Context, dbName string, tableInfo *mo
}
jsonTbl.Columns[col.Info.Name.L] = proto
col.FMSketch.DestroyAndPutToPool()
hist.DestroyAndPutToPool()
}
for _, idx := range tbl.Indices {
proto := dumpJSONCol(&idx.Histogram, idx.CMSketch, idx.TopN, nil, &idx.StatsVer)
Expand Down
10 changes: 9 additions & 1 deletion pkg/statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func NewHistogram(id, ndv, nullCount int64, version uint64, tp *types.FieldType,
NullCount: nullCount,
LastUpdateVersion: version,
Tp: tp,
Bounds: chunk.NewChunkWithCapacity([]*types.FieldType{tp}, 2*bucketSize),
Bounds: chunk.NewChunkFromPoolWithCapacity([]*types.FieldType{tp}, 2*bucketSize),
Buckets: make([]Bucket, 0, bucketSize),
TotColSize: totColSize,
}
Expand Down Expand Up @@ -220,6 +220,14 @@ func (hg *Histogram) Len() int {
return len(hg.Buckets)
}

// DestroyAndPutToPool resets the FMSketch and puts it to the pool.
func (hg *Histogram) DestroyAndPutToPool() {
if hg == nil {
return
}
hg.Bounds.Destroy(len(hg.Buckets), []*types.FieldType{hg.Tp})
}

// HistogramEqual tests if two histograms are equal.
func HistogramEqual(a, b *Histogram, ignoreID bool) bool {
if ignoreID {
Expand Down
1 change: 1 addition & 0 deletions pkg/util/chunk/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go_library(
"//pkg/util/hack",
"//pkg/util/logutil",
"//pkg/util/memory",
"//pkg/util/syncutil",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@org_golang_x_sys//cpu",
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func NewChunkWithCapacity(fields []*types.FieldType, capacity int) *Chunk {
return New(fields, capacity, capacity)
}

// NewChunkFromPoolWithCapacity creates a new chunk with field types and capacity from the pool.
func NewChunkFromPoolWithCapacity(fields []*types.FieldType, initCap int) *Chunk {
return getChunkFromPool(initCap, fields)
}

// New creates a new chunk.
//
// cap: the limit for the max number of rows.
Expand Down Expand Up @@ -669,3 +674,8 @@ func (c *Chunk) AppendPartialRows(colOff int, rows []Row) {
}
}
}

// Destroy is to destroy the Chunk and put Chunk into the pool
func (c *Chunk) Destroy(initCap int, fields []*types.FieldType) {
putChunkFromPool(initCap, fields, c)
}
59 changes: 49 additions & 10 deletions pkg/util/chunk/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,45 @@ import (
"sync"

"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/syncutil"
)

var (
globalChunkPoolMutex syncutil.RWMutex
// globalChunkPool is a chunk pool, the key is the init capacity.
globalChunkPool = make(map[int]*Pool)
)

// getChunkFromPool gets a Chunk from the Pool. In fact, initCap is the size of the bucket in the histogram.
// so it will not have too many difference value.
func getChunkFromPool(initCap int, fields []*types.FieldType) *Chunk {
globalChunkPoolMutex.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare with before, is there any performance issue for the destroy and get ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, as long as we don't encounter a new size, we are dealing with a read mutex. But since there are only so many sizes, it would only impact at startup. After that, it should all be read mutex.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, if we really encounter performance issues, this part can be turned into a shardmap. However, this would increase the code complexity here and affect the review process. We can consider this for subsequent optimizations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this initCap is the size of bucket in the histogram. I think it will not have too many difference values.

pool, ok := globalChunkPool[initCap]
globalChunkPoolMutex.RUnlock()
if ok {
elsa0520 marked this conversation as resolved.
Show resolved Hide resolved

return pool.GetChunk(fields)
}
globalChunkPoolMutex.Lock()
defer globalChunkPoolMutex.Unlock()
globalChunkPool[initCap] = NewPool(initCap)
return globalChunkPool[initCap].GetChunk(fields)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that we need exactly a matched capacity-size pool, a little bit less-than is adequate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this initCap is the size of bucket in the histogram. I think it can meet the requirement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add some comments for it.

}

func putChunkFromPool(initCap int, fields []*types.FieldType, chk *Chunk) {
globalChunkPoolMutex.RLock()
pool, ok := globalChunkPool[initCap]
globalChunkPoolMutex.RUnlock()
if ok {
pool.PutChunk(fields, chk)
return
}
globalChunkPoolMutex.Lock()
defer globalChunkPoolMutex.Unlock()
globalChunkPool[initCap] = NewPool(initCap)
globalChunkPool[initCap].PutChunk(fields, chk)
}

// Pool is the Column pool.
// NOTE: Pool is non-copyable.
type Pool struct {
Expand All @@ -36,11 +73,11 @@ type Pool struct {
func NewPool(initCap int) *Pool {
return &Pool{
initCap: initCap,
varLenColPool: &sync.Pool{New: func() interface{} { return newVarLenColumn(initCap) }},
fixLenColPool4: &sync.Pool{New: func() interface{} { return newFixedLenColumn(4, initCap) }},
fixLenColPool8: &sync.Pool{New: func() interface{} { return newFixedLenColumn(8, initCap) }},
fixLenColPool16: &sync.Pool{New: func() interface{} { return newFixedLenColumn(16, initCap) }},
fixLenColPool40: &sync.Pool{New: func() interface{} { return newFixedLenColumn(40, initCap) }},
varLenColPool: &sync.Pool{New: func() any { return newVarLenColumn(initCap) }},
fixLenColPool4: &sync.Pool{New: func() any { return newFixedLenColumn(4, initCap) }},
fixLenColPool8: &sync.Pool{New: func() any { return newFixedLenColumn(8, initCap) }},
fixLenColPool16: &sync.Pool{New: func() any { return newFixedLenColumn(16, initCap) }},
fixLenColPool40: &sync.Pool{New: func() any { return newFixedLenColumn(40, initCap) }},
}
}

Expand Down Expand Up @@ -69,17 +106,19 @@ func (p *Pool) GetChunk(fields []*types.FieldType) *Chunk {
// PutChunk puts a Chunk back to the Pool.
func (p *Pool) PutChunk(fields []*types.FieldType, chk *Chunk) {
for i, f := range fields {
c := chk.columns[i]
c.reset()
switch elemLen := getFixedLen(f); elemLen {
case varElemLen:
p.varLenColPool.Put(chk.columns[i])
p.varLenColPool.Put(c)
case 4:
p.fixLenColPool4.Put(chk.columns[i])
p.fixLenColPool4.Put(c)
case 8:
p.fixLenColPool8.Put(chk.columns[i])
p.fixLenColPool8.Put(c)
case 16:
p.fixLenColPool16.Put(chk.columns[i])
p.fixLenColPool16.Put(c)
case 40:
p.fixLenColPool40.Put(chk.columns[i])
p.fixLenColPool40.Put(c)
}
}
chk.columns = nil // release the Column references.
Expand Down