2
0

Replace MarshalBinary with Encode

This new approach can avoid allocations.
This commit is contained in:
Jack Christensen
2017-05-26 17:00:44 -05:00
parent b1934ad4c2
commit d6312305ae
35 changed files with 277 additions and 285 deletions
+11 -10
View File
@@ -4,6 +4,8 @@ import (
"bytes"
"encoding/binary"
"encoding/json"
"github.com/jackc/pgx/pgio"
)
type CopyBothResponse struct {
@@ -37,20 +39,19 @@ func (dst *CopyBothResponse) Decode(src []byte) error {
return nil
}
func (src *CopyBothResponse) MarshalBinary() ([]byte, error) {
var bigEndian BigEndianBuf
buf := &bytes.Buffer{}
buf.WriteByte('W')
buf.Write(bigEndian.Uint32(uint32(4 + 1 + 2 + 2*len(src.ColumnFormatCodes))))
buf.Write(bigEndian.Uint16(uint16(len(src.ColumnFormatCodes))))
func (src *CopyBothResponse) Encode(dst []byte) []byte {
dst = append(dst, 'W')
sp := len(dst)
dst = pgio.AppendInt32(dst, -1)
dst = pgio.AppendUint16(dst, uint16(len(src.ColumnFormatCodes)))
for _, fc := range src.ColumnFormatCodes {
buf.Write(bigEndian.Uint16(fc))
dst = pgio.AppendUint16(dst, fc)
}
return buf.Bytes(), nil
pgio.SetInt32(dst[sp:], int32(len(dst[sp:])))
return dst
}
func (src *CopyBothResponse) MarshalJSON() ([]byte, error) {