Skip to content
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

Cache fields #2

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.19.x', '1.20.x', '1.21.x', '1.22.x']
go-version: [ '1.20.x', '1.21.x', '1.22.x']

steps:
- uses: actions/checkout@v4
Expand Down
90 changes: 90 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package pgx_collect_test

import (
"database/sql"
"testing"
"time"

pgxc "github.com/zolstein/pgx-collect"
pgxc_internal "github.com/zolstein/pgx-collect/internal"
. "github.com/zolstein/pgx-collect/internal/testutils"
)

func BenchmarkInitialize(b *testing.B) {
type Person struct {
Name string
Age sql.NullInt64
}

type Record struct {
ID int64
Person
CreatedAt time.Time
}

rows := MakeMockRows("id,name,age,created_at", nil)

b.Run("cold", func(b *testing.B) {
b.Run("struct-by-pos", func(b *testing.B) {
var rowTo pgxc.RowSpec[Record] = pgxc.RowToStructByPos[Record]
scanner := rowTo.Scanner()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pgxc_internal.ClearStructFieldCaches()
scanner.Initialize(rows)
}
})

b.Run("struct-by-name", func(b *testing.B) {
var rowTo pgxc.RowSpec[Record] = pgxc.RowToStructByName[Record]
scanner := rowTo.Scanner()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pgxc_internal.ClearStructFieldCaches()
scanner.Initialize(rows)
}
})

b.Run("struct-by-name-lax", func(b *testing.B) {
var rowTo pgxc.RowSpec[Record] = pgxc.RowToStructByNameLax[Record]
scanner := rowTo.Scanner()
b.ResetTimer()
for i := 0; i < b.N; i++ {
pgxc_internal.ClearStructFieldCaches()
scanner.Initialize(rows)
}
})
})

b.Run("hot", func(b *testing.B) {
b.Run("struct-by-pos", func(b *testing.B) {
var rowTo pgxc.RowSpec[Record] = pgxc.RowToStructByPos[Record]
scanner := rowTo.Scanner()
scanner.Initialize(rows) // First time to populate cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
scanner.Initialize(rows)
}
})

b.Run("struct-by-name", func(b *testing.B) {
var rowTo pgxc.RowSpec[Record] = pgxc.RowToStructByName[Record]
scanner := rowTo.Scanner()
scanner.Initialize(rows) // First time to populate cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
scanner.Initialize(rows)
}
})

b.Run("struct-by-name-lax", func(b *testing.B) {
var rowTo pgxc.RowSpec[Record] = pgxc.RowToStructByNameLax[Record]
scanner := rowTo.Scanner()
scanner.Initialize(rows) // First time to populate cache
b.ResetTimer()
for i := 0; i < b.N; i++ {
scanner.Initialize(rows)
}
})
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zolstein/pgx-collect

go 1.19
go 1.20

require (
github.com/jackc/pgx/v5 v5.5.5
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZ
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -26,12 +24,10 @@ github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading
Loading