[#752] innerring: Optimize keyPosition()

```
goos: linux
goarch: amd64
pkg: git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
              │      old       │                 new                 │
              │     sec/op     │   sec/op     vs base                │
KeyPosition-8   2771.50n ± 10%   40.32n ± 4%  -98.55% (p=0.000 n=10)

              │     old      │                  new                  │
              │     B/op     │     B/op      vs base                 │
KeyPosition-8   1.531Ki ± 0%   0.000Ki ± 0%  -100.00% (p=0.000 n=10)

              │    old     │                new                 │
              │ allocs/op  │ allocs/op  vs base                 │
KeyPosition-8   21.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)
```

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-10-25 16:04:21 +03:00
parent 0a9830564f
commit cddc58ace2
2 changed files with 21 additions and 3 deletions

View file

@ -1,7 +1,6 @@
package innerring
import (
"bytes"
"fmt"
"sync"
"time"
@ -113,10 +112,9 @@ func (s *innerRingIndexer) AlphabetIndex() (int32, error) {
// index of the key.
func keyPosition(key *keys.PublicKey, list keys.PublicKeys) (result int32) {
result = -1
rawBytes := key.Bytes()
for i := range list {
if bytes.Equal(list[i].Bytes(), rawBytes) {
if key.Equal(list[i]) {
result = int32(i)
break
}

View file

@ -223,3 +223,23 @@ func (f *testIRFetcher) InnerRingKeys() (keys.PublicKeys, error) {
f.calls.Add(1)
return f.keys, f.err
}
func BenchmarkKeyPosition(b *testing.B) {
list := make(keys.PublicKeys, 7)
for i := range list {
p, err := keys.NewPrivateKey()
require.NoError(b, err)
list[i] = p.PublicKey()
}
key := new(keys.PublicKey)
require.NoError(b, key.DecodeBytes(list[5].Bytes()))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if keyPosition(key, list) != 5 {
b.FailNow()
}
}
}