]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: avoid TypeOf in init
authorJoe Tsai <joetsai@digital-static.net>
Thu, 28 Jul 2022 06:19:40 +0000 (23:19 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Mon, 8 Aug 2022 17:36:35 +0000 (17:36 +0000)
Calling TypeOf to initialize variables forces any import of "reflect"
to link in the declared types of "reflect" even if they are unused.
TypeOf operates on Type and which will pull in
all transitive dependencies of Type, which includes Value as well.
Avoid this problem by declaring a rtypeOf function that
directly extracts the *rtype from an interface value
without going through Type as an intermediate type.

For a program that blank imports "reflect",
this reduces the binary size by ~34 KiB.

Updates #54097

Change-Id: I8dc7d8da8fedc48cc0dd842b69f510d17144827e
Reviewed-on: https://go-review.googlesource.com/c/go/+/419757
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/reflect/type.go
src/reflect/value.go

index 75994c97a9bd13ca04a0c514577ce9737219e46c..cb657905d0b9fce44dd7c6dd5d35350281edab64 100644 (file)
@@ -1433,6 +1433,12 @@ func TypeOf(i any) Type {
        return toType(eface.typ)
 }
 
+// rtypeOf directly extracts the *rtype of the provided value.
+func rtypeOf(i any) *rtype {
+       eface := *(*emptyInterface)(unsafe.Pointer(&i))
+       return eface.typ
+}
+
 // ptrMap is the cache for PointerTo.
 var ptrMap sync.Map // map[*rtype]*ptrType
 
index 969e57cf83be8df80b09d2d572ae3528067ac8f1..d8479c64ef9b285fd64f04e7388cf9da26d8af3a 100644 (file)
@@ -290,7 +290,7 @@ func (v Value) panicNotBool() {
        v.mustBe(Bool)
 }
 
-var bytesType = TypeOf(([]byte)(nil)).(*rtype)
+var bytesType = rtypeOf(([]byte)(nil))
 
 // Bytes returns v's underlying value.
 // It panics if v's underlying value is not a slice of bytes or
@@ -1381,7 +1381,7 @@ func (v Value) Float() float64 {
        panic(&ValueError{"reflect.Value.Float", v.kind()})
 }
 
-var uint8Type = TypeOf(uint8(0)).(*rtype)
+var uint8Type = rtypeOf(uint8(0))
 
 // Index returns v's i'th element.
 // It panics if v's Kind is not Array, Slice, or String or i is out of range.
@@ -1640,7 +1640,7 @@ func (v Value) lenNonSlice() int {
        panic(&ValueError{"reflect.Value.Len", v.kind()})
 }
 
-var stringType = TypeOf("").(*rtype)
+var stringType = rtypeOf("")
 
 // MapIndex returns the value associated with key in the map v.
 // It panics if v's Kind is not Map.