Skip to content

Commit 1bb3719

Browse files
committed
database/sql: reduce memory footprint with errors.New
“errors.New” is more efficient and uses less memory than “fmt.Errorf”. So use it for non-formatted strings.
1 parent adce7f1 commit 1bb3719

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

src/database/sql/convert_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package sql
66

77
import (
88
"database/sql/driver"
9+
"errors"
910
"fmt"
1011
"internal/asan"
1112
"reflect"
@@ -532,7 +533,7 @@ func (d *dec) Compose(form byte, negative bool, coefficient []byte, exponent int
532533
// This isn't strictly correct, as the extra bytes could be all zero,
533534
// ignore this for this test.
534535
if len(coefficient) > 16 {
535-
return fmt.Errorf("coefficient too large")
536+
return errors.New("coefficient too large")
536537
}
537538
copy(d.coefficient[:], coefficient)
538539

@@ -565,7 +566,7 @@ func (d *decFinite) Compose(form byte, negative bool, coefficient []byte, expone
565566
// This isn't strictly correct, as the extra bytes could be all zero,
566567
// ignore this for this test.
567568
if len(coefficient) > 16 {
568-
return fmt.Errorf("coefficient too large")
569+
return errors.New("coefficient too large")
569570
}
570571
copy(d.coefficient[:], coefficient)
571572

src/database/sql/driver/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package driver
66

77
import (
8+
"errors"
89
"fmt"
910
"reflect"
1011
"strconv"
@@ -171,7 +172,7 @@ type NotNull struct {
171172

172173
func (n NotNull) ConvertValue(v any) (Value, error) {
173174
if v == nil {
174-
return nil, fmt.Errorf("nil value not allowed")
175+
return nil, errors.New("nil value not allowed")
175176
}
176177
return n.Converter.ConvertValue(v)
177178
}
@@ -275,7 +276,7 @@ func (defaultConverter) ConvertValue(v any) (Value, error) {
275276
case reflect.Uint64:
276277
u64 := rv.Uint()
277278
if u64 >= 1<<63 {
278-
return nil, fmt.Errorf("uint64 values with high bit set are not supported")
279+
return nil, errors.New("uint64 values with high bit set are not supported")
279280
}
280281
return int64(u64), nil
281282
case reflect.Float32, reflect.Float64:

src/database/sql/sql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3366,7 +3366,7 @@ func (rs *Rows) Scan(dest ...any) error {
33663366
if rs.closemuScanHold {
33673367
// This should only be possible if the user calls Scan twice in a row
33683368
// without calling Next.
3369-
return fmt.Errorf("sql: Scan called without calling Next (closemuScanHold)")
3369+
return errors.New("sql: Scan called without calling Next (closemuScanHold)")
33703370
}
33713371

33723372
rs.closemu.RLock()

0 commit comments

Comments
 (0)