-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhancement: Avoid calling reflect.New() when passing in slice of values to Scan()
#5388
enhancement: Avoid calling reflect.New() when passing in slice of values to Scan()
#5388
Conversation
Relevant: #5372 |
user := *GetUser("scan", Config{}) | ||
DB.Create(&user) | ||
|
||
var u User |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var u User | |
b.ResetTimer() | |
var u User |
DB.Create(&user) | ||
} | ||
|
||
var u []User |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var u []User | |
b.ResetTimer() | |
var u []User |
DB.Raw("select * from users").Scan(&u) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func BenchmarkScanSlicePointer(b *testing.B) { | |
for i := 0; i < 10_000; i++ { | |
user := *GetUser(fmt.Sprintf("scan-%d", i), Config{}) | |
DB.Create(&user) | |
} | |
b.ResetTimer() | |
var u []*User | |
for x := 0; x < b.N; x++ { | |
DB.Raw("select * from users").Scan(&u) | |
} | |
} | |
My benchmark result suggests something promising.
|
@umitanuki Agreed, benchmark according to my benchmark, improvement is ~50%
|
New:
Old:
cc: @a631807682 |
@jinzhu when you have time, can you take a look? thank you! |
scan.go
Outdated
@@ -261,7 +262,11 @@ func Scan(rows Rows, db *DB, mode ScanMode) { | |||
} | |||
} | |||
} else { | |||
elem = reflect.New(reflectValueType) | |||
if isPtr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can change this to if isPtr && db.RowsAffected > 0
?
Seems not necessary to create a new struct for the first record.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jinzhu do you mean if isPtr && db.RowsAffected == 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no, you are correct, making this change now.
Just submitted a small review. In general looks great, thank you for your contribution. ❤️ |
What did this pull request do?
Tries to optimize
Scan()
method by recycling a struct when a user passes a slice of values to prevent callingreflect.New()
every time regardless of the type.User Case Description
When passing a slice of values, the run time is the same as passing in a slice of pointers. Users should not be penalized for such.