diff options
| author | Mateusz Poliwczak <mpoliwczak34@gmail.com> | 2023-03-29 17:04:54 +0000 |
|---|---|---|
| committer | Roland Shoemaker <roland@golang.org> | 2023-03-29 18:24:36 +0000 |
| commit | c2923971600a89db65daee86858f5fc054322129 (patch) | |
| tree | 4d5dbd097837dcd82eedf05927d22a7ab7ee91f8 /src/encoding | |
| parent | b441eb3f978b8b7d4f09f8e583d419d8f4e40af6 (diff) | |
| download | go-c2923971600a89db65daee86858f5fc054322129.tar.xz | |
encoding/asn1: improve memory efficiency of ObjectIdentifier.String
name old time/op new time/op delta
ObjectIdentifierString-4 670ns ± 9% 157ns ±14% -76.59% (p=0.000 n=10+9)
name old alloc/op new alloc/op delta
ObjectIdentifierString-4 184B ± 0% 32B ± 0% -82.61% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
ObjectIdentifierString-4 14.0 ± 0% 1.0 ± 0% -92.86% (p=0.000 n=10+10)
This also improves the x509 certificate parser performance by ~12-15%
name old time/op new time/op delta
ParseCertificate/ecdsa_leaf-4 24.5µs ± 8% 20.9µs ±11% -14.66% (p=0.000 n=10+10)
ParseCertificate/rsa_leaf-4 26.6µs ± 5% 23.5µs ± 7% -11.83% (p=0.000 n=8+10)
name old alloc/op new alloc/op delta
ParseCertificate/ecdsa_leaf-4 12.5kB ± 0% 12.0kB ± 0% -3.72% (p=0.000 n=10+10)
ParseCertificate/rsa_leaf-4 13.9kB ± 0% 13.4kB ± 0% -3.34% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
ParseCertificate/ecdsa_leaf-4 238 ± 0% 165 ± 0% -30.67% (p=0.000 n=10+10)
ParseCertificate/rsa_leaf-4 262 ± 0% 189 ± 0% -27.86% (p=0.000 n=10+10)
Change-Id: I49905bbf8319b840e9211da73570db35d1445217
GitHub-Last-Rev: 361d68dc9b64c50e3b20e2cf91bffe54cfaf10d4
GitHub-Pull-Request: golang/go#59198
Reviewed-on: https://go-review.googlesource.com/c/go/+/478836
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/encoding')
| -rw-r--r-- | src/encoding/asn1/asn1.go | 11 | ||||
| -rw-r--r-- | src/encoding/asn1/asn1_test.go | 7 |
2 files changed, 14 insertions, 4 deletions
diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go index f743cd6f69..e7bf793a82 100644 --- a/src/encoding/asn1/asn1.go +++ b/src/encoding/asn1/asn1.go @@ -26,6 +26,7 @@ import ( "math/big" "reflect" "strconv" + "strings" "time" "unicode/utf16" "unicode/utf8" @@ -236,16 +237,18 @@ func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool { } func (oi ObjectIdentifier) String() string { - var s string + var s strings.Builder + s.Grow(32) + buf := make([]byte, 0, 19) for i, v := range oi { if i > 0 { - s += "." + s.WriteByte('.') } - s += strconv.Itoa(v) + s.Write(strconv.AppendInt(buf, int64(v), 10)) } - return s + return s.String() } // parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and diff --git a/src/encoding/asn1/asn1_test.go b/src/encoding/asn1/asn1_test.go index 0e67dbf396..9a605e245c 100644 --- a/src/encoding/asn1/asn1_test.go +++ b/src/encoding/asn1/asn1_test.go @@ -1168,3 +1168,10 @@ func TestNonMinimalEncodedOID(t *testing.T) { t.Fatalf("accepted non-minimally encoded oid") } } + +func BenchmarkObjectIdentifierString(b *testing.B) { + oidPublicKeyRSA := ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} + for i := 0; i < b.N; i++ { + _ = oidPublicKeyRSA.String() + } +} |
