2
0

Add comments to explain the use of reflection after type assertion.

Removes one local variable, which is used twice only in an error.
This commit is contained in:
Simo Haasanen
2020-08-09 09:17:40 +01:00
parent b90570feb5
commit ec14212d30
24 changed files with 240 additions and 72 deletions
+10 -3
View File
@@ -31,6 +31,7 @@ func (dst *Float4Array) Set(src interface{}) error {
}
}
// Attempt to match to select common types:
switch value := src.(type) {
case []float32:
@@ -84,6 +85,9 @@ func (dst *Float4Array) Set(src interface{}) error {
}
}
default:
// Fallback to reflection if an optimised match was not found.
// The reflection is necessary for arrays and multidimensional slices,
// but it comes with a 20-50% performance penalty for large arrays/slices
reflectedValue := reflect.ValueOf(src)
if !reflectedValue.IsValid() || reflectedValue.IsZero() {
*dst = Float4Array{Status: Null}
@@ -189,6 +193,7 @@ func (src *Float4Array) AssignTo(dst interface{}) error {
switch src.Status {
case Present:
if len(src.Dimensions) == 1 {
// Attempt to match to select common types:
switch v := dst.(type) {
case *[]float32:
@@ -212,6 +217,9 @@ func (src *Float4Array) AssignTo(dst interface{}) error {
}
}
// Fallback to reflection if an optimised match was not found.
// The reflection is necessary for arrays and multidimensional slices,
// but it comes with a 20-50% performance penalty for large arrays/slices
value := reflect.ValueOf(dst)
if value.Kind() == reflect.Ptr {
value = value.Elem()
@@ -251,9 +259,8 @@ func (src *Float4Array) assignToRecursive(value reflect.Value, index, dimension
length := int(src.Dimensions[dimension].Length)
if reflect.Array == kind {
typ := value.Type()
typLen := typ.Len()
if typLen != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typLen)
if typ.Len() != length {
return 0, errors.Errorf("expected size %d array, but %s has size %d array", length, typ, typ.Len())
}
value.Set(reflect.New(typ).Elem())
} else {