]> Cypherpunks.ru repositories - gostls13.git/commit
reflect: optimize Value.IsZero
authorJoe Tsai <joetsai@digital-static.net>
Fri, 10 Jun 2022 01:32:53 +0000 (18:32 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Wed, 24 Aug 2022 08:17:52 +0000 (08:17 +0000)
commit1ab6b790be3de2364edc3c15741afe3705f7c358
tree24cb67c0ff43db61f3b0975e3d6bdc2ef7ba6094
parent1a8dfadbfe26978cba0d6ce57bf437a93f796da6
reflect: optimize Value.IsZero

If a struct or array is comparable, then we can leverage rtype.equal,
which is almost always faster than what Go reflection can achieve.

As a secondary optimization, pre-compute Value.Len and Value.NumField
outside of the loop conditional.

Performance:

name                       old time/op  new time/op  delta
IsZero/ArrayComparable      136ns ± 4%    16ns ± 1%  -88.28%  (p=0.008 n=5+5)
IsZero/ArrayIncomparable    197ns ±10%   123ns ± 1%  -37.74%  (p=0.008 n=5+5)
IsZero/StructComparable    26.4ns ± 0%   9.6ns ± 1%  -63.68%  (p=0.016 n=4+5)
IsZero/StructIncomparable  43.5ns ± 1%  27.8ns ± 1%  -36.21%  (p=0.008 n=5+5)

The incomparable types gain a performance boost since
they are generally constructed from nested comparable types.

Change-Id: If2c1929f8bb1b5b19306ef0c69f3c95a27d4b60d
Reviewed-on: https://go-review.googlesource.com/c/go/+/411478
Reviewed-by: Dan Kortschak <dan@kortschak.io>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
src/reflect/all_test.go
src/reflect/value.go