mirror of https://github.com/golang/go.git
reflect: avoid lock for some NumMethod()==0 cases
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>
This commit is contained in:
parent
f9d6b909b1
commit
3c6ed76da2
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue