Skip to content
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

feat: use sync.Pool to reduce alloc #729

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion ua/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ua

import (
"bytes"
"encoding/binary"
"io"
"math"
Expand Down Expand Up @@ -156,7 +157,7 @@ func (b *Buffer) ReadBytes() []byte {
if b.err != nil {
return nil
}
return d
return bytes.Clone(d)
}

func (b *Buffer) ReadStruct(r interface{}) {
Expand Down
34 changes: 34 additions & 0 deletions uacp/buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uacp

import "sync"

type Buffer struct {
buf []byte
}

func (b *Buffer) Bytes() []byte {
return b.buf
}

var bufferPool sync.Pool

func AllocBuffer(size int) *Buffer {
v := bufferPool.Get()
if v == nil {
return &Buffer{
buf: make([]byte, size),
}
}
buf := v.(*Buffer)
if cap(buf.buf) < size {
buf.buf = make([]byte, size)
} else {
buf.buf = buf.buf[:size]
}
return buf
}

func FreeBuffer(b *Buffer) {
b.buf = b.buf[:0]
bufferPool.Put(b)
}
14 changes: 7 additions & 7 deletions uacp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ func (c *Conn) Handshake(ctx context.Context, endpoint string) error {
return err
}

b, err := c.Receive()
buf := AllocBuffer(int(c.ack.ReceiveBufSize))
defer FreeBuffer(buf)
b, err := c.Receive(buf.Bytes())
if err != nil {
return err
}
Expand Down Expand Up @@ -282,7 +284,9 @@ func (c *Conn) Handshake(ctx context.Context, endpoint string) error {
}

func (c *Conn) srvhandshake(endpoint string) error {
b, err := c.Receive()
buf := AllocBuffer(int(c.ack.ReceiveBufSize))
defer FreeBuffer(buf)
b, err := c.Receive(buf.Bytes())
if err != nil {
c.SendError(ua.StatusBadTCPInternalError)
return err
Expand Down Expand Up @@ -350,11 +354,7 @@ const hdrlen = 8
// Receive reads a full UACP message from the underlying connection.
// The size of b must be at least ReceiveBufSize. Otherwise,
// the function returns an error.
func (c *Conn) Receive() ([]byte, error) {
// TODO(kung-foo): allow user-specified buffer
// TODO(kung-foo): sync.Pool
b := make([]byte, c.ack.ReceiveBufSize)

func (c *Conn) Receive(b []byte) ([]byte, error) {
if _, err := io.ReadFull(c, b[:hdrlen]); err != nil {
// todo(fs): do not wrap this error since it hides io.EOF
// todo(fs): use golang.org/x/xerrors
Expand Down
4 changes: 3 additions & 1 deletion uacp/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ NEXT:
t.Fatal(err)
}

got, err := srvConn.Receive()
buf := AllocBuffer(int(srvConn.ack.ReceiveBufSize))
defer FreeBuffer(buf)
got, err := srvConn.Receive(buf.Bytes())
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion uasc/secure_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,9 @@ func (s *SecureChannel) receive(ctx context.Context) *response {

func (s *SecureChannel) readChunk() (*MessageChunk, error) {
// read a full message from the underlying conn.
b, err := s.c.Receive()
buf := uacp.AllocBuffer(int(s.c.ReceiveBufSize()))
defer uacp.FreeBuffer(buf)
b, err := s.c.Receive(buf.Bytes())
if err == io.EOF || len(b) == 0 {
return nil, io.EOF
}
Expand Down