Replace MarshalBinary with Encode
This new approach can avoid allocations.
This commit is contained in:
+10
-8
@@ -3,6 +3,8 @@ package pgproto3
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/jackc/pgx/pgio"
|
||||
)
|
||||
|
||||
type PasswordMessage struct {
|
||||
@@ -23,14 +25,14 @@ func (dst *PasswordMessage) Decode(src []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (src *PasswordMessage) MarshalBinary() ([]byte, error) {
|
||||
var bigEndian BigEndianBuf
|
||||
buf := &bytes.Buffer{}
|
||||
buf.WriteByte('p')
|
||||
buf.Write(bigEndian.Uint32(uint32(4 + len(src.Password) + 1)))
|
||||
buf.WriteString(src.Password)
|
||||
buf.WriteByte(0)
|
||||
return buf.Bytes(), nil
|
||||
func (src *PasswordMessage) Encode(dst []byte) []byte {
|
||||
dst = append(dst, 'p')
|
||||
dst = pgio.AppendInt32(dst, int32(4+len(src.Password)+1))
|
||||
|
||||
dst = append(dst, src.Password...)
|
||||
dst = append(dst, 0)
|
||||
|
||||
return dst
|
||||
}
|
||||
|
||||
func (src *PasswordMessage) MarshalJSON() ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user