Skip to content

Commit c292397

Browse files
mateusz834rolandshoemaker
authored andcommitted
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: 361d68d GitHub-Pull-Request: #59198 Reviewed-on: https://go-review.googlesource.com/c/go/+/478836 Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Mateusz Poliwczak <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent b441eb3 commit c292397

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/encoding/asn1/asn1.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"math/big"
2727
"reflect"
2828
"strconv"
29+
"strings"
2930
"time"
3031
"unicode/utf16"
3132
"unicode/utf8"
@@ -236,16 +237,18 @@ func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool {
236237
}
237238

238239
func (oi ObjectIdentifier) String() string {
239-
var s string
240+
var s strings.Builder
241+
s.Grow(32)
240242

243+
buf := make([]byte, 0, 19)
241244
for i, v := range oi {
242245
if i > 0 {
243-
s += "."
246+
s.WriteByte('.')
244247
}
245-
s += strconv.Itoa(v)
248+
s.Write(strconv.AppendInt(buf, int64(v), 10))
246249
}
247250

248-
return s
251+
return s.String()
249252
}
250253

251254
// parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and

src/encoding/asn1/asn1_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,10 @@ func TestNonMinimalEncodedOID(t *testing.T) {
11681168
t.Fatalf("accepted non-minimally encoded oid")
11691169
}
11701170
}
1171+
1172+
func BenchmarkObjectIdentifierString(b *testing.B) {
1173+
oidPublicKeyRSA := ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1}
1174+
for i := 0; i < b.N; i++ {
1175+
_ = oidPublicKeyRSA.String()
1176+
}
1177+
}

0 commit comments

Comments
 (0)