diff options
author | Joe Tsai <joetsai@digital-static.net> | 2022-04-16 20:23:28 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-04-18 22:14:50 +0000 |
commit | c5edd5f616b4ee4bbaefdb1579c6078e7ed7e84e (patch) | |
tree | 32df92d62203cd08f36dc643530f2ff3e0ef4aee /src/reflect/value.go | |
parent | f49e802892a225c7fd14a3a8bb8c0e83875d888d (diff) | |
download | go-c5edd5f616b4ee4bbaefdb1579c6078e7ed7e84e.tar.gz go-c5edd5f616b4ee4bbaefdb1579c6078e7ed7e84e.zip |
reflect: make Value.MapRange inlineable
This allows the caller to decide whether MapIter should be
stack allocated or heap allocated based on whether it escapes.
In most cases, it does not escape and thus removes the utility
of MapIter.Reset (#46293). In fact, use of sync.Pool with MapIter
and calling MapIter.Reset is likely to be slower.
Change-Id: Ic93e7d39e5dd4c83e7fca9e0bdfbbcd70777f0e1
Reviewed-on: https://go-review.googlesource.com/c/go/+/400675
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/reflect/value.go')
-rw-r--r-- | src/reflect/value.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go index 06f0469ede..6fe3cee017 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -1833,10 +1833,20 @@ func (iter *MapIter) Reset(v Value) { // ... // } func (v Value) MapRange() *MapIter { - v.mustBe(Map) + // This is inlinable to take advantage of "function outlining". + // The allocation of MapIter can be stack allocated if the caller + // does not allow it to escape. + // See https://blog.filippo.io/efficient-go-apis-with-the-inliner/ + if v.kind() != Map { + v.panicNotMap() + } return &MapIter{m: v} } +func (f flag) panicNotMap() { + f.mustBe(Map) +} + // copyVal returns a Value containing the map key or value at ptr, // allocating a new variable as needed. func copyVal(typ *rtype, fl flag, ptr unsafe.Pointer) Value { |