diff options
author | Martin Möhrmann <moehrmann@google.com> | 2020-05-03 19:58:10 +0200 |
---|---|---|
committer | Martin Möhrmann <moehrmann@google.com> | 2020-08-18 17:46:40 +0000 |
commit | 861a9483357a1a13609430ec6684b3dc9209e80c (patch) | |
tree | 4c0f6ebc273d72afb5189e0b41633fa40169b7c3 /src/encoding | |
parent | cdc77d34d7770ed02d84b9193380f9646017dce6 (diff) | |
download | go-861a9483357a1a13609430ec6684b3dc9209e80c.tar.gz go-861a9483357a1a13609430ec6684b3dc9209e80c.zip |
encoding/asn1: speed up marshal by reducing allocations
Replace strings.Split by strings.IndexByte and explicit
slicing to avoid the allocation of the return slice
of strings.Split.
name old time/op new time/op delta
Marshal 43.3µs ± 1% 36.7µs ± 1% -15.23% (p=0.000 n=9+9)
name old alloc/op new alloc/op delta
Marshal 10.7kB ± 0% 9.2kB ± 0% -13.96% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
Marshal 444 ± 0% 366 ± 0% -17.57% (p=0.000 n=10+10)
Change-Id: I9e727defa23f7e5fc684f246de0136fe28cf8d25
Reviewed-on: https://go-review.googlesource.com/c/go/+/231738
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/encoding')
-rw-r--r-- | src/encoding/asn1/common.go | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/encoding/asn1/common.go b/src/encoding/asn1/common.go index e2aa8bd9c5..1c712e1eff 100644 --- a/src/encoding/asn1/common.go +++ b/src/encoding/asn1/common.go @@ -92,7 +92,16 @@ type fieldParameters struct { // parseFieldParameters will parse it into a fieldParameters structure, // ignoring unknown parts of the string. func parseFieldParameters(str string) (ret fieldParameters) { - for _, part := range strings.Split(str, ",") { + var part string + for len(str) > 0 { + // This loop uses IndexByte and explicit slicing + // instead of strings.Split(str, ",") to reduce allocations. + i := strings.IndexByte(str, ',') + if i < 0 { + part, str = str, "" + } else { + part, str = str[:i], str[i+1:] + } switch { case part == "optional": ret.optional = true |