mirror of
https://github.com/hanzoai/metrics.git
synced 2026-10-30 20:03:39 +00:00
Production workload shows that it's useful optimisation. Channel based objects pool allows to handle irregural data ingestion requests and make memory allocations more smooth. It's improves sync.Pool efficiency, since objects from sync.Pool removed after 2 GC cycles. With GOGC=30 value, GC runs significantly more often. https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6733 ### Checklist The following checks are **mandatory**: - [ ] My change adheres [VictoriaMetrics contributing guidelines](https://docs.victoriametrics.com/contributing/). --------- Signed-off-by: f41gh7 <[email protected]> Signed-off-by: hagen1778 <[email protected]> Co-authored-by: hagen1778 <[email protected]> (cherry picked from commit f255800da35206df39961cc243dcfce5a690c516) Signed-off-by: hagen1778 <[email protected]> # Conflicts: # app/vminsert/common/insert_ctx_pool.go
40 lines
707 B
Go
40 lines
707 B
Go
package common
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
|
)
|
|
|
|
// GetInsertCtx returns InsertCtx from the pool.
|
|
//
|
|
// Call PutInsertCtx for returning it to the pool.
|
|
func GetInsertCtx() *InsertCtx {
|
|
select {
|
|
case ctx := <-insertCtxPoolCh:
|
|
return ctx
|
|
default:
|
|
if v := insertCtxPool.Get(); v != nil {
|
|
return v.(*InsertCtx)
|
|
}
|
|
return &InsertCtx{}
|
|
}
|
|
}
|
|
|
|
// PutInsertCtx returns ctx to the pool.
|
|
//
|
|
// ctx cannot be used after the call.
|
|
func PutInsertCtx(ctx *InsertCtx) {
|
|
ctx.Reset(0)
|
|
select {
|
|
case insertCtxPoolCh <- ctx:
|
|
default:
|
|
insertCtxPool.Put(ctx)
|
|
}
|
|
}
|
|
|
|
var (
|
|
insertCtxPool sync.Pool
|
|
insertCtxPoolCh = make(chan *InsertCtx, cgroup.AvailableCPUs())
|
|
)
|