[#1054] network: Optimize IsTLSEnabled()
DCO action / DCO (pull_request) Successful in 5m33s Details
Vulncheck / Vulncheck (pull_request) Successful in 5m54s Details
Build / Build Components (1.21) (pull_request) Successful in 10m3s Details
Tests and linters / gopls check (pull_request) Successful in 11m49s Details
Build / Build Components (1.20) (pull_request) Successful in 12m47s Details
Tests and linters / Staticcheck (pull_request) Successful in 13m21s Details
Tests and linters / Lint (pull_request) Successful in 14m21s Details
Tests and linters / Tests (1.20) (pull_request) Successful in 16m39s Details
Tests and linters / Tests with -race (pull_request) Successful in 16m46s Details
Tests and linters / Tests (1.21) (pull_request) Successful in 16m59s Details

No big deal, but it is called multiple times in sorting routine, this
easily results in 20 allocations per group traversal.

```
goos: linux
goarch: amd64
pkg: git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network
cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
                    │     old     │                 new                 │
                    │   sec/op    │   sec/op     vs base                │
AddressTLSEnabled-8   184.6n ± 1%   103.3n ± 6%  -44.04% (p=0.000 n=10)

                    │    old     │                new                │
                    │    B/op    │   B/op    vs base                 │
AddressTLSEnabled-8   704.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=10)

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

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
pull/1054/head
Evgenii Stratonikov 2024-03-21 18:46:07 +03:00
parent c09c701613
commit c7a12ca3d8
2 changed files with 17 additions and 7 deletions

View File

@ -13,11 +13,6 @@ var tls, _ = multiaddr.NewMultiaddr("/" + tlsProtocolName)
// IsTLSEnabled searches for wrapped TLS protocol in multiaddr.
func (a Address) IsTLSEnabled() bool {
for _, protoc := range a.ma.Protocols() {
if protoc.Code == multiaddr.P_TLS {
return true
}
}
return false
_, err := a.ma.ValueForProtocol(multiaddr.P_TLS)
return err == nil
}

View File

@ -27,3 +27,18 @@ func TestAddress_TLSEnabled(t *testing.T) {
require.Equal(t, test.wantTLS, addr.IsTLSEnabled(), test.input)
}
}
func BenchmarkAddressTLSEnabled(b *testing.B) {
var addr Address
err := addr.FromString("/dns4/localhost/tcp/8080/tls")
require.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
var enabled bool
for i := 0; i < b.N; i++ {
enabled = addr.IsTLSEnabled()
}
require.True(b, enabled)
}