Skip to content

Commit

Permalink
Merge pull request #247 from RoaringBitmap/dlemire/fastertobytes
Browse files Browse the repository at this point in the history
No longer allocate temporary memory when serializing
  • Loading branch information
alldroll authored Mar 29, 2020
2 parents 4318595 + d53779d commit 239247b
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions roaringarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 239247b

Please sign in to comment.