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

decrease the memory footprint of request/reply procedure #262

Open
wants to merge 1 commit into
base: master
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
10 changes: 5 additions & 5 deletions core/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ func (ch *Channel) nextSeqNum() uint16 {
}

func (ch *Channel) newRequest(msg api.Message, multi bool) *vppRequest {
return &vppRequest{
msg: msg,
seqNum: ch.nextSeqNum(),
multi: multi,
}
request := ch.conn.requestPool.Get().(*vppRequest)
request.msg = msg
request.seqNum = ch.nextSeqNum()
request.multi = multi
return request
}

func (ch *Channel) CheckCompatiblity(msgs ...api.Message) error {
Expand Down
5 changes: 5 additions & 0 deletions core/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ type Connection struct {
msgControlPing api.Message
msgControlPingReply api.Message

requestPool sync.Pool
replyPool sync.Pool

traceLock sync.Mutex
trace *Trace // API tracer (disabled by default)
}
Expand Down Expand Up @@ -170,6 +173,8 @@ func newConnection(binapi adapter.VppAPI, attempts int, interval time.Duration,
msgControlPing: msgControlPing,
msgControlPingReply: msgControlPingReply,
channelIdPool: newIDPool(0x7fff),
requestPool: sync.Pool{New: func() interface{} { return &vppRequest{} }},
replyPool: sync.Pool{New: func() interface{} { return &vppReply{} }},
}
c.channelPool = genericpool.New[*Channel](func() *Channel {
if isDebugOn(debugOptChannels) {
Expand Down
13 changes: 7 additions & 6 deletions core/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,15 @@ func (c *Connection) msgCallback(msgID uint16, data []byte) {
// treat this as a last part of the reply
lastReplyReceived := isMulti && msgID == c.pingReplyID

reply := c.replyPool.Get().(*vppReply)
reply.msgID = msgID
reply.seqNum = seqNum
reply.data = append([]byte(nil), data...)
reply.lastReceived = lastReplyReceived

// send the data to the channel, it needs to be copied,
// because it will be freed after this function returns
sendReply(ch, &vppReply{
msgID: msgID,
seqNum: seqNum,
data: append([]byte(nil), data...),
lastReceived: lastReplyReceived,
})
sendReply(ch, reply)

// store actual time of this reply
c.lastReplyLock.Lock()
Expand Down
6 changes: 5 additions & 1 deletion core/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (s *Stream) SendMsg(msg api.Message) error {
if err := s.conn.processRequest(s.channel, req); err != nil {
return err
}
s.conn.requestPool.Put(req)
s.Lock()
s.pkgPath = s.conn.GetMessagePath(msg)
s.Unlock()
Expand All @@ -216,9 +217,12 @@ func (s *Stream) RecvMsg() (api.Message, error) {
// allocate message instance
msg = reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message)
// decode message data
if err := s.channel.msgCodec.DecodeMsg(reply.data, msg); err != nil {
if err = s.channel.msgCodec.DecodeMsg(reply.data, msg); err != nil {
return nil, err
}
reply.err = nil
s.conn.replyPool.Put(reply)

return msg, nil
}

Expand Down
Loading