frostfs-node/pkg/local_object_storage/blobovnicza/sizes.go
Evgenii Stratonikov 1f4061c0e2 [#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>
2023-04-26 17:35:11 +03:00

52 lines
953 B
Go

package blobovnicza
import (
"encoding/binary"
"fmt"
"math/bits"
"strconv"
)
const firstBucketBound = uint64(32 * 1 << 10) // 32KB
func stringifyBounds(lower, upper uint64) string {
return fmt.Sprintf("[%s:%s]",
stringifyByteSize(lower),
stringifyByteSize(upper),
)
}
func stringifyByteSize(sz uint64) string {
return strconv.FormatUint(sz, 10)
}
func bucketKeyFromBounds(upperBound uint64) []byte {
buf := make([]byte, binary.MaxVarintLen64)
ln := binary.PutUvarint(buf, upperBound)
return buf[:ln]
}
func bucketForSize(sz uint64) []byte {
return bucketKeyFromBounds(upperPowerOfTwo(sz))
}
func upperPowerOfTwo(v uint64) uint64 {
if v <= firstBucketBound {
return firstBucketBound
}
return 1 << bits.Len64(v-1)
}
func (b *Blobovnicza) incSize(sz uint64) {
b.filled.Add(sz)
}
func (b *Blobovnicza) decSize(sz uint64) {
b.filled.Sub(sz)
}
func (b *Blobovnicza) full() bool {
return b.filled.Load() >= b.fullSizeLimit
}