Skip to content

Commit

Permalink
Tiny tune: struct layout optimization (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu authored Nov 12, 2022
1 parent 87fe307 commit 8536df8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.17
id: go

- name: Check out code into the Go module directory
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/linxGnu/mssqlx

go 1.17

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.6
github.com/stretchr/testify v1.8.0
github.com/lib/pq v1.10.7
github.com/stretchr/testify v1.8.1
)

require (
Expand Down
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
17 changes: 7 additions & 10 deletions mssqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ func ping(w *wrapper) (err error) {

// DBs sqlx wrapper supports querying master-slave database connections for HA and scalability, auto-balancer integrated.
type DBs struct {
masters *balancer
slaves *balancer
all *balancer
driverName string
_masters []*wrapper
_slaves []*wrapper
_all []*wrapper
readQuerySource ReadQuerySource

masters *balancer
slaves *balancer
all *balancer

_masters []*wrapper
_slaves []*wrapper
_all []*wrapper
}

// DriverName returns the driverName passed to the Open function for this DB.
Expand Down Expand Up @@ -442,7 +440,7 @@ func _mapperFunc(target []*wrapper, mf func(string) string) {
for ind, db := range target {
if db != nil {
wg.Add(1)
go func(db *wrapper, ind int) {
go func(db *wrapper, _ int) {
db.db.MapperFunc(mf)
wg.Done()
}(db, ind)
Expand Down Expand Up @@ -1493,7 +1491,6 @@ func ConnectMasterSlaves(driverName string, masterDSNs []string, slaveDSNs []str

dbs._all[eId] = dbs._masters[mId]
dbs.all.add(dbs._masters[mId])

}(i, n)
n++
}
Expand Down
61 changes: 30 additions & 31 deletions mssqlx_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// The following environment variables, if set, will be used:
//
// * MSSQLX_POSTGRES_DSN
// * MSSQLX_MYSQL_DSN
// * MSSQLX_SQLITE_DSN
// - MSSQLX_POSTGRES_DSN
// - MSSQLX_MYSQL_DSN
// - MSSQLX_SQLITE_DSN
//
// Set any of these variables to 'skip' to skip them. Note that for MySQL,
// the string '?parseTime=True' will be appended to the DSN if it's not there
// already.
//
package mssqlx

import (
Expand All @@ -27,11 +26,15 @@ import (
"github.com/stretchr/testify/require"
)

var TestWPostgres = false // test with postgres?
var TestWMysql = false // test with mysql?
var (
TestWPostgres = false // test with postgres?
TestWMysql = false // test with mysql?
)

var myDBs *DBs
var pgDBs *DBs
var (
myDBs *DBs
pgDBs *DBs
)

func init() {
ConnectMasterSlave()
Expand Down Expand Up @@ -133,10 +136,10 @@ drop table employees;
}

type Person struct {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
AddedAt time.Time `db:"added_at"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
}

type Place struct {
Expand All @@ -146,8 +149,8 @@ type Place struct {
}

type PlacePtr struct {
Country string
City *string
Country string
TelCode int
}

Expand All @@ -169,9 +172,9 @@ type EmbedConflict struct {
type SliceMember struct {
Country string
City sql.NullString
TelCode int
People []Person `db:"-"`
Addresses []Place `db:"-"`
TelCode int
}

func MultiExec(e sqlx.Execer, query string) {
Expand Down Expand Up @@ -566,7 +569,7 @@ func TestMissingName(t *testing.T) {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
//AddedAt time.Time `db:"added_at"`
// AddedAt time.Time `db:"added_at"`
}

// test Select first
Expand Down Expand Up @@ -878,8 +881,7 @@ func TestSelectSliceMapTimes(t *testing.T) {
t.Fatal(err)
}
for rows.Next() {
_, err := sqlx.SliceScan(rows)
if err != nil {
if _, err = sqlx.SliceScan(rows); err != nil {
t.Error(err)
}
}
Expand All @@ -895,7 +897,6 @@ func TestSelectSliceMapTimes(t *testing.T) {
t.Error(err)
}
}

})
}

Expand Down Expand Up @@ -951,7 +952,7 @@ func TestNilReceivers(t *testing.T) {
}

func TestNamedQueries(t *testing.T) {
var schema = Schema{
schema := Schema{
create: `
CREATE TABLE place (
id integer PRIMARY KEY,
Expand Down Expand Up @@ -983,10 +984,10 @@ func TestNamedQueries(t *testing.T) {

_RunWithSchema(schema, t, func(db *DBs, t *testing.T) {
type Person struct {
AddedAt *time.Time `db:"added_at"`
FirstName sql.NullString `db:"first_name"`
LastName sql.NullString `db:"last_name"`
Email sql.NullString
AddedAt *time.Time `db:"added_at"`
}

// BindNamed
Expand Down Expand Up @@ -1096,8 +1097,8 @@ func TestNamedQueries(t *testing.T) {

// Test nested structs
type Place struct {
ID int `db:"id"`
Name sql.NullString `db:"name"`
ID int `db:"id"`
}
type PlacePerson struct {
FirstName sql.NullString `db:"first_name"`
Expand Down Expand Up @@ -1183,7 +1184,7 @@ func TestNamedQueries(t *testing.T) {
}

func TestNilInsert(t *testing.T) {
var schema = Schema{
schema := Schema{
create: `
CREATE TABLE tt (
id integer,
Expand All @@ -1194,8 +1195,8 @@ func TestNilInsert(t *testing.T) {

_RunWithSchema(schema, t, func(db *DBs, t *testing.T) {
type TT struct {
ID int
Value *string
ID int
}
var v, v2 TT
r := db.Rebind
Expand Down Expand Up @@ -1231,7 +1232,7 @@ func TestNilInsert(t *testing.T) {
}

func TestScanErrors(t *testing.T) {
var schema = Schema{
schema := Schema{
create: `
CREATE TABLE kv (
k text,
Expand All @@ -1242,8 +1243,8 @@ func TestScanErrors(t *testing.T) {

_RunWithSchema(schema, t, func(db *DBs, t *testing.T) {
type WrongTypes struct {
K int
V string
K int
}
_, err := db.Exec(db.Rebind("INSERT INTO kv (k, v) VALUES (?, ?)"), "hi", 1)
if err != nil {
Expand Down Expand Up @@ -1351,7 +1352,7 @@ func TestUsages(t *testing.T) {
if err != nil {
t.Error(err)
}
//fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])
// fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])

// if you have null fields and use SELECT *, you must use sql.Null* in your struct
// this test also verifies that you can use either a []Struct{} or a []*Struct{}
Expand Down Expand Up @@ -1698,7 +1699,7 @@ func TestUsages(t *testing.T) {
}

func Test_EmbeddedLiterals(t *testing.T) {
var schema = Schema{
schema := Schema{
create: `
CREATE TABLE x (
k text
Expand All @@ -1711,10 +1712,8 @@ func Test_EmbeddedLiterals(t *testing.T) {
K *string
}
type t2 struct {
Inline struct {
F string
}
K *string
K *string
Inline struct{ F string }
}

_, err := db.Exec(db.Rebind("INSERT INTO x (k) VALUES (?), (?), (?);"), "one", "two", "three")
Expand Down Expand Up @@ -1753,7 +1752,7 @@ func Test_ErrBadConnChecker(t *testing.T) {
}

func TestStressQueries(t *testing.T) {
var schema = Schema{
schema := Schema{
create: `
CREATE TABLE stress (
k text,
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const (
)

type clusterOptions struct {
isWsrep bool
readQuerySource ReadQuerySource
instantiate Instantiate
readQuerySource ReadQuerySource
isWsrep bool
}

// Option setter.
Expand Down

0 comments on commit 8536df8

Please sign in to comment.