]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: make Value.Type inlineable
authorJoe Tsai <joetsai@digital-static.net>
Sat, 16 Apr 2022 01:09:48 +0000 (18:09 -0700)
committerGopher Robot <gobot@golang.org>
Sun, 17 Apr 2022 00:58:13 +0000 (00:58 +0000)
This allows the result of Type to be computed much faster.

Performance:

old     new     delta
1.76ns  0.66ns  -62.27%

Change-Id: Ie007fd175aaa41b2f67c71fa2a34ab8d292dd0e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/400335
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/reflect/value.go

index 2496cbe46366716252727a928f4c40c2d86197c9..06f0469edefc96ae88f1fa0d84cf29654210e21e 100644 (file)
@@ -2465,12 +2465,17 @@ func (v Value) TrySend(x Value) bool {
 
 // Type returns v's type.
 func (v Value) Type() Type {
-       f := v.flag
-       if f == 0 {
+       if v.flag != 0 && v.flag&flagMethod == 0 {
+               return v.typ
+       }
+       return v.typeSlow()
+}
+
+func (v Value) typeSlow() Type {
+       if v.flag == 0 {
                panic(&ValueError{"reflect.Value.Type", Invalid})
        }
-       if f&flagMethod == 0 {
-               // Easy case
+       if v.flag&flagMethod == 0 {
                return v.typ
        }