From 05f96f3956d9d198aa690e655930daa60559607f Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 13 Jun 2018 11:55:12 -0700 Subject: [PATCH] Avoid allocating larger buffer if the incoming body is small (#6035) --- cmd/xl-v1-multipart.go | 13 +++++++++++-- cmd/xl-v1-object.go | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/xl-v1-multipart.go b/cmd/xl-v1-multipart.go index 182d24edf..f17d33902 100644 --- a/cmd/xl-v1-multipart.go +++ b/cmd/xl-v1-multipart.go @@ -375,8 +375,17 @@ func (xl xlObjects) PutObjectPart(ctx context.Context, bucket, object, uploadID } // Fetch buffer for I/O, returns from the pool if not allocates a new one and returns. - buffer := xl.bp.Get() - defer xl.bp.Put(buffer) + var buffer []byte + switch size := data.Size(); { + case size == 0: + buffer = make([]byte, 1) // Allocate atleast a byte to reach EOF + case size < blockSizeV1: + // No need to allocate fully blockSizeV1 buffer if the incoming data is smaller. + buffer = make([]byte, size, 2*size) + default: + buffer = xl.bp.Get() + defer xl.bp.Put(buffer) + } file, err := storage.CreateFile(ctx, data, minioMetaTmpBucket, tmpPartPath, buffer, DefaultBitrotAlgorithm, writeQuorum) if err != nil { diff --git a/cmd/xl-v1-object.go b/cmd/xl-v1-object.go index 606903430..ce23e64cc 100644 --- a/cmd/xl-v1-object.go +++ b/cmd/xl-v1-object.go @@ -611,8 +611,17 @@ func (xl xlObjects) putObject(ctx context.Context, bucket string, object string, } // Fetch buffer for I/O, returns from the pool if not allocates a new one and returns. - buffer := xl.bp.Get() - defer xl.bp.Put(buffer) + var buffer []byte + switch size := data.Size(); { + case size == 0: + buffer = make([]byte, 1) // Allocate atleast a byte to reach EOF + case size < blockSizeV1: + // No need to allocate fully blockSizeV1 buffer if the incoming data is smaller. + buffer = make([]byte, size, 2*size) + default: + buffer = xl.bp.Get() + defer xl.bp.Put(buffer) + } // Read data and split into parts - similar to multipart mechanism for partIdx := 1; ; partIdx++ {