From 45554b42c0fe109c14ad1bc1a029247a7cf55fe7 Mon Sep 17 00:00:00 2001 From: Injun Song Date: Tue, 18 Jul 2023 14:11:36 +0900 Subject: [PATCH] perf(metarepos): add a pool for mrpb.Reports This change adds the pool for mrpb.Reports, which makes the metadata repository reuse heap object for it. --- .../metarepos/raft_metadata_repository.go | 7 ++--- proto/mrpb/raft_entry.go | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 proto/mrpb/raft_entry.go diff --git a/internal/metarepos/raft_metadata_repository.go b/internal/metarepos/raft_metadata_repository.go index 4909c767b..a1477ee37 100644 --- a/internal/metarepos/raft_metadata_repository.go +++ b/internal/metarepos/raft_metadata_repository.go @@ -306,6 +306,8 @@ Loop: mr.logger.Error(err.Error()) continue } + // Release *mrpb.Reports only if e contains Reports. + e.GetRequest().Report.Release() select { case mr.rnProposeC <- b: @@ -347,10 +349,7 @@ func (mr *RaftMetadataRepository) processReport(ctx context.Context) { mr.muReportQueue.Lock() num := len(mr.reportQueue) if num > 0 { - reports = &mrpb.Reports{ - NodeID: mr.nodeID, - CreatedTime: time.Now(), - } + reports = mrpb.NewReports(mr.nodeID, time.Now()) reports.Reports = mr.reportQueue mr.reportQueue = make([]*mrpb.Report, 0, 1024) } diff --git a/proto/mrpb/raft_entry.go b/proto/mrpb/raft_entry.go new file mode 100644 index 000000000..0f2a226b8 --- /dev/null +++ b/proto/mrpb/raft_entry.go @@ -0,0 +1,28 @@ +package mrpb + +import ( + "sync" + "time" + + "github.com/kakao/varlog/pkg/types" +) + +var reportsPool = sync.Pool{ + New: func() any { + return &Reports{} + }, +} + +func NewReports(nodeID types.NodeID, ts time.Time) *Reports { + rs := reportsPool.Get().(*Reports) + rs.NodeID = nodeID + rs.CreatedTime = ts + return rs +} + +func (rs *Reports) Release() { + if rs != nil { + *rs = Reports{} + reportsPool.Put(rs) + } +}