Преглед изворни кода

Avoid an unnecessary slice allocation.

Dmitry Panov пре 9 година
родитељ
комит
4caa2e8278
2 измењених фајлова са 13 додато и 12 уклоњено
  1. 3 6
      object_goreflect.go
  2. 10 6
      object_goreflect_test.go

+ 3 - 6
object_goreflect.go

@@ -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)
 		}
@@ -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)
 		}
 	}

+ 10 - 6
object_goreflect_test.go

@@ -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")
 	}
 }