From cddc58ace282f74385d065119b779f9e9a5fdf10 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 25 Oct 2023 16:04:21 +0300 Subject: [PATCH] [#752] innerring: Optimize keyPosition() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` 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 --- pkg/innerring/indexer.go | 4 +--- pkg/innerring/indexer_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/innerring/indexer.go b/pkg/innerring/indexer.go index ac5fb93ef..5d0a87d64 100644 --- a/pkg/innerring/indexer.go +++ b/pkg/innerring/indexer.go @@ -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 } diff --git a/pkg/innerring/indexer_test.go b/pkg/innerring/indexer_test.go index 1937f7a49..5bc2cc988 100644 --- a/pkg/innerring/indexer_test.go +++ b/pkg/innerring/indexer_test.go @@ -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() + } + } +}