]> Cypherpunks repositories - gostls13.git/commit
reflect: optimize TypeFor for non-interface types
authorJoe Tsai <joetsai@digital-static.net>
Sat, 13 Jan 2024 02:13:14 +0000 (18:13 -0800)
committerGopher Robot <gobot@golang.org>
Mon, 12 Feb 2024 14:38:07 +0000 (14:38 +0000)
commit2413629aa8a2737fb0343d8bc9107208ae7d643f
treea0f4118c8b2d40d6a73697a7934f7532c70a7de6
parent2b58355ef624239dbe32185dc8dfc9d1074615c6
reflect: optimize TypeFor for non-interface types

The reflect.Type.Elem method is somewhat slow,
which is unfortunate since the reflect.TypeOf((*T)(nil)).Elem()
trick is only needed if T is an interface.

Optimize for concrete types by doing the faster reflect.TypeOf(v)
call first and only falling back on the Elem method if needed.

Performance:

name              old time/op  new time/op  delta
TypeForString-24  9.10ns ± 1%  1.78ns ± 2%  -80.49%  (p=0.000 n=10+10)
TypeForError-24   9.55ns ± 1%  9.78ns ± 1%   +2.39%  (p=0.000 n=10+9)

Updates #60088

Change-Id: I2ae76988c9a3dbcbae10d2c19b55db3c8d4559bf
Reviewed-on: https://go-review.googlesource.com/c/go/+/555597
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
src/reflect/type.go
src/reflect/type_test.go