Skip to content

Commit 75a7ebf

Browse files
committed
fixes jmoiron#688
The changes in jmoiron#635 changed the some of the output types of In to pointers. This takes less time but it also changed the types in the output of In in a way that I think is more aggressive than I would have preferred. I'm also not 100% convinced that using pointers to types like `int` and `string` would provide an overall performance benefit when you factor in GC. Despite that, timings did get worse: pre-change: ``` BenchmarkIn-4 3136129 376 ns/op 272 B/op 4 allocs/op BenchmarkIn1k-4 171588 6602 ns/op 19488 B/op 3 allocs/op BenchmarkIn1kInt-4 157549 7502 ns/op 19488 B/op 3 allocs/op BenchmarkIn1kString-4 155502 7604 ns/op 19488 B/op 3 allocs/op ``` post-change: ``` BenchmarkIn-4 3007132 396 ns/op 272 B/op 4 allocs/op BenchmarkIn1k-4 175978 6768 ns/op 19488 B/op 3 allocs/op BenchmarkIn1kInt-4 120422 10125 ns/op 19488 B/op 3 allocs/op BenchmarkIn1kString-4 108813 10755 ns/op 19488 B/op 3 allocs/op ``` I'd prefer to keep `[]int{..}` producing ints instead of `*int` even if it means losing ~25% of perf on these special cased functions.
1 parent 4cb7f7d commit 75a7ebf

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

bind.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interfa
249249
args = append(args, val...)
250250
case []int:
251251
for i := range val {
252-
args = append(args, &val[i])
252+
args = append(args, val[i])
253253
}
254254
case []string:
255255
for i := range val {
256-
args = append(args, &val[i])
256+
args = append(args, val[i])
257257
}
258258
default:
259259
for si := 0; si < vlen; si++ {

sqlx_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,3 +1852,43 @@ func BenchmarkRebindBuffer(b *testing.B) {
18521852
rebindBuff(DOLLAR, q2)
18531853
}
18541854
}
1855+
1856+
func TestIn130Regression(t *testing.T) {
1857+
t.Run("[]interface{}{}", func(t *testing.T) {
1858+
q, args, err := In("SELECT * FROM people WHERE name IN (?)", []interface{}{[]string{"gopher"}}...)
1859+
if err != nil {
1860+
t.Fatal(err)
1861+
}
1862+
if q != "SELECT * FROM people WHERE name IN (?)" {
1863+
t.Errorf("got=%v", q)
1864+
}
1865+
t.Log(args)
1866+
for _, a := range args {
1867+
switch a.(type) {
1868+
case string:
1869+
t.Log("ok: string", a)
1870+
case *string:
1871+
t.Error("ng: string pointer", a, *a.(*string))
1872+
}
1873+
}
1874+
})
1875+
1876+
t.Run("[]string{}", func(t *testing.T) {
1877+
q, args, err := In("SELECT * FROM people WHERE name IN (?)", []string{"gopher"})
1878+
if err != nil {
1879+
t.Fatal(err)
1880+
}
1881+
if q != "SELECT * FROM people WHERE name IN (?)" {
1882+
t.Errorf("got=%v", q)
1883+
}
1884+
t.Log(args)
1885+
for _, a := range args {
1886+
switch a.(type) {
1887+
case string:
1888+
t.Log("ok: string", a)
1889+
case *string:
1890+
t.Error("ng: string pointer", a, *a.(*string))
1891+
}
1892+
}
1893+
})
1894+
}

0 commit comments

Comments
 (0)