Skip to content

Reduce memory allocation at several palces. #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions packet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"net"
"sync"

"crypto/rand"
"crypto/rsa"
Expand All @@ -15,6 +16,29 @@ import (
. "github.com/siddontang/go-mysql/mysql"
)

type BufPool struct {
pool *sync.Pool
}

func NewBufPool() *BufPool {
return &BufPool{
pool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
}
}

func (b *BufPool) Get() *bytes.Buffer {
return b.pool.Get().(*bytes.Buffer)
}

func (b *BufPool) Return(buf *bytes.Buffer) {
buf.Reset()
b.pool.Put(buf)
}

/*
Conn is the base class to handle MySQL protocol.
*/
Expand All @@ -24,24 +48,31 @@ type Conn struct {
// we removed the buffer reader because it will cause the SSLRequest to block (tls connection handshake won't be
// able to read the "Client Hello" data since it has been buffered into the buffer reader)

bufPool *BufPool

Sequence uint8
}

func NewConn(conn net.Conn) *Conn {
c := new(Conn)

c.Conn = conn
c.bufPool = NewBufPool()

return c
}

func (c *Conn) ReadPacket() ([]byte, error) {
var buf bytes.Buffer
// Here we use `sync.Pool` to avoid allocate/destroy buffers frequently.
buf := c.bufPool.Get()
defer c.bufPool.Return(buf)

if err := c.ReadPacketTo(&buf); err != nil {
if err := c.ReadPacketTo(buf); err != nil {
return nil, errors.Trace(err)
} else {
return buf.Bytes(), nil
result := make([]byte, buf.Len())
copy(result, buf.Bytes())
return result, nil
}
}

Expand All @@ -66,6 +97,11 @@ func (c *Conn) ReadPacketTo(w io.Writer) error {

c.Sequence++

if buf, ok := w.(*bytes.Buffer); ok {
// Allocate the buffer with expected length directly instead of call `grow` and migrate data many times.
buf.Grow(length)
}

if n, err := io.CopyN(w, c.Conn, int64(length)); err != nil {
return ErrBadConn
} else if n != int64(length) {
Expand Down
7 changes: 7 additions & 0 deletions replication/row_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ func (e *RowsEvent) Decode(data []byte) error {
}
}()

// Pre-allocate memory for rows.
rowsLen := e.ColumnCount
if e.needBitmap2 {
rowsLen += e.ColumnCount
}
e.Rows = make([][]interface{}, 0, rowsLen)

for pos < len(data) {
if n, err = e.decodeRows(data[pos:], e.Table, e.ColumnBitmap1); err != nil {
return errors.Trace(err)
Expand Down