Skip to content

Commit

Permalink
sql: use a string builder in decodeCopy
Browse files Browse the repository at this point in the history
For returning strings, a string builder is better than a bytes buffer.

```
name                  old time/op    new time/op    delta
DecodeCopySimple-12     10.8ns ± 1%    10.6ns ± 1%   -2.03%  (p=0.024 n=5+5)
DecodeCopyEscaped-12     249ns ± 1%     178ns ± 2%  -28.71%  (p=0.008 n=5+5)

name                  old alloc/op   new alloc/op   delta
DecodeCopySimple-12      0.00B          0.00B          ~     (all equal)
DecodeCopyEscaped-12     96.0B ± 0%     40.0B ± 0%  -58.33%  (p=0.008 n=5+5)

name                  old allocs/op  new allocs/op  delta
DecodeCopySimple-12       0.00           0.00          ~     (all equal)
DecodeCopyEscaped-12      2.00 ± 0%      2.00 ± 0%     ~     (all equal)
```

Release note: None
  • Loading branch information
maddyblue committed Aug 3, 2020
1 parent 470510e commit b4e6ea6
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/sql/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"context"
"io"
"strconv"
"strings"
"time"
"unsafe"

Expand Down Expand Up @@ -418,7 +419,7 @@ func (c *copyMachine) addRow(ctx context.Context, line []byte) error {
//
// See: https://www.postgresql.org/docs/9.5/static/sql-copy.html#AEN74432
func decodeCopy(in string) string {
var buf bytes.Buffer
var buf strings.Builder
start := 0
for i, n := 0, len(in); i < n; i++ {
if in[i] != '\\' {
Expand Down

0 comments on commit b4e6ea6

Please sign in to comment.