[#285] blobonicza: Optimize upperPowerOfTwo()

The real reason is this:
```
pkg/local_object_storage/blobovnicza/sizes.go:36:69
    revive empty-block: this block is empty, you can remove it
```

Didn't want to make this function longer or to add `nolint`, thus this
change. To justify:
```
UpperBound/size=1-8          0.4924n ± 1%   0.2472n ± 2%  -49.80% (p=0.000 n=10)
UpperBound/size=1023-8       0.4936n ± 3%   0.2442n ± 1%  -50.52% (p=0.000 n=10)
UpperBound/size=66560-8      0.8201n ± 2%   0.2436n ± 1%  -70.29% (p=0.000 n=10)
UpperBound/size=41943040-8   6.6900n ± 5%   0.2432n ± 0%  -96.36% (p=0.000 n=10)
geomean                       1.075n        0.2446n       -77.24%
```

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-04-26 11:29:33 +03:00
parent dfe4ada838
commit 1f4061c0e2
2 changed files with 16 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package blobovnicza
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/bits"
"strconv" "strconv"
) )
@ -31,11 +32,11 @@ func bucketForSize(sz uint64) []byte {
return bucketKeyFromBounds(upperPowerOfTwo(sz)) return bucketKeyFromBounds(upperPowerOfTwo(sz))
} }
func upperPowerOfTwo(v uint64) (upperBound uint64) { func upperPowerOfTwo(v uint64) uint64 {
for upperBound = firstBucketBound; upperBound < v; upperBound *= 2 { if v <= firstBucketBound {
return firstBucketBound
} }
return 1 << bits.Len64(v-1)
return
} }
func (b *Blobovnicza) incSize(sz uint64) { func (b *Blobovnicza) incSize(sz uint64) {

View file

@ -1,6 +1,7 @@
package blobovnicza package blobovnicza
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -36,3 +37,13 @@ func TestSizes(t *testing.T) {
require.Equal(t, bucketKeyFromBounds(item.upperBound), bucketForSize(item.sz)) require.Equal(t, bucketKeyFromBounds(item.upperBound), bucketForSize(item.sz))
} }
} }
func BenchmarkUpperBound(b *testing.B) {
for _, size := range []uint64{1, 1023, 65 * 1024, 40 * 1024 * 1024} {
b.Run(fmt.Sprintf("size=%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = upperPowerOfTwo(size)
}
})
}
}