diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2023-03-10 09:38:49 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2023-03-14 20:34:27 +0000 |
| commit | 08c32998297e66486416d4021630510eafdcd81e (patch) | |
| tree | 0d1631a348053d49763c4e515a2bb392f82b919e /src/database/sql/sql_test.go | |
| parent | 46bc9a6e1e94d0c7929a4835a3d163b721085329 (diff) | |
| download | go-08c32998297e66486416d4021630510eafdcd81e.tar.xz | |
database/sql: remove a distracting alloc, use atomic.Bool
This removes an allocation in Conn.grabConn that, while not super
important, was distracting me when optimizing code elsewhere.
While here, convert an atomic that was forgotten when this package was
earlier updated to use the new Go 1.19 typed atomics.
Change-Id: I4666256b4c0512e2162bd485c389130699f9d5ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/475415
Reviewed-by: David Crawshaw <crawshaw@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/database/sql/sql_test.go')
| -rw-r--r-- | src/database/sql/sql_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go index 8c58723c03..2b3d76f513 100644 --- a/src/database/sql/sql_test.go +++ b/src/database/sql/sql_test.go @@ -9,6 +9,8 @@ import ( "database/sql/driver" "errors" "fmt" + "internal/race" + "internal/testenv" "math/rand" "reflect" "runtime" @@ -4583,3 +4585,35 @@ func BenchmarkManyConcurrentQueries(b *testing.B) { } }) } + +func TestGrabConnAllocs(t *testing.T) { + testenv.SkipIfOptimizationOff(t) + if race.Enabled { + t.Skip("skipping allocation test when using race detector") + } + c := new(Conn) + ctx := context.Background() + n := int(testing.AllocsPerRun(1000, func() { + _, release, err := c.grabConn(ctx) + if err != nil { + t.Fatal(err) + } + release(nil) + })) + if n > 0 { + t.Fatalf("Conn.grabConn allocated %v objects; want 0", n) + } +} + +func BenchmarkGrabConn(b *testing.B) { + b.ReportAllocs() + c := new(Conn) + ctx := context.Background() + for i := 0; i < b.N; i++ { + _, release, err := c.grabConn(ctx) + if err != nil { + b.Fatal(err) + } + release(nil) + } +} |
