From cecea8053a8e394994a8f85765a7be07b0da394f Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Mon, 3 Apr 2023 13:09:44 +0300 Subject: [PATCH] [#199] putsvc: Refactor streamer pool Resolve staticcheck linter for putBytesPool Signed-off-by: Dmitrii Stepanov --- pkg/services/object/put/distributed.go | 6 +++--- pkg/services/object/put/pool.go | 16 ++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/services/object/put/distributed.go b/pkg/services/object/put/distributed.go index 3b5f4ec5..e4566157 100644 --- a/pkg/services/object/put/distributed.go +++ b/pkg/services/object/put/distributed.go @@ -28,7 +28,7 @@ type distributedTarget struct { obj *objectSDK.Object objMeta object.ContentMeta - payload []byte + payload *payload nodeTargetInitializer func(nodeDesc) preparedObjectTarget @@ -122,7 +122,7 @@ func (t *distributedTarget) WriteHeader(obj *objectSDK.Object) error { } func (t *distributedTarget) Write(p []byte) (n int, err error) { - t.payload = append(t.payload, p...) + t.payload.Data = append(t.payload.Data, p...) return len(p), nil } @@ -133,7 +133,7 @@ func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) { t.payload = nil }() - t.obj.SetPayload(t.payload) + t.obj.SetPayload(t.payload.Data) var err error diff --git a/pkg/services/object/put/pool.go b/pkg/services/object/put/pool.go index 70527322..5726856e 100644 --- a/pkg/services/object/put/pool.go +++ b/pkg/services/object/put/pool.go @@ -6,15 +6,19 @@ import ( const defaultAllocSize = 1024 +type payload struct { + Data []byte +} + var putBytesPool = &sync.Pool{ - New: func() any { return make([]byte, 0, defaultAllocSize) }, + New: func() any { return &payload{Data: make([]byte, 0, defaultAllocSize)} }, } -func getPayload() []byte { - return putBytesPool.Get().([]byte) +func getPayload() *payload { + return putBytesPool.Get().(*payload) } -func putPayload(p []byte) { - //nolint:staticcheck - putBytesPool.Put(p[:0]) +func putPayload(p *payload) { + p.Data = p.Data[:0] + putBytesPool.Put(p) }