Skip to content

Commit 0461cba

Browse files
committed
sqlite: remove strings.EqualFold from per-row processing
│ before │ after │ │ sec/op │ sec/op vs base │ QueryRows100MixedTypes-8 37.75µ ± 1% 36.88µ ± 1% -2.29% (p=0.000 n=10) │ before │ after │ │ B/op │ B/op vs base │ QueryRows100MixedTypes-8 4.281Ki ± 0% 4.281Ki ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ before │ after │ │ allocs/op │ allocs/op vs base │ QueryRows100MixedTypes-8 207.0 ± 0% 207.0 ± 0% ~ (p=1.000 n=10) ¹ Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 7108799 commit 0461cba

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

sqlite.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ type stmt struct {
375375

376376
// filled on first step only if persist==true
377377
colTypes []sqliteh.ColumnType
378-
colDeclTypes []string
378+
colDeclTypes []colDeclType
379379
colNames []string
380380
}
381381

@@ -607,6 +607,26 @@ func (s *stmt) bindBasic(debugName any, ordinal int, v any) (found bool, err err
607607
}
608608
}
609609

610+
// colDeclType is whether and how the declared SQLite column type should
611+
// map to any special handling (as a date, or as a boolean, etc).
612+
type colDeclType byte
613+
614+
const (
615+
declTypeUnknown colDeclType = iota
616+
declTypeDateOrTime
617+
declTypeBoolean
618+
)
619+
620+
func colDeclTypeFromString(s string) colDeclType {
621+
if strings.EqualFold(s, "DATETIME") || strings.EqualFold(s, "DATE") {
622+
return declTypeDateOrTime
623+
}
624+
if strings.EqualFold(s, "BOOLEAN") {
625+
return declTypeBoolean
626+
}
627+
return declTypeUnknown
628+
}
629+
610630
type rows struct {
611631
stmt *stmt
612632
closed bool
@@ -615,7 +635,7 @@ type rows struct {
615635

616636
// Filled on first call to Next.
617637
colTypes []sqliteh.ColumnType
618-
colDeclTypes []string
638+
colDeclTypes []colDeclType
619639
}
620640

621641
func (r *rows) Columns() []string {
@@ -668,10 +688,10 @@ func (r *rows) Next(dest []driver.Value) error {
668688
} else {
669689
colCount := r.stmt.stmt.ColumnCount()
670690
r.colTypes = make([]sqliteh.ColumnType, colCount)
671-
r.colDeclTypes = make([]string, colCount)
691+
r.colDeclTypes = make([]colDeclType, colCount)
672692
for i := range r.colTypes {
673693
r.colTypes[i] = r.stmt.stmt.ColumnType(i)
674-
r.colDeclTypes[i] = r.stmt.stmt.ColumnDeclType(i)
694+
r.colDeclTypes[i] = colDeclTypeFromString(r.stmt.stmt.ColumnDeclType(i))
675695
}
676696
if r.stmt.persist {
677697
r.stmt.colTypes = r.colTypes
@@ -681,7 +701,7 @@ func (r *rows) Next(dest []driver.Value) error {
681701
}
682702

683703
for i := range dest {
684-
if strings.EqualFold(r.colDeclTypes[i], "DATETIME") || strings.EqualFold(r.colDeclTypes[i], "DATE") {
704+
if r.colDeclTypes[i] == declTypeDateOrTime {
685705
switch r.colTypes[i] {
686706
case sqliteh.SQLITE_INTEGER:
687707
v := r.stmt.stmt.ColumnInt64(i)
@@ -712,7 +732,7 @@ func (r *rows) Next(dest []driver.Value) error {
712732
switch r.colTypes[i] {
713733
case sqliteh.SQLITE_INTEGER:
714734
val := r.stmt.stmt.ColumnInt64(i)
715-
if strings.EqualFold(r.colDeclTypes[i], "BOOLEAN") {
735+
if r.colDeclTypes[i] == declTypeBoolean {
716736
dest[i] = val > 0
717737
} else {
718738
dest[i] = val

0 commit comments

Comments
 (0)