mirror of
https://github.com/jackc/pgx.git
synced 2025-05-29 10:42:31 +00:00
Reduce allocs and copies
This commit is contained in:
parent
932caef600
commit
a5f702c41d
14
messages.go
14
messages.go
@ -118,6 +118,20 @@ func (wb *WriteBuf) closeMsg() {
|
|||||||
binary.BigEndian.PutUint32(wb.buf[wb.sizeIdx:wb.sizeIdx+4], uint32(len(wb.buf)-wb.sizeIdx))
|
binary.BigEndian.PutUint32(wb.buf[wb.sizeIdx:wb.sizeIdx+4], uint32(len(wb.buf)-wb.sizeIdx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (wb *WriteBuf) reserveSize() int {
|
||||||
|
sizePosition := len(wb.buf)
|
||||||
|
wb.buf = append(wb.buf, 0, 0, 0, 0)
|
||||||
|
return sizePosition
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wb *WriteBuf) setComputedSize(sizePosition int) {
|
||||||
|
binary.BigEndian.PutUint32(wb.buf[sizePosition:], uint32(len(wb.buf)-sizePosition-4))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wb *WriteBuf) setSize(sizePosition int, size int32) {
|
||||||
|
binary.BigEndian.PutUint32(wb.buf[sizePosition:], uint32(size))
|
||||||
|
}
|
||||||
|
|
||||||
func (wb *WriteBuf) WriteByte(b byte) {
|
func (wb *WriteBuf) WriteByte(b byte) {
|
||||||
wb.buf = append(wb.buf, b)
|
wb.buf = append(wb.buf, b)
|
||||||
}
|
}
|
||||||
|
27
values.go
27
values.go
@ -106,29 +106,27 @@ func encodePreparedStatementArgument(wbuf *WriteBuf, oid pgtype.Oid, arg interfa
|
|||||||
|
|
||||||
switch arg := arg.(type) {
|
switch arg := arg.(type) {
|
||||||
case pgtype.BinaryEncoder:
|
case pgtype.BinaryEncoder:
|
||||||
buf := &bytes.Buffer{}
|
sp := wbuf.reserveSize()
|
||||||
null, err := arg.EncodeBinary(wbuf.conn.ConnInfo, buf)
|
null, err := arg.EncodeBinary(wbuf.conn.ConnInfo, wbuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if null {
|
if null {
|
||||||
wbuf.WriteInt32(-1)
|
wbuf.setSize(sp, -1)
|
||||||
} else {
|
} else {
|
||||||
wbuf.WriteInt32(int32(buf.Len()))
|
wbuf.setComputedSize(sp)
|
||||||
wbuf.WriteBytes(buf.Bytes())
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
case pgtype.TextEncoder:
|
case pgtype.TextEncoder:
|
||||||
buf := &bytes.Buffer{}
|
sp := wbuf.reserveSize()
|
||||||
null, err := arg.EncodeText(wbuf.conn.ConnInfo, buf)
|
null, err := arg.EncodeText(wbuf.conn.ConnInfo, wbuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if null {
|
if null {
|
||||||
wbuf.WriteInt32(-1)
|
wbuf.setSize(sp, -1)
|
||||||
} else {
|
} else {
|
||||||
wbuf.WriteInt32(int32(buf.Len()))
|
wbuf.setComputedSize(sp)
|
||||||
wbuf.WriteBytes(buf.Bytes())
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
case driver.Valuer:
|
case driver.Valuer:
|
||||||
@ -161,16 +159,15 @@ func encodePreparedStatementArgument(wbuf *WriteBuf, oid pgtype.Oid, arg interfa
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
sp := wbuf.reserveSize()
|
||||||
null, err := value.(pgtype.BinaryEncoder).EncodeBinary(wbuf.conn.ConnInfo, buf)
|
null, err := value.(pgtype.BinaryEncoder).EncodeBinary(wbuf.conn.ConnInfo, wbuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if null {
|
if null {
|
||||||
wbuf.WriteInt32(-1)
|
wbuf.setSize(sp, -1)
|
||||||
} else {
|
} else {
|
||||||
wbuf.WriteInt32(int32(buf.Len()))
|
wbuf.setComputedSize(sp)
|
||||||
wbuf.WriteBytes(buf.Bytes())
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user