Skip to content

Commit 7bb00ba

Browse files
authored
Remove CanInterface() checks (#264)
1 parent 1cec03a commit 7bb00ba

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

.github/workflows/diff.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ jobs:
1212
uses: actions/setup-go@v2
1313
with:
1414
go-version: 1.19
15-
- uses: actions/checkout@v2
1615
- name: Install benchstat
1716
run: go install golang.org/x/perf/cmd/benchstat@latest
1817

19-
- name: Benchmark
20-
run: go test -bench=. -benchmem -run=^$ -count=20 -timeout=30m | tee new.txt
18+
- uses: actions/checkout@v2
19+
- name: Benchmark new code
20+
run: go test -bench=. -benchmem -run=^$ -count=20 -timeout=30m | tee /tmp/new.txt
2121

2222
- name: Checkout master
2323
uses: actions/checkout@v2
2424
with:
2525
ref: master
26-
- name: Benchmark
27-
run: go test -bench=. -benchmem -run=^$ -count=20 -timeout=30m | tee old.txt
26+
- name: Benchmark master
27+
run: go test -bench=. -benchmem -run=^$ -count=20 -timeout=30m | tee /tmp/old.txt
2828

2929
- name: Diff
30-
run: benchstat old.txt new.txt
30+
run: benchstat /tmp/old.txt /tmp/new.txt

bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func Benchmark_filter(b *testing.B) {
8282
Items: make([]Item, 100),
8383
}
8484
for i := 1; i <= 100; i++ {
85-
env.Items[i-1] = Item{Value: i}
85+
env.Items[i-1].Value = i
8686
}
8787

8888
var out interface{}

vm/runtime/runtime.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func Fetch(from, i interface{}) interface{} {
1818
// Methods can be defined on any type.
1919
if v.NumMethod() > 0 {
2020
method := v.MethodByName(i.(string))
21-
if method.IsValid() && method.CanInterface() {
21+
if method.IsValid() {
2222
return method.Interface()
2323
}
2424
}
@@ -35,16 +35,14 @@ func Fetch(from, i interface{}) interface{} {
3535

3636
case reflect.Array, reflect.Slice, reflect.String:
3737
value := v.Index(ToInt(i))
38-
if value.IsValid() && value.CanInterface() {
38+
if value.IsValid() {
3939
return value.Interface()
4040
}
4141

4242
case reflect.Map:
4343
value := v.MapIndex(reflect.ValueOf(i))
4444
if value.IsValid() {
45-
if value.CanInterface() {
46-
return value.Interface()
47-
}
45+
return value.Interface()
4846
} else {
4947
elem := reflect.TypeOf(from).Elem()
5048
return reflect.Zero(elem).Interface()
@@ -59,7 +57,7 @@ func Fetch(from, i interface{}) interface{} {
5957
}
6058
return name == fieldName
6159
})
62-
if value.IsValid() && value.CanInterface() {
60+
if value.IsValid() {
6361
return value.Interface()
6462
}
6563
}
@@ -87,7 +85,7 @@ func FetchField(from interface{}, field *Field) interface{} {
8785
// v.FieldByIndex() function as we don't need to verify what a field
8886
// is a struct as we already did it on compilation step.
8987
value := fieldByIndex(v, field.Index)
90-
if value.IsValid() && value.CanInterface() {
88+
if value.IsValid() {
9189
return value.Interface()
9290
}
9391
}
@@ -124,7 +122,7 @@ func FetchMethod(from interface{}, method *Method) interface{} {
124122
if kind != reflect.Invalid {
125123
// Methods can be defined on any type, no need to dereference.
126124
method := v.Method(method.Index)
127-
if method.IsValid() && method.CanInterface() {
125+
if method.IsValid() {
128126
return method.Interface()
129127
}
130128
}
@@ -157,7 +155,7 @@ func Deref(i interface{}) interface{} {
157155
}
158156
}
159157

160-
if v.IsValid() && v.CanInterface() {
158+
if v.IsValid() {
161159
return v.Interface()
162160
}
163161

@@ -180,13 +178,13 @@ func Slice(array, from, to interface{}) interface{} {
180178
}
181179

182180
value := v.Slice(a, b)
183-
if value.IsValid() && value.CanInterface() {
181+
if value.IsValid() {
184182
return value.Interface()
185183
}
186184

187185
case reflect.Ptr:
188186
value := v.Elem()
189-
if value.IsValid() && value.CanInterface() {
187+
if value.IsValid() {
190188
return Slice(value.Interface(), from, to)
191189
}
192190

@@ -205,7 +203,7 @@ func In(needle interface{}, array interface{}) bool {
205203
case reflect.Array, reflect.Slice:
206204
for i := 0; i < v.Len(); i++ {
207205
value := v.Index(i)
208-
if value.IsValid() && value.CanInterface() {
206+
if value.IsValid() {
209207
if Equal(value.Interface(), needle).(bool) {
210208
return true
211209
}
@@ -237,7 +235,7 @@ func In(needle interface{}, array interface{}) bool {
237235

238236
case reflect.Ptr:
239237
value := v.Elem()
240-
if value.IsValid() && value.CanInterface() {
238+
if value.IsValid() {
241239
return In(needle, value.Interface())
242240
}
243241
return false

0 commit comments

Comments
 (0)