From 07eef0b45428c775baa29e2cfb9b9700c4884be4 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 2 Aug 2024 09:16:44 -0400 Subject: [PATCH] fix: make RunOptimize idempotent --- arraycontainer.go | 2 +- bitmapcontainer.go | 2 +- roaring_test.go | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/arraycontainer.go b/arraycontainer.go index 0489c2b5..c07010bf 100644 --- a/arraycontainer.go +++ b/arraycontainer.go @@ -1233,7 +1233,7 @@ func (ac *arrayContainer) toEfficientContainer() container { card := ac.getCardinality() sizeAsArrayContainer := arrayContainerSizeInBytes(card) - if sizeAsRunContainer <= minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) { + if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) { return newRunContainer16FromArray(ac) } if card <= arrayDefaultMaxSize { diff --git a/bitmapcontainer.go b/bitmapcontainer.go index 3eaad2ba..10bc0f1c 100644 --- a/bitmapcontainer.go +++ b/bitmapcontainer.go @@ -1187,7 +1187,7 @@ func (bc *bitmapContainer) toEfficientContainer() container { card := bc.getCardinality() sizeAsArrayContainer := arrayContainerSizeInBytes(card) - if sizeAsRunContainer <= minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) { + if sizeAsRunContainer < minOfInt(sizeAsBitmapContainer, sizeAsArrayContainer) { return newRunContainer16FromBitmapContainer(bc) } if card <= arrayDefaultMaxSize { diff --git a/roaring_test.go b/roaring_test.go index 859bb0e8..efe3ce01 100644 --- a/roaring_test.go +++ b/roaring_test.go @@ -13,6 +13,18 @@ import ( "github.com/stretchr/testify/require" ) +func TestIssue440(t *testing.T) { + a := NewBitmap() + a.AddMany([]uint32{1, 2, 3}) + a.RunOptimize() + b1, err := a.MarshalBinary() + require.NoError(t, err) + a.RunOptimize() + b2, err := a.MarshalBinary() + require.NoError(t, err) + require.Equal(t, b1, b2) +} + func checkValidity(t *testing.T, rb *Bitmap) { t.Helper()