From 67a538e2b4df11f8ec7139388838a13bce84b5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Thu, 22 Jun 2023 14:19:44 +0300 Subject: [PATCH] s2: add GetBufferCapacity() method (#832) Add GetBufferCapacity() method. We are reusing readers with sync.Pool and we'd like to avoid allocating memory for the default block size since most of the inputs are smaller. To have a better estimate of how big the lazy buffer should be, we are thinking about keeping in mind a running average of the internal buffer capacities. This method would allow us to implement that. --- s2/reader.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/s2/reader.go b/s2/reader.go index 8b84baa6d2..2f01a3987f 100644 --- a/s2/reader.go +++ b/s2/reader.go @@ -147,6 +147,13 @@ type Reader struct { ignoreCRC bool } +// GetBufferCapacity returns the capacity of the internal buffer. +// This might be useful to know when reusing the same reader in combination +// with the lazy buffer option. +func (r *Reader) GetBufferCapacity() int { + return cap(r.buf) +} + // ensureBufferSize will ensure that the buffer can take at least n bytes. // If false is returned the buffer exceeds maximum allowed size. func (r *Reader) ensureBufferSize(n int) bool {