From d53779d1396f033ef7998fef3717bf741ddf87d0 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 27 Mar 2020 12:34:31 -0400 Subject: [PATCH] No longer allocate temporary memory. --- roaringarray.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/roaringarray.go b/roaringarray.go index d9d5edda..2f46fbb0 100644 --- a/roaringarray.go +++ b/roaringarray.go @@ -488,20 +488,15 @@ func (ra *roaringArray) writeTo(w io.Writer) (n int64, err error) { nw += 2 binary.LittleEndian.PutUint16(buf[2:], uint16(len(ra.keys)-1)) nw += 2 - - // compute isRun bitmap - var ir []byte - - isRun := newBitmapContainer() + // compute isRun bitmap without temporary allocation + var runbitmapslice = buf[nw:nw+isRunSizeInBytes] for i, c := range ra.containers { switch c.(type) { case *runContainer16: - isRun.iadd(uint16(i)) + runbitmapslice[i / 8] |= 1<<(uint(i)%8) } } - // convert to little endian - ir = isRun.asLittleEndianByteSlice()[:isRunSizeInBytes] - nw += copy(buf[nw:], ir) + nw += isRunSizeInBytes } else { binary.LittleEndian.PutUint32(buf[0:], uint32(serialCookieNoRunContainer)) nw += 4