-
Notifications
You must be signed in to change notification settings - Fork 17.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
database/sql: avoidable range elements copying in critical section. #20277
Comments
@FreeBirdLjj Do you have any benchmarks or timing that quantify this problem? |
I write a brief simple benchmark just test this if-block. func Benchmark_conn(b *testing.B) {
b.StopTimer()
ctx := context.Background()
db := new(DB)
db.freeConn = make([]*driverConn, b.N)
for i, _ := range db.freeConn {
db.freeConn[i] = &driverConn{createdAt: time.Now()}
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := db.conn(ctx, cachedOrNewConn)
if err != nil {
b.Fatal(err.Error())
}
}
} I ran it on my laptop, and the original implementation took a long time used up my patience (This benchmark is
I know it's rare that a server holds such a large number of |
The benchmark is not correct. It has to do |
@cznic Yeah, I know it's an extreme case. But there is also a If I understand correctly, this if-block is the fast-path of |
cc'ing @kardianos |
This isn't really a concern to me. Unless you can show that this even shows up in a real or even stress test of a real program, I'm not inclined to change this part of the code in Go 1. |
src/database/sql/sql.go
In my opinion, copying or moving a lot of elements is a little bit heavy in critical section. Here returning the last one can avoid the
copy()
invocation (but still needs resizing).The text was updated successfully, but these errors were encountered: