Skip to content

Commit 788e038

Browse files
committed
reflect: make all flag.mustBe* methods inlinable
mustBe was barely over budget, so manually inlining the first flag.kind call is enough. Add a TODO to reverse that in the future, once the compiler gets better. mustBeExported and mustBeAssignable were over budget by a larger amount, so add slow path functions instead. This is the same strategy used in the sync package for common methods like Once.Do, for example. Lots of exported reflect.Value methods call these assert-like unexported methods, so avoiding the function call overhead in the common case does shave off a percent from most exported APIs. Finally, add the methods to TestIntendedInlining. While at it, replace a couple of uses of the 0 Kind with its descriptive name, Invalid. name old time/op new time/op delta Call-8 68.0ns ± 1% 66.8ns ± 1% -1.81% (p=0.000 n=10+9) PtrTo-8 8.00ns ± 2% 7.83ns ± 0% -2.19% (p=0.000 n=10+9) Updates #7818. Change-Id: Ic1603b640519393f6b50dd91ec3767753eb9e761 Reviewed-on: https://go-review.googlesource.com/c/go/+/166462 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent cc5dc00 commit 788e038

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/cmd/compile/internal/gc/inl_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,11 @@ func TestIntendedInlining(t *testing.T) {
133133
"Value.pointer",
134134
"add",
135135
"align",
136+
"flag.mustBe",
137+
"flag.mustBeAssignable",
138+
"flag.mustBeExported",
136139
"flag.kind",
137140
"flag.ro",
138-
139-
// TODO: these use panic, which gets their budgets
140-
// slightly over the limit
141-
// "flag.mustBe",
142-
// "flag.mustBeAssignable",
143-
// "flag.mustBeExported",
144141
},
145142
"regexp": {
146143
"(*bitState).push",

src/reflect/value.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,23 @@ type nonEmptyInterface struct {
203203
// v.flag.mustBe(Bool), which will only bother to copy the
204204
// single important word for the receiver.
205205
func (f flag) mustBe(expected Kind) {
206-
if f.kind() != expected {
206+
// TODO(mvdan): use f.kind() again once mid-stack inlining gets better
207+
if Kind(f&flagKindMask) != expected {
207208
panic(&ValueError{methodName(), f.kind()})
208209
}
209210
}
210211

211212
// mustBeExported panics if f records that the value was obtained using
212213
// an unexported field.
213214
func (f flag) mustBeExported() {
215+
if f == 0 || f&flagRO != 0 {
216+
f.mustBeExportedSlow()
217+
}
218+
}
219+
220+
func (f flag) mustBeExportedSlow() {
214221
if f == 0 {
215-
panic(&ValueError{methodName(), 0})
222+
panic(&ValueError{methodName(), Invalid})
216223
}
217224
if f&flagRO != 0 {
218225
panic("reflect: " + methodName() + " using value obtained using unexported field")
@@ -223,6 +230,12 @@ func (f flag) mustBeExported() {
223230
// which is to say that either it was obtained using an unexported field
224231
// or it is not addressable.
225232
func (f flag) mustBeAssignable() {
233+
if f&flagRO != 0 || f&flagAddr == 0 {
234+
f.mustBeAssignableSlow()
235+
}
236+
}
237+
238+
func (f flag) mustBeAssignableSlow() {
226239
if f == 0 {
227240
panic(&ValueError{methodName(), Invalid})
228241
}
@@ -981,7 +994,7 @@ func (v Value) Interface() (i interface{}) {
981994

982995
func valueInterface(v Value, safe bool) interface{} {
983996
if v.flag == 0 {
984-
panic(&ValueError{"reflect.Value.Interface", 0})
997+
panic(&ValueError{"reflect.Value.Interface", Invalid})
985998
}
986999
if safe && v.flag&flagRO != 0 {
9871000
// Do not allow access to unexported values via Interface,

0 commit comments

Comments
 (0)