]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: avoid lock for some NumMethod()==0 cases
authorDavid Crawshaw <crawshaw@golang.org>
Fri, 24 Jun 2016 15:09:48 +0000 (11:09 -0400)
committerDavid Crawshaw <crawshaw@golang.org>
Fri, 24 Jun 2016 18:01:08 +0000 (18:01 +0000)
The encoding/json package uses NumMethod()==0 as a fast check for
interface satisfaction. In the case when a type has no methods at
all, we don't need to grab the RWMutex.

Improves JSON decoding benchmark on linux/amd64:

name           old time/op    new time/op    delta
CodeDecoder-8    44.2ms ± 2%    40.6ms ± 1%  -8.11%  (p=0.000 n=10+10)

name           old speed      new speed      delta
CodeDecoder-8  43.9MB/s ± 2%  47.8MB/s ± 1%  +8.82%  (p=0.000 n=10+10)

For #16117

Change-Id: Id717e7fcd2f41b7d51d50c26ac167af45bae3747
Reviewed-on: https://go-review.googlesource.com/24433
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/reflect/type.go

index d60d0b060b278a5d4a317429de81e2fa692749ee..1c30608cefb692a293e178580da044ec63ebf88f 100644 (file)
@@ -820,6 +820,9 @@ func (t *rtype) NumMethod() int {
                tt := (*interfaceType)(unsafe.Pointer(t))
                return tt.NumMethod()
        }
+       if t.tflag&tflagUncommon == 0 {
+               return 0 // avoid methodCache lock in zero case
+       }
        return len(t.exportedMethods())
 }