Skip to content

Commit d14e375

Browse files
authored
Merge pull request #877 from tsenart/optimizations
Reduce writeBuf.string run-time by 87.68%
2 parents 99d4933 + e1aeba0 commit d14e375

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

buf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (b *writeBuf) int16(n int) {
6666
}
6767

6868
func (b *writeBuf) string(s string) {
69-
b.buf = append(b.buf, (s + "\000")...)
69+
b.buf = append(append(b.buf, s...), '\000')
7070
}
7171

7272
func (b *writeBuf) byte(c byte) {

buf_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pq
2+
3+
import "testing"
4+
5+
func Benchmark_writeBuf_string(b *testing.B) {
6+
var buf writeBuf
7+
const s = "foo"
8+
9+
b.ReportAllocs()
10+
b.ResetTimer()
11+
12+
for i := 0; i < b.N; i++ {
13+
buf.string(s)
14+
buf.buf = buf.buf[:0]
15+
}
16+
}

0 commit comments

Comments
 (0)