diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2014-01-10 12:19:36 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2014-01-10 12:19:36 -0800 |
| commit | 258ed3f2265a41a46e936e884d8afd6e6f646973 (patch) | |
| tree | 3b57fa17c1c5e297a2c676804009709b9ea14bb8 /src/pkg/database/sql/convert.go | |
| parent | 3b8dfc32b3e75f128fa896c1bb9a70fca2da6dcb (diff) | |
| download | go-258ed3f2265a41a46e936e884d8afd6e6f646973.tar.xz | |
database/sql: avoiding fmt.Sprintf while scanning, avoid allocs with RawBytes
A user reported heavy contention on fmt's printer cache. Avoid
fmt.Sprint. We have to do reflection anyway, and there was
already an asString function to use strconv, so use it.
This CL also eliminates a redundant allocation + copy when
scanning into *[]byte (avoiding the intermediate string)
and avoids an extra alloc when assigning to a caller's RawBytes
(trying to reuse the caller's memory).
Fixes #7086
R=golang-codereviews, nightlyone
CC=golang-codereviews
https://golang.org/cl/50240044
Diffstat (limited to 'src/pkg/database/sql/convert.go')
| -rw-r--r-- | src/pkg/database/sql/convert.go | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/src/pkg/database/sql/convert.go b/src/pkg/database/sql/convert.go index c04adde1fc..c0b38a2494 100644 --- a/src/pkg/database/sql/convert.go +++ b/src/pkg/database/sql/convert.go @@ -160,27 +160,19 @@ func convertAssign(dest, src interface{}) error { reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64: - *d = fmt.Sprintf("%v", src) + *d = asString(src) return nil } case *[]byte: sv = reflect.ValueOf(src) - switch sv.Kind() { - case reflect.Bool, - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Float32, reflect.Float64: - *d = []byte(fmt.Sprintf("%v", src)) + if b, ok := asBytes(nil, sv); ok { + *d = b return nil } case *RawBytes: sv = reflect.ValueOf(src) - switch sv.Kind() { - case reflect.Bool, - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Float32, reflect.Float64: - *d = RawBytes(fmt.Sprintf("%v", src)) + if b, ok := asBytes([]byte(*d)[:0], sv); ok { + *d = RawBytes(b) return nil } case *bool: @@ -271,5 +263,37 @@ func asString(src interface{}) string { case []byte: return string(v) } + rv := reflect.ValueOf(src) + switch rv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return strconv.FormatInt(rv.Int(), 10) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return strconv.FormatUint(rv.Uint(), 10) + case reflect.Float64: + return strconv.FormatFloat(rv.Float(), 'g', -1, 64) + case reflect.Float32: + return strconv.FormatFloat(rv.Float(), 'g', -1, 32) + case reflect.Bool: + return strconv.FormatBool(rv.Bool()) + } return fmt.Sprintf("%v", src) } + +func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) { + switch rv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return strconv.AppendInt(buf, rv.Int(), 10), true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return strconv.AppendUint(buf, rv.Uint(), 10), true + case reflect.Float32: + return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true + case reflect.Float64: + return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true + case reflect.Bool: + return strconv.AppendBool(buf, rv.Bool()), true + case reflect.String: + s := rv.String() + return append(buf, s...), true + } + return +} |
