Skip to content

Commit

Permalink
only reset sqlite3 statement when necessary
Browse files Browse the repository at this point in the history
This commit changes the bind logic to only reset the sqlite3 statement
when necessary, which saves us a call to sqlite3_reset the first time
the query is used.

goos: darwin
goarch: arm64
pkg: github.com/charlievieth/go-sqlite3
cpu: Apple M4 Pro
                              │    y1.txt    │               y2.txt               │
                              │    sec/op    │   sec/op     vs base               │
Suite/BenchmarkQuery-14          1.718µ ± 1%   1.634µ ± 0%  -4.89% (p=0.000 n=10)
Suite/BenchmarkQuerySimple-14   1009.0n ± 2%   941.1n ± 0%  -6.73% (p=0.000 n=10)
geomean                          1.317µ        1.240µ       -5.81%

                              │   y1.txt   │               y2.txt                │
                              │    B/op    │    B/op     vs base                 │
Suite/BenchmarkQuery-14         640.0 ± 0%   640.0 ± 0%       ~ (p=1.000 n=10) ¹
Suite/BenchmarkQuerySimple-14   440.0 ± 0%   440.0 ± 0%       ~ (p=1.000 n=10) ¹
geomean                         530.7        530.7       +0.00%
¹ all samples are equal

                              │   y1.txt   │               y2.txt                │
                              │ allocs/op  │ allocs/op   vs base                 │
Suite/BenchmarkQuery-14         22.00 ± 0%   22.00 ± 0%       ~ (p=1.000 n=10) ¹
Suite/BenchmarkQuerySimple-14   13.00 ± 0%   13.00 ± 0%       ~ (p=1.000 n=10) ¹
geomean                         16.91        16.91       +0.00%
¹ all samples are equal
  • Loading branch information
charlievieth committed Dec 18, 2024
1 parent a77a2ce commit d533627
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ type SQLiteStmt struct {
s *C.sqlite3_stmt
closed bool
cls bool // True if the statement was created by SQLiteConn.Query
reset bool // True if the statement needs to reset before re-use
}

// SQLiteResult implements sql.Result.
Expand Down Expand Up @@ -2197,15 +2198,20 @@ func hasNamedArgs(args []driver.NamedValue) bool {
}

func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
rv := C.sqlite3_reset(s.s)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
return s.c.lastError()
if s.reset {
rv := C.sqlite3_reset(s.s)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
return s.c.lastError()
}
} else {
s.reset = true
}

if hasNamedArgs(args) {
return s.bindIndices(args)
}

var rv C.int
for _, arg := range args {
n := C.int(arg.Ordinal)
switch v := arg.Value.(type) {
Expand Down

0 comments on commit d533627

Please sign in to comment.