Commit 3c6ed76d authored by David Crawshaw's avatar David Crawshaw

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/24433Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent f9d6b909
......@@ -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())
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment