From: Tomasz Jezierski Date: Mon, 1 Aug 2022 19:18:36 +0000 (+0200) Subject: encoding/gob: replace runtime values with constants in init() X-Git-Tag: go1.20rc1~1796 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0f6ee42fe063a48d7825bc03097bbb714aafdb7d;p=gostls13.git encoding/gob: replace runtime values with constants in init() Current init() implementation in `encoding/gob/decode.go` checks int/uint/uintptr bit size with reflection in runtime. We could replace it with values available on compile stage. This should reduce time and allocations on binary start. Results from GODEBUG=inittrace=1: before: init encoding/gob @4.4 ms, 0.21 ms clock, 43496 bytes, 652 allocs after: init encoding/gob @4.4 ms, 0.15 ms clock, 41672 bytes, 643 allocs Updates #54184 Change-Id: I46dda2682fb92519da199415e29401d61edce697 Reviewed-on: https://go-review.googlesource.com/c/go/+/420455 TryBot-Result: Gopher Robot Run-TryBot: Rob Pike Reviewed-by: Rob Pike Reviewed-by: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov --- diff --git a/src/encoding/gob/decode.go b/src/encoding/gob/decode.go index eea2924f1a..7bca13c957 100644 --- a/src/encoding/gob/decode.go +++ b/src/encoding/gob/decode.go @@ -1228,9 +1228,14 @@ func (dec *Decoder) decodeIgnoredValue(wireId typeId) { } } +const ( + intBits = 32 << (^uint(0) >> 63) + uintptrBits = 32 << (^uintptr(0) >> 63) +) + func init() { var iop, uop decOp - switch reflect.TypeOf(int(0)).Bits() { + switch intBits { case 32: iop = decInt32 uop = decUint32 @@ -1244,7 +1249,7 @@ func init() { decOpTable[reflect.Uint] = uop // Finally uintptr - switch reflect.TypeOf(uintptr(0)).Bits() { + switch uintptrBits { case 32: uop = decUint32 case 64: