From 6e863376f7ba8c43e3b8a97730919dc61a23dbc2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 15 Aug 2020 01:40:51 +0300 Subject: [PATCH] lib/leveledbytebufferpool: pre-allocate byte slice with the given capacity if the pool is empty This should reduce memory allocations and copying when the byte slice is growing. --- lib/leveledbytebufferpool/pool.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/leveledbytebufferpool/pool.go b/lib/leveledbytebufferpool/pool.go index 5ffc98085..37dcd312e 100644 --- a/lib/leveledbytebufferpool/pool.go +++ b/lib/leveledbytebufferpool/pool.go @@ -18,6 +18,9 @@ var pools [30]sync.Pool // Get returns byte buffer with the given capacity. func Get(capacity int) *bytesutil.ByteBuffer { + if capacity <= 0 { + capacity = 1 + } for i := 0; i < 2; i++ { v := getPool(capacity).Get() if v != nil { @@ -28,7 +31,9 @@ func Get(capacity int) *bytesutil.ByteBuffer { } capacity *= 2 } - return &bytesutil.ByteBuffer{} + return &bytesutil.ByteBuffer{ + B: make([]byte, 0, capacity), + } } // Put returns bb to the pool.