-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Changes from all commits
dff0feb
4da11cf
55d2440
c6aced4
214212a
8171566
ddb61e1
9b4d22d
fe19328
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,44 @@ 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() | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -36,11 +72,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) }}, | ||
} | ||
} | ||
|
||
|
@@ -69,17 +105,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. | ||
|
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.