fix: reduce allocations when slice of values

This commit is contained in:
Melbex De Leon 2022-05-30 22:32:10 +08:00
parent 93986de8e4
commit 90cceca6e7
No known key found for this signature in database
GPG Key ID: 9A664266F44E5B74

View File

@ -237,6 +237,7 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
switch reflectValue.Kind() { switch reflectValue.Kind() {
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
var elem reflect.Value var elem reflect.Value
recyclableStruct := reflect.New(reflectValueType)
if !update || reflectValue.Len() == 0 { if !update || reflectValue.Len() == 0 {
update = false update = false
@ -261,7 +262,11 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
} }
} }
} else { } else {
if isPtr {
elem = reflect.New(reflectValueType) elem = reflect.New(reflectValueType)
} else {
elem = recyclableStruct
}
} }
db.scanIntoStruct(rows, elem, values, fields, joinFields) db.scanIntoStruct(rows, elem, values, fields, joinFields)