Skip to content

Commit

Permalink
Avoid an unnecessary slice allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Nov 13, 2016
1 parent c838446 commit 4caa2e8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 3 additions & 6 deletions object_goreflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,9 @@ func (r *Runtime) buildFieldInfo(t reflect.Type, index []int, info *reflectTypeI
}
}

idx := make([]int, 0, len(index)+1)
idx = append(idx, index...)
idx = append(idx, i)
idx := make([]int, len(index)+1)
copy(idx, index)
idx[len(idx)-1] = i
if _, exists := info.Fields[name]; !exists {
info.FieldNames = append(info.FieldNames, name)
}
Expand All @@ -427,9 +427,6 @@ func (r *Runtime) buildFieldInfo(t reflect.Type, index []int, info *reflectTypeI
Anonymous: field.Anonymous,
}
if field.Anonymous {
idx := make([]int, 0, len(index)+1)
idx = append(idx, index...)
idx = append(idx, i)
r.buildFieldInfo(field.Type, idx, info)
}
}
Expand Down
16 changes: 10 additions & 6 deletions object_goreflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,22 @@ func TestGoReflectCustomNaming(t *testing.T) {
})
}

type testGoReflectMethod_Bench struct {
field string
Test1, Test2, Test3, Test4, Test5, Test string
}

func BenchmarkGoReflectGet(b *testing.B) {
type parent struct {
field, Test1, Test2, Test3, Test4, Test5, Test string
}

type child struct {
parent
Test6 string
}

b.StopTimer()
vm := New()

b.StartTimer()
for i := 0; i < b.N; i++ {
v := vm.ToValue(testGoReflectMethod_O{Test: "Test"}).(*Object)
v := vm.ToValue(child{parent: parent{Test: "Test"}}).(*Object)
v.Get("Test")
}
}

0 comments on commit 4caa2e8

Please sign in to comment.